Example #1
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     _baseStream.Write(buffer, offset, count);
     using (PinnedArray pinRead = new PinnedArray(buffer))
     {
         _crc32 = NativeMethods.Crc32(_crc32, pinRead[offset], (uint)count);
     }
 }
Example #2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int readLen = _baseStream.Read(buffer, offset, count);

            using (PinnedArray pinRead = new PinnedArray(buffer))
            {
                _crc32 = NativeMethods.Crc32(_crc32, pinRead[offset], (uint)readLen);
            }
            return(readLen);
        }
Example #3
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (_mode != ZLibMode.Decompress)
            {
                throw new NotSupportedException("Read() not supported on compression");
            }

            ValidateReadWriteArgs(buffer, offset, count);

            int readLen = 0;

            if (_internalBufPos != -1)
            {
                using (PinnedArray pinRead = new PinnedArray(_internalBuf)) // [In] Compressed
                    using (PinnedArray pinWrite = new PinnedArray(buffer))  // [Out] Will-be-decompressed
                    {
                        _zstream.NextIn   = pinRead[_internalBufPos];
                        _zstream.NextOut  = pinWrite[offset];
                        _zstream.AvailOut = (uint)count;

                        while (0 < _zstream.AvailOut)
                        {
                            if (_zstream.AvailIn == 0)
                            { // Compressed Data is no longer available in array, so read more from _stream
                                int baseReadSize = _baseStream.Read(_internalBuf, 0, _internalBuf.Length);

                                _internalBufPos  = 0;
                                _zstream.NextIn  = pinRead;
                                _zstream.AvailIn = (uint)baseReadSize;
                                TotalIn         += baseReadSize;
                            }

                            uint inCount  = _zstream.AvailIn;
                            uint outCount = _zstream.AvailOut;

                            // flush method for inflate has no effect
                            ZLibReturnCode ret = NativeMethods.Inflate(_zstream, ZLibFlush.NO_FLUSH);

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

                            if (ret == ZLibReturnCode.STREAM_END)
                            {
                                _internalBufPos = -1; // magic for StreamEnd
                                break;
                            }

                            ZLibException.CheckZLibOK(ret, _zstream);
                        }

                        TotalOut += readLen;
                    }
            }
            return(readLen);
        }
Example #4
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 = NativeMethods.Deflate(_zstream, ZLibFlush.FINISH);

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

                        if (ret != ZLibReturnCode.STREAM_END && ret != ZLibReturnCode.OK)
                        {
                            throw new ZLibException(ret, _zstream.LastErrorMsg);
                        }
                    }

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

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

            _baseStream.Flush();
        }
Example #5
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      = NativeMethods.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;
                        }

                        ZLibException.CheckZLibOK(ret, _zstream);
                    }
                }
        }