You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow passing in compressed size into DecompressionStream #50
I have some stream where it contains multiple zstd streams with other data in between, so I need to read the exact compressed buffer stream that I know the size of, and I seemingly can't use DecompressionStream because it will read data from stream up to its buffer size which may read past of my compressed size.
I can't really set stream buffer size to anything other than full compressed size, which would defeat the purpose of using the stream here.
My current non-stream code is this:
privatestaticvoidDecompressZSTD(ZstdSharp.DecompressorzstdDecompressor,BinaryReaderreader,Span<byte>output,intcompressedSize){varinputBuf=ArrayPool<byte>.Shared.Rent(compressedSize);try{varinput=inputBuf.AsSpan(0,compressedSize);reader.Read(input);if(!zstdDecompressor.TryUnwrap(input,output,outvarwritten)||output.Length!=written){thrownewInvalidDataException($"Failed to decompress ZSTD (expected {output.Length} bytes, got {written})");}}finally{ArrayPool<byte>.Shared.Return(inputBuf);}}
The issue is basically here, as it will read past my total compressed size to fill in the input buffer.
You can put this logic into the inner stream. Read can return less data than requested (buffer size). DecompressionStream will decompress all the data it has before reading the next part.
LimitedReadStream is something like this (written by ChatGPT)
usingSystem;usingSystem.IO;publicclassLimitedReadStream:Stream{privatereadonlyBinaryReader_baseReader;privatereadonlylong_bytesRemaining;privatelong_bytesRead;publicLimitedReadStream(BinaryReaderbaseReader,longlength){if(baseReader==null)thrownewArgumentNullException(nameof(baseReader));if(length<0)thrownewArgumentOutOfRangeException(nameof(length),"Length must be non-negative.");_baseReader=baseReader;_bytesRemaining=length;_bytesRead=0;}publicoverrideboolCanRead=>true;publicoverrideboolCanSeek=>false;publicoverrideboolCanWrite=>false;publicoverridelongLength=>_bytesRemaining;publicoverridelongPosition{get=>_bytesRead;set=>thrownewNotSupportedException("Setting the position is not supported.");}publicoverridevoidFlush(){// No-op since stream is read-only}publicoverrideintRead(byte[]buffer,intoffset,intcount){if(buffer==null)thrownewArgumentNullException(nameof(buffer));if(offset<0||count<0||(offset+count)>buffer.Length)thrownewArgumentOutOfRangeException();longbytesLeft=_bytesRemaining-_bytesRead;if(bytesLeft<=0)return0;inttoRead=(int)Math.Min(count,bytesLeft);intread=_baseReader.Read(buffer,offset,toRead);_bytesRead+=read;returnread;}publicoverridelongSeek(longoffset,SeekOriginorigin)=>thrownewNotSupportedException("Seeking is not supported.");publicoverridevoidSetLength(longvalue)=>thrownewNotSupportedException("Setting length is not supported.");publicoverridevoidWrite(byte[]buffer,intoffset,intcount)=>thrownewNotSupportedException("Writing is not supported.");}
Activity
oleg-st commentedon Apr 27, 2025
You can put this logic into the inner stream.
Readcan return less data than requested (buffer size).DecompressionStreamwill decompress all the data it has before reading the next part.oleg-st commentedon May 10, 2025
@xPaw
LimitedReadStreamis something like this (written byChatGPT)