private void WriteInternal(byte[] buffer, int offset, int count, int flush) { _zstream.next_in = buffer; _zstream.next_in_index = offset; _zstream.avail_in = count; while (true) { _zstream.next_out = _block; _zstream.next_out_index = 0; _zstream.avail_out = _block.Length; int err; if (_compress) { err = _zstream.deflate(flush); } else { err = _zstream.inflate(flush); } if (err != JZlib.Z_OK && (flush == JZlib.Z_FINISH && err != JZlib.Z_STREAM_END)) { throw new InvalidOperationException(string.Format("Error while {0}flating - {1}.", (_compress ? "de" : "in"), _zstream.msg)); } _output.Write(_block, 0, _block.Length - _zstream.avail_out); if (_zstream.avail_out == 0) { continue; } if (_zstream.avail_in == 0) { break; } } }
/// <summary> /// Reads data from the underlying stream, compressing or decompressing them during the process. /// </summary> /// <param name="buffer">An array of bytes. This method reads count bytes from the underlying stream and copies them into the buffer.</param> /// <param name="offset">The zero-based byte offset in buffer to which to copy bytes read from the current stream.</param> /// <param name="count">The maximum number of bytes to be read from the current stream.</param> /// <returns> /// The total number of bytes read into the buffer. This can be less than the number of bytes requested /// if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. /// </returns> public override int Read(byte[] buffer, int offset, int count) { if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset", offset, "Offset is outside the bounds of an array."); } //#elif !DOTNET11 // throw new ArgumentOutOfRangeException("offset", "Offset is outside the bounds of an array."); if (count < 0 || (offset + count) > buffer.Length) { throw new ArgumentException("Count is outside the bounds of an array.", "count"); } if (_outputEnded || count == 0) { return(0); } _zstream.next_out = buffer; _zstream.next_out_index = offset; _zstream.avail_out = count; while (_zstream.avail_out > 0) { if (_zstream.avail_in == 0 && !_inputEnded) { _zstream.next_in = _buffer; _zstream.next_in_index = 0; _zstream.avail_in = _input.Read(_buffer, 0, _buffer.Length); if (_zstream.avail_in == 0) { _inputEnded = true; } } int flush; if (_inputEnded) { flush = JZlib.Z_FINISH; } else { flush = JZlib.Z_NO_FLUSH; } int err; if (_compress) { err = _zstream.deflate(flush); } else { err = _zstream.inflate(flush); } switch (err) { case JZlib.Z_BUF_ERROR: if (_inputEnded) { break; } goto default; case JZlib.Z_OK: break; case JZlib.Z_STREAM_END: break; default: throw new InvalidOperationException(string.Format("Error while {0}flating - {1}.", (_compress ? "de" : "in"), _zstream.msg)); } if (err != JZlib.Z_OK) { break; } } count -= _zstream.avail_out; if (count == 0) { _outputEnded = true; } return(count); }