Example #1
0
        internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, bool usingGZip)
        {
            this._stream = stream;
            this._mode = mode;
            this._leaveOpen = leaveOpen;
            if (this._stream == null) {
                throw new ArgumentNullException("stream");
            }
            switch (this._mode) {
                case CompressionMode.Decompress:
                    if (!this._stream.CanRead) {
                        throw new ArgumentException("The base stream is not readable.", "stream");
                    }
                    this.inflater = new Inflater(usingGZip);
                    this.m_CallBack = new AsyncCallback(this.ReadCallback);
                    break;

                case CompressionMode.Compress:
                    if (!this._stream.CanWrite) {
                        throw new ArgumentException("The base stream is not writeable.", "stream");
                    }
                    this.deflater = new Deflater(usingGZip);
                    this.m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                    this.m_CallBack = new AsyncCallback(this.WriteCallback);
                    break;

                default:
                    throw new ArgumentException("Enum value was out of legal range.", "mode");
            }
            this.buffer = new byte[0x1000];
        }
Example #2
0
        public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
        {
            this._stream    = stream;
            this._mode      = mode;
            this._leaveOpen = leaveOpen;
            if (this._stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            switch (this._mode)
            {
            case CompressionMode.Decompress:
                if (!this._stream.CanRead)
                {
                    throw new ArgumentException(SR.GetString("NotReadableStream"), "stream");
                }
                this.inflater   = new Inflater();
                this.m_CallBack = new AsyncCallback(this.ReadCallback);
                break;

            case CompressionMode.Compress:
                if (!this._stream.CanWrite)
                {
                    throw new ArgumentException(SR.GetString("NotWriteableStream"), "stream");
                }
                this.deflater = new Deflater();
                this.m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                this.m_CallBack            = new AsyncCallback(this.WriteCallback);
                break;

            default:
                throw new ArgumentException(SR.GetString("ArgumentOutOfRange_Enum"), "mode");
            }
            this.buffer = new byte[0x1000];
        }
Example #3
0
        internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, bool usingGZip)
        {
            this._stream    = stream;
            this._mode      = mode;
            this._leaveOpen = leaveOpen;
            if (this._stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            switch (this._mode)
            {
            case CompressionMode.Decompress:
                if (!this._stream.CanRead)
                {
                    throw new ArgumentException("The base stream is not readable.", "stream");
                }
                this.inflater   = new Inflater(usingGZip);
                this.m_CallBack = new AsyncCallback(this.ReadCallback);
                break;

            case CompressionMode.Compress:
                if (!this._stream.CanWrite)
                {
                    throw new ArgumentException("The base stream is not writeable.", "stream");
                }
                this.deflater = new Deflater(usingGZip);
                this.m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                this.m_CallBack            = new AsyncCallback(this.WriteCallback);
                break;

            default:
                throw new ArgumentException("Enum value was out of legal range.", "mode");
            }
            this.buffer = new byte[0x1000];
        }
        internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, bool usingGZip){                                                                                          
            _stream = stream;
            _mode = mode;
            _leaveOpen = leaveOpen;

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

            switch (_mode) {
            case CompressionMode.Decompress:
                if (!(_stream.CanRead)) {
                    throw new ArgumentException(SR.GetString(SR.NotReadableStream), "stream");
                }
                inflater = new Inflater(usingGZip);
                m_CallBack = new AsyncCallback(ReadCallback); 
                break;

            case CompressionMode.Compress:
                if (!(_stream.CanWrite)) {
                    throw new ArgumentException(SR.GetString(SR.NotWriteableStream), "stream");
                }
                deflater = new Deflater(usingGZip);
                m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                m_CallBack = new AsyncCallback(WriteCallback); 
                break;    

            default:
                throw new ArgumentException(SR.GetString(SR.ArgumentOutOfRange_Enum), "mode");
            }           
            buffer = new byte[bufferSize];
        }
        public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
        {
            this._stream = stream;
            this._mode = mode;
            this._leaveOpen = leaveOpen;
            if (this._stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            switch (this._mode)
            {
                case CompressionMode.Decompress:
                    if (!this._stream.CanRead)
                    {
                        throw new ArgumentException(SR.GetString("NotReadableStream"), "stream");
                    }
                    this.inflater = new Inflater();
                    this.m_CallBack = new AsyncCallback(this.ReadCallback);
                    break;

                case CompressionMode.Compress:
                    if (!this._stream.CanWrite)
                    {
                        throw new ArgumentException(SR.GetString("NotWriteableStream"), "stream");
                    }
                    this.deflater = new Deflater();
                    this.m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                    this.m_CallBack = new AsyncCallback(this.WriteCallback);
                    break;

                default:
                    throw new ArgumentException(SR.GetString("ArgumentOutOfRange_Enum"), "mode");
            }
            this.buffer = new byte[0x1000];
        }
Example #6
0
 // Close this stream.
 public override void Close()
 {
     if (stream != null)
     {
         if (deflater != null)
         {
             int temp;
             deflater.Finish();
             while (!deflater.IsFinished)
             {
                 temp = deflater.Deflate(buf, 0, buf.Length);
                 if (temp <= 0)
                 {
                     if (!deflater.IsFinished)
                     {
                         throw new IOException
                                   ("IO_Compress_Input");
                     }
                     break;
                 }
                 stream.Write(buf, 0, temp);
             }
         }
         if (!leaveOpen)
         {
             stream.Close();
         }
         stream   = null;
         inflater = null;
         deflater = null;
         buf      = null;
     }
 }
Example #7
0
        /// <summary>
        /// Sets up this DeflateStream to be used for Zlib Deflation/Compression
        /// </summary>
        internal void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
        {
            Debug.Assert(stream != null);
            if (!stream.CanWrite)
            {
                throw new ArgumentException(SR.NotSupported_UnwritableStream, nameof(stream));
            }

            _deflater = new Deflater(compressionLevel, windowBits);

            _stream    = stream;
            _mode      = CompressionMode.Compress;
            _leaveOpen = leaveOpen;
            InitializeBuffer();
        }
Example #8
0
        /// <summary>
        /// Sets up this DeflateStream to be used for Zlib Deflation/Compression
        /// </summary>
        internal void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
        {
            Debug.Assert(stream != null);
            if (!stream.CanWrite)
            {
                throw new ArgumentException(SR.NotWriteableStream, "stream");
            }

            _deflater = new Deflater(compressionLevel, windowBits);

            _stream    = stream;
            _mode      = CompressionMode.Compress;
            _leaveOpen = leaveOpen;
            _buffer    = new byte[DefaultBufferSize];
        }
Example #9
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                PurgeBuffers(disposing);
            }
            finally
            {
                // Close the underlying stream even if PurgeBuffers threw.
                // Stream.Close() may throw here (may or may not be due to the same error).
                // In this case, we still need to clean up internal resources, hence the inner finally blocks.
                try
                {
                    if (disposing && !_leaveOpen)
                    {
                        _stream?.Dispose();
                    }
                }
                finally
                {
                    _stream = null;

                    try
                    {
                        _deflater?.Dispose();
                        _inflater?.Dispose();
                    }
                    finally
                    {
                        _deflater = null;
                        _inflater = null;

                        byte[] buffer = _buffer;
                        if (buffer != null)
                        {
                            _buffer = null;
                            if (!AsyncOperationIsActive)
                            {
                                ArrayPool <byte> .Shared.Return(buffer);
                            }
                        }

                        base.Dispose(disposing);
                    }
                }
            }
        }
