Skip to content

Allow passing in compressed size into DecompressionStream #50

Description

@xPaw

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:

private static void DecompressZSTD(ZstdSharp.Decompressor zstdDecompressor, BinaryReader reader, Span<byte> output, int compressedSize)
{
    var inputBuf = ArrayPool<byte>.Shared.Rent(compressedSize);

    try
    {
        var input = inputBuf.AsSpan(0, compressedSize);
        reader.Read(input);

        if (!zstdDecompressor.TryUnwrap(input, output, out var written) || output.Length != written)
        {
            throw new InvalidDataException($"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.

// Otherwise, read some more input
int bytesRead;
if ((bytesRead = innerStream.Read(inputBuffer, 0, inputBufferSize)) == 0)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions