airlift / aircompressor

A port of Snappy, LZO, LZ4, and Zstandard to Java
Apache License 2.0
549 stars 110 forks source link

Decompression: How to determine the size of the output buffer? #178

Closed MartinHaeusler closed 9 months ago

MartinHaeusler commented 9 months ago

I'm trying out this library for snappy compression and decompression. Currently I'm using the Xerial Snappy library. This may be a very dumb question, but I can't seem to figure this out:

How do we know the required size of the output buffer?

public ByteArray decompress(ByteArray compressed){
    var decompressor = SnappyDecompressor();
    var outputBuffer = ByteBuffer.allocate(?????);
    decompressor.decompress(ByteBuffer.wrap(compressed), outputBuffer);
    return outputBuffer.array()
}

ByteBuffers by design cannot resize dynamically. And I don't know anything about the input array, except that it has been previously compressed by snappy. So... how should I size the output buffer?

MartinHaeusler commented 9 months ago

Ah, nevermind, found it in the tests! It's:

SnappyDecompressor.getUncompressedLength(bytes, 0)

Just got confused because for the compressor, maxCompressedLength(...) is an instance method, whereas getUncompressedLength(...) is a static method and the IDE didn't show it to me.