Example #10
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                PurgeBuffers(disposing);
            }
            finally
            {
                // Close the underlying stream even if PurgeBuffers threw.
                // Stream.Close() may throw here (may or may not be due to the same error).
                // In this case, we still need to clean up internal resources, hence the inner finally blocks.
                try
                {
                    if (disposing && !_leaveOpen && _stream != null)
                    {
                        _stream.Dispose();
                    }
                }
                finally
                {
                    _stream = null;

                    try
                    {
                        if (_deflater != null)
                        {
                            _deflater.Dispose();
                        }
                        if (_inflater != null)
                        {
                            _inflater.Dispose();
                        }
                    }
                    finally
                    {
                        if (_buffer != null)
                        {
                            ArrayPool <byte> .Shared.Return(_buffer, clearArray : true);
                        }
                        _buffer   = null;
                        _deflater = null;
                        _inflater = null;
                        base.Dispose(disposing);
                    }
                }
            }
        }
Example #11
0
        // Note: The .NET Framework SDK 2.0 version of this class does
        // not have BeginWrite and EndWrite for some inexplicable reason.

        // Close this stream.
        public override void Close()
        {
            if (stream != null)
            {
                if (deflater != null)
                {
                    int temp;
                    deflater.Finish();
                    while (!deflater.IsFinished)
                    {
                        temp = deflater.Deflate(buf, 0, buf.Length);
                        if (temp <= 0)
                        {
                            if (!deflater.IsFinished)
                            {
                                throw new IOException
                                          (S._("IO_Compress_Input"));
                            }
                            break;
                        }
                        stream.Write(buf, 0, temp);
                    }
                    byte[] footer = new byte [8];
                    temp      = (int)(crc32.Value);
                    footer[0] = (byte)temp;
                    footer[1] = (byte)(temp << 8);
                    footer[2] = (byte)(temp << 16);
                    footer[3] = (byte)(temp << 24);
                    temp      = deflater.TotalIn;
                    footer[4] = (byte)temp;
                    footer[5] = (byte)(temp << 8);
                    footer[6] = (byte)(temp << 16);
                    footer[7] = (byte)(temp << 24);
                    stream.Write(footer, 0, 8);
                }
                if (!leaveOpen)
                {
                    stream.Close();
                }
                stream   = null;
                inflater = null;
                deflater = null;
                buf      = null;
            }
        }
