lz4 / lz4-java

LZ4 compression for Java
Apache License 2.0
1.1k stars 253 forks source link

Fix LZ4SafeDecompressor javadoc #168

Closed wsargent closed 3 years ago

odaira commented 3 years ago

LZ4SafeDecompressor does require the size of the compressed data (i.e. the size after compression) to be known, doesn't it?

wsargent commented 3 years ago

I'm obviously confused, but I don't understand how decompression in LZ4 works -- the decompression byte array needs to be a certain size, but how do you know how large it should be if you don't have access to the decompressed size itself?

odaira commented 3 years ago

There is no simple way to know how large the decompressed data will be. The maximum possible inflation rate of LZ4 is 256x, but it would be too much to prepare a buffer of 256x the size of the compressed data in most cases. LZ4FastDecompressor and LZ4SafeDecompressor are low-level APIs. They just expose the original C APIs of the LZ4 library. You need to get the original size by storing it somewhere else. These APIs are designed so for maximum flexibility.

Most people would use LZ4FrameOutputStream/LZ4FrameInputStream if they do not want to manage the original size themselves, especially when they exchange LZ4-compressed data through files or network. If you really prefer block-oriented API to stream-oriented one, LZ4CompressorWithLength/LZ4DecompressorWithLength are provided, but please note that they are not compatible with the LZ4 block format or LZ4 frame format.