Esempio n. 1
0
        public override void Flush()
        {
            if (_mode == ZLibMode.Decompress)
            {
                BaseStream.Flush();
                return;
            }

            using (PinnedArray pinWrite = new PinnedArray(_internalBuf))
            {
                _zstream.NextIn   = IntPtr.Zero;
                _zstream.AvailIn  = 0;
                _zstream.NextOut  = pinWrite[_internalBufPos];
                _zstream.AvailOut = (uint)(_internalBuf.Length - _internalBufPos);

                ZLibReturnCode ret = ZLibReturnCode.OK;
                while (ret != ZLibReturnCode.STREAM_END)
                {
                    if (_zstream.AvailOut != 0)
                    {
                        uint outCount = _zstream.AvailOut;
                        ret = ZLibNative.Deflate(_zstream, ZLibFlush.FINISH);

                        _internalBufPos += (int)(outCount - _zstream.AvailOut);

                        if (ret != ZLibReturnCode.STREAM_END && ret != ZLibReturnCode.OK)
                        {
                            throw new Exception(ret + " " + _zstream.LastErrorMsg);
                        }
                    }

                    BaseStream.Write(_internalBuf, 0, _internalBufPos);
                    TotalOut += _internalBufPos;

                    _internalBufPos   = 0;
                    _zstream.NextOut  = pinWrite;
                    _zstream.AvailOut = (uint)_internalBuf.Length;
                }
            }

            BaseStream.Flush();
        }
Esempio n. 2
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_mode != ZLibMode.Compress)
            {
                throw new NotSupportedException("Write not supported on decompression");
            }

            TotalIn += count;

            using (PinnedArray pinRead = new PinnedArray(buffer))
                using (PinnedArray pinWrite = new PinnedArray(_internalBuf))
                {
                    _zstream.NextIn   = pinRead[offset];
                    _zstream.AvailIn  = (uint)count;
                    _zstream.NextOut  = pinWrite[_internalBufPos];
                    _zstream.AvailOut = (uint)(_internalBuf.Length - _internalBufPos);

                    while (_zstream.AvailIn != 0)
                    {
                        uint           outCount = _zstream.AvailOut;
                        ZLibReturnCode ret      = ZLibNative.Deflate(_zstream, ZLibFlush.NO_FLUSH);
                        _internalBufPos += (int)(outCount - _zstream.AvailOut);

                        if (_zstream.AvailOut == 0)
                        {
                            BaseStream.Write(_internalBuf, 0, _internalBuf.Length);
                            TotalOut += _internalBuf.Length;

                            _internalBufPos   = 0;
                            _zstream.NextOut  = pinWrite;
                            _zstream.AvailOut = (uint)_internalBuf.Length;
                        }

                        if (ret != ZLibReturnCode.OK)
                        {
                            throw new Exception(ret + " " + _zstream.LastErrorMsg);
                        }
                    }
                }
        }