Example #12
0
 public GZipStream(Stream stream, CompressionMode mode, bool leaveOpen)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (mode == CompressionMode.Decompress)
     {
         if (!stream.CanRead)
         {
             throw new ArgumentException
                       (S._("IO_NotReadable"), "stream");
         }
     }
     else if (mode == CompressionMode.Compress)
     {
         if (!stream.CanWrite)
         {
             throw new ArgumentException
                       (S._("IO_NotWritable"), "stream");
         }
     }
     else
     {
         throw new ArgumentException
                   (S._("IO_CompressionMode"), "mode");
     }
     this.stream      = stream;
     this.mode        = mode;
     this.leaveOpen   = leaveOpen;
     this.buf         = new byte [4096];
     this.crc32       = new Crc32();
     this.endOfStream = false;
     this.headerDone  = false;
     if (mode == CompressionMode.Decompress)
     {
         inflater = new Inflater(true);
     }
     else
     {
         deflater = new Deflater(-1, true);
     }
 }
Example #13
0
 public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (mode == CompressionMode.Decompress)
     {
         if (!stream.CanRead)
         {
             throw new ArgumentException
                       ("IO_NotReadable", "stream");
         }
     }
     else if (mode == CompressionMode.Compress)
     {
         if (!stream.CanWrite)
         {
             throw new ArgumentException
                       ("IO_NotWritable", "stream");
         }
     }
     else
     {
         throw new ArgumentException
                   ("IO_CompressionMode", "mode");
     }
     this.stream    = stream;
     this.mode      = mode;
     this.leaveOpen = leaveOpen;
     this.buf       = new byte [4096];
     if (mode == CompressionMode.Decompress)
     {
         inflater = new Inflater();
     }
     else
     {
         deflater = new Deflater();
     }
 }
        internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, bool usingGZip)
        {
            _stream    = stream;
            _mode      = mode;
            _leaveOpen = leaveOpen;

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

            switch (_mode)
            {
            case CompressionMode.Decompress:
                if (!(_stream.CanRead))
                {
                    throw new ArgumentException(SR.GetString(SR.NotReadableStream), "stream");
                }
                inflater   = new Inflater(usingGZip);
                m_CallBack = new AsyncCallback(ReadCallback);
                break;

            case CompressionMode.Compress:
                if (!(_stream.CanWrite))
                {
                    throw new ArgumentException(SR.GetString(SR.NotWriteableStream), "stream");
                }
                deflater = new Deflater(usingGZip);
                m_AsyncWriterDelegate = new AsyncWriteDelegate(this.InternalWrite);
                m_CallBack            = new AsyncCallback(WriteCallback);
                break;

            default:
                throw new ArgumentException(SR.GetString(SR.ArgumentOutOfRange_Enum), "mode");
            }
            buffer = new byte[bufferSize];
        }
 public WritableDeflateTransform(CompressionLevel compressionLevel, int bits)
 {
     _deflater = new Deflater(compressionLevel, bits);
 }
        /// <summary>
        /// Sets up this DeflateStream to be used for Zlib Deflation/Compression
        /// </summary>
        internal void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
        {
            Debug.Assert(stream != null);
            if (!stream.CanWrite)
                throw new ArgumentException(SR.NotSupported_UnwritableStream, nameof(stream));

            _deflater = new Deflater(compressionLevel, windowBits);

            _stream = stream;
            _mode = CompressionMode.Compress;
            _leaveOpen = leaveOpen;
            IntializeBuffer();
        }
