public override int Read(byte[] array, int offset, int count) { EnsureDecompressionMode(); ValidateParameters(array, offset, count); EnsureNotDisposed(); int bytesRead; int currentOffset = offset; int remainingCount = count; while (true) { bytesRead = _inflater.Inflate(array, currentOffset, remainingCount); currentOffset += bytesRead; remainingCount -= bytesRead; if (remainingCount == 0) { break; } if (_inflater.Finished()) { // if we finished decompressing, we can't have anything left in the outputwindow. Debug.Assert(_inflater.AvailableOutput == 0, "We should have copied all stuff out!"); break; } Debug.Assert(_inflater.NeedsInput(), "We can only run into this case if we are short of input"); int bytes = _stream.Read(_buffer, 0, _buffer.Length); if (bytes <= 0) { break; } else if (bytes > _buffer.Length) { // The stream is either malicious or poorly implemented and returned a number of // bytes larger than the buffer supplied to it. throw new InvalidDataException(SR.GenericInvalidData); } _inflater.SetInput(_buffer, 0, bytes); } return(count - remainingCount); }
internal int ReadCore(Span <byte> buffer) { EnsureDecompressionMode(); EnsureNotDisposed(); EnsureBufferInitialized(); int totalRead = 0; while (true) { int bytesRead = _inflater.Inflate(buffer.Slice(totalRead)); totalRead += bytesRead; if (totalRead == buffer.Length) { break; } // If the stream is finished then we have a few potential cases here: // 1. DeflateStream => return // 2. GZipStream that is finished but may have an additional GZipStream appended => feed more input // 3. GZipStream that is finished and appended with garbage => return if (_inflater.Finished() && (!_inflater.IsGzipStream() || !_inflater.NeedsInput())) { break; } if (_inflater.NeedsInput()) { int bytes = _stream.Read(_buffer, 0, _buffer.Length); if (bytes <= 0) { break; } else if (bytes > _buffer.Length) { // The stream is either malicious or poorly implemented and returned a number of // bytes larger than the buffer supplied to it. throw new InvalidDataException(SR.GenericInvalidData); } _inflater.SetInput(_buffer, 0, bytes); } } return(totalRead); }
public override int Read(byte[] array, int offset, int count) { EnsureDecompressionMode(); ValidateParameters(array, offset, count); EnsureNotDisposed(); int bytesRead; int currentOffset = offset; int remainingCount = count; while (true) { bytesRead = _inflater.Inflate(array, currentOffset, remainingCount); currentOffset += bytesRead; remainingCount -= bytesRead; if (remainingCount == 0) { break; } if (_inflater.Finished()) { // if we finished decompressing, we can't have anything left in the outputwindow. Debug.Assert(_inflater.AvailableOutput == 0, "We should have copied all stuff out!"); break; } Debug.Assert(_inflater.NeedsInput(), "We can only run into this case if we are short of input"); int bytes = _stream.Read(_buffer, 0, _buffer.Length); if (bytes == 0) { break; //Do we want to throw an exception here? } _inflater.SetInput(_buffer, 0, bytes); } return(count - remainingCount); }