Example #1
0
        /// <summary>
        /// See <see cref="Stream.Dispose(bool)"/>
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    try
                    {
                        if (_CompressStream != null)
                        {
                            _CompressStream.Dispose();
                        }
                    }
                    finally
                    {
                        _CompressStream = null;
                    }

                    try
                    {
                        if (_DecompressStream != null)
                        {
                            _DecompressStream.Dispose();
                        }
                    }
                    finally
                    {
                        _DecompressStream = null;
                    }

                    try
                    {
                        if ((_Stream != null) && !_LeaveOpen)
                        {
                            _Stream.Dispose();
                        }
                    }
                    finally
                    {
                        _Stream = null;
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DeflateParallelStream"/> class
        /// using the specified <paramref name="stream"/> and <paramref name="compressionMode"/>,
        /// and optionally leaves the stream open.
        /// </summary>
        /// <param name="stream">The stream to compress or decompress.</param>
        /// <param name="compressionMode">One of the enumeration values that indicates whether to compress or decompress the stream.</param>
        /// <param name="leaveOpen">true to leave the stream object open after disposing the <see cref="DeflateParallelStream"/> object; otherwise, false.</param>
        public DeflateParallelStream(Stream stream, CompressionMode compressionMode, bool leaveOpen)
        {
            #region Contracts

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if ((compressionMode == CompressionMode.Compress) && !stream.CanWrite)
            {
                throw new NotSupportedException("stream does not support writing");
            }

            if ((compressionMode == CompressionMode.Decompress) && !stream.CanRead)
            {
                throw new NotSupportedException("stream does not support reading");
            }

            Contract.EndContractBlock();

            #endregion

            _Stream = stream;

            _LeaveOpen = leaveOpen;

            switch (compressionMode)
            {
            case CompressionMode.Compress:

                _CompressStream = new CompressStream(_Stream, CompressionLevel.Optimal, DefaultBufferSize);

                break;

            case CompressionMode.Decompress:

                _DecompressStream = new DecompressStream(_Stream);

                break;

            default:

                throw new NotSupportedException();
            }
        }