Example #17
0
        /// <summary>
        /// Sets up this DeflateStream to be used for Zlib Deflation/Compression
        /// </summary>
        internal void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
        {
            Debug.Assert(stream != null);
            if (!stream.CanWrite)
                throw new ArgumentException(SR.NotWriteableStream, "stream");

            _deflater = new Deflater(compressionLevel, windowBits);

            _stream = stream;
            _mode = CompressionMode.Compress;
            _leaveOpen = leaveOpen;
            _buffer = new byte[DefaultBufferSize];
        }
Example #18
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                PurgeBuffers(disposing);
            }
            finally
            {
                // Close the underlying stream even if PurgeBuffers threw.
                // Stream.Close() may throw here (may or may not be due to the same error).
                // In this case, we still need to clean up internal resources, hence the inner finally blocks.
                try
                {
                    if (disposing && !_leaveOpen)
                        _stream?.Dispose();
                }
                finally
                {
                    _stream = null;

                    try
                    {
                        _deflater?.Dispose();
                        _inflater?.Dispose();
                    }
                    finally
                    {
                        _deflater = null;
                        _inflater = null;

                        byte[] buffer = _buffer;
                        if (buffer != null)
                        {
                            _buffer = null;
                            if (!AsyncOperationIsActive)
                            {
                                ArrayPool<byte>.Shared.Return(buffer);
                            }
                        }

                        base.Dispose(disposing);
                    }
                }
            }
        }
        protected override void Dispose(bool disposing)
        {
            try
            {
                PurgeBuffers(disposing);
            }
            finally
            {
                // Close the underlying stream even if PurgeBuffers threw.
                // Stream.Close() may throw here (may or may not be due to the same error).
                // In this case, we still need to clean up internal resources, hence the inner finally blocks.
                try
                {
                    if (disposing && !_leaveOpen && _stream != null)
                        _stream.Dispose();
                }
                finally
                {
                    _stream = null;

                    try
                    {
                        if (_deflater != null)
                            _deflater.Dispose();
                        if (_inflater != null)
                            _inflater.Dispose();
                    }
                    finally
                    {
                        _deflater = null;
                        _inflater = null;
                        base.Dispose(disposing);
                    }
                }
            }
        }
Example #20
0
        /// <summary>
        /// Sets up this DeflateStream to be used for Zlib Deflation/Compression
        /// </summary>
        internal void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
        {
            Debug.Assert(stream != null);
            if (!stream.CanWrite)
                throw new ArgumentException(SR.NotSupported_UnwritableStream, "stream");

            _deflater = new Deflater(compressionLevel, windowBits);

            _stream = stream;
            _mode = CompressionMode.Compress;
            _leaveOpen = leaveOpen;
            _buffer = ArrayPool<byte>.Shared.Rent(DefaultBufferSize);
        }