/// <summary>Reads the next chunk from stream.</summary> /// <returns><c>true</c> if next has been read, or <c>false</c> if it is legitimate end of file. /// Throws <see cref="EndOfStreamException"/> if end of stream was unexpected.</returns> private bool AcquireNextChunk() { do { ulong varint; if (!TryReadVarInt(out varint)) { return(false); } var flags = (ChunkFlags)varint; var isCompressed = (flags & ChunkFlags.Compressed) != 0; var originalLength = (int)ReadVarInt(); var compressedLength = isCompressed ? (int)ReadVarInt() : originalLength; if (compressedLength > originalLength) { throw EndOfStream(); // corrupted } var compressed = new byte[compressedLength]; var chunk = ReadBlock(compressed, 0, compressedLength); if (chunk != compressedLength) { throw EndOfStream(); // corrupted } if (!isCompressed) { _buffer = compressed; // no compression on this chunk _bufferLength = compressedLength; } else { if (_buffer == null || _buffer.Length < originalLength) { _buffer = new byte[originalLength]; } var passes = (int)flags >> 2; if (passes != 0) { throw new NotSupportedException("Chunks with multiple passes are not supported."); } LZ4Codec.Decode(compressed, 0, compressedLength, _buffer, 0, originalLength, true); _bufferLength = originalLength; } _bufferOffset = 0; } while (_bufferLength == 0); // skip empty block (shouldn't happen but...) return(true); }
/// <summary>Flushes current chunk.</summary> private void FlushCurrentChunk() { if (_bufferOffset <= 0) { return; } var compressed = new byte[_bufferOffset]; var compressedLength = _highCompression ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset) : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset); if (compressedLength <= 0 || compressedLength >= _bufferOffset) { // incompressible block compressed = _buffer; compressedLength = _bufferOffset; } var isCompressed = compressedLength < _bufferOffset; var flags = ChunkFlags.None; if (isCompressed) { flags |= ChunkFlags.Compressed; } if (_highCompression) { flags |= ChunkFlags.HighCompression; } WriteVarInt((ulong)flags); WriteVarInt((ulong)_bufferOffset); if (isCompressed) { WriteVarInt((ulong)compressedLength); } _innerStream.Write(compressed, 0, compressedLength); _bufferOffset = 0; }