Example #1
0
        public void OnStreamPartChange(PackageDataStreamPart oldPart, PackageDataStreamPart newPart)
        {
            EnsureNotDisposed();

            bool keepSameStream = oldPart != null && newPart != null && oldPart.Path == newPart.Path;

            if (keepSameStream)
            {
                // move stream to new part
                newPart.Stream = oldPart.Stream;
                currentPart.FileStream.Seek(newPart.SegmentOffsetInDataFile, SeekOrigin.Begin);
            }
            else
            {
                // close old one
                if (oldPart != null)
                {
                    DisposeCurrentPart();
                }

                // open new part
                if (newPart != null)
                {
                    currentPart            = new CurrentPart();
                    currentPart.Part       = newPart;
                    currentPart.FileStream = new FileStream(newPart.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    currentPart.FileStream.Seek(newPart.SegmentOffsetInDataFile, SeekOrigin.Begin);
                    currentPart.Part.Stream = currentPart.FileStream;
                }
            }
        }
Example #2
0
        public void OnStreamPartChange(PackageDataStreamPart oldPart, PackageDataStreamPart newPart)
        {
            EnsureNotDisposed();

            bool keepSameStream = oldPart != null && newPart != null && oldPart.Path == newPart.Path;

            // compute hash
            if (oldPart != null)
            {
                ComputeCurrentPartHash();
            }

            if (!keepSameStream)
            {
                // close old one
                if (oldPart != null)
                {
                    DisposeCurrentPart();
                }

                // open new part
                if (newPart != null)
                {
                    logger.LogTrace($"Opening data file {Path.GetFileName(newPart.Path)} for writing.");

                    currentPart = new CurrentPart
                    {
                        Part = newPart,
                    };
                }
            }

            // update current part
            if (newPart != null)
            {
                currentPart.Part          = newPart;
                currentPart.HashAlgorithm = cryptoProvider.CreateHashAlgorithm();
                if (writeToNestedStream)
                {
                    // allocate and write to memory stream
                    memStream.Position = 0;
                    memStream.SetLength(0);
                    currentPart.HashStream = new CryptoStream(memStream, currentPart.HashAlgorithm, CryptoStreamMode.Write, leaveOpen: true);
                }
                else
                {
                    // write to NULL - just compute hash
                    currentPart.HashStream = new CryptoStream(Stream.Null, currentPart.HashAlgorithm, CryptoStreamMode.Write, leaveOpen: true);
                }
                currentPart.Part.Stream = currentPart.HashStream;
            }
        }
Example #3
0
        public void OnStreamPartChange(PackageDataStreamPart oldPart, PackageDataStreamPart newPart)
        {
            EnsureNotDisposed();

            bool keepSameStream = oldPart != null && newPart != null && oldPart.Path == newPart.Path;

            // compute hash
            if (oldPart != null)
            {
                ComputeCurrentPartHash();
            }

            if (!keepSameStream)
            {
                // close old one
                if (oldPart != null)
                {
                    DisposeCurrentPart();
                }

                // open new part
                if (newPart != null)
                {
                    logger.LogDebug($"Creating new data file {Path.GetFileName(newPart.Path)}. Already wrote {SizeFormatter.ToString(totalSize)}.");

                    currentPart = new CurrentPart
                    {
                        Part       = newPart,
                        FileStream = new FileStream(newPart.Path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)
                    };
                    currentPart.FileStream.SetLength(newPart.DataFileLength);
                }
            }

            // update current part
            if (newPart != null)
            {
                currentPart.Part = newPart;
                currentPart.FileStream.Seek(newPart.SegmentOffsetInDataFile, SeekOrigin.Begin);
                currentPart.HashAlgorithm = cryptoProvider.CreateHashAlgorithm();
                currentPart.HashStream    = new CryptoStream(currentPart.FileStream, currentPart.HashAlgorithm, CryptoStreamMode.Write, leaveOpen: true);
                currentPart.Part.Stream   = currentPart.HashStream;
            }
        }
Example #4
0
        public void OnStreamPartChange(PackageDataStreamPart oldPart, PackageDataStreamPart newPart)
        {
            EnsureNotDisposed();

            bool keepSameStream = oldPart != null && newPart != null && oldPart.Path == newPart.Path;

            // flush
            if (oldPart != null)
            {
                FlushCurrentPart();
            }

            if (!keepSameStream)
            {
                // close old one
                if (oldPart != null)
                {
                    DisposeCurrentPart();
                }

                // open new part
                if (newPart != null)
                {
                    logger.LogTrace($"Opening data file {Path.GetFileName(newPart.Path)} for writing.");

                    currentPart = new CurrentPart
                    {
                        Part       = newPart,
                        FileStream = new FileStream(newPart.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
                    };
                }
            }

            // update current part
            if (newPart != null)
            {
                currentPart.Part = newPart;
                currentPart.FileStream.Seek(newPart.SegmentOffsetInDataFile, SeekOrigin.Begin);
                currentPart.Part.Stream = currentPart.FileStream;
            }
        }