lz4 / lz4-java

LZ4 compression for Java
Apache License 2.0
1.11k stars 252 forks source link

How should I initial the length for dest byte[] when i don't know the decompressed length #88

Closed zhangjiajie023 closed 6 years ago

zhangjiajie023 commented 8 years ago

byte[] restored = new byte[decompressedLength]; // - method 2: when the compressed length is known (a little slower) // the destination buffer needs to be over-sized LZ4SafeDecompressor decompressor2 = factory.safeDecompressor(); int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);

this example has initial a correct length for restored to decompress.

But when i don't know the decompressed length, how should I initial the length fordest byte[]?

I tried to byte[] restored = new byte[1]; failed, definitely; I used the decompress(ByteBuffer src, ByteBuffer dest), initial the capacity of dest to be 1, failed too;

so the dest must be set a longer length than it should be, right? could u tell me how to use the safeDecompress(..)? tks for your answer

zhangjiajie023 commented 8 years ago

Someone suggested that set the dest.length to be src.length * 3 or more, is it suitable, or any other better solution ?

odaira commented 7 years ago

There is no easy way to guess the decompressed length in lz4. The maximum possible compression ratio is 255, but it is too large for the decompressed buffer allocation.

darouwan commented 7 years ago

I meet the same problem. LZ4BlockInputStream may solve your problem as stream can be read in multi times.

odaira commented 6 years ago

You can also use LZ4FrameInputStream (1.4.0 or later) or LZ4DecompressorWithLength (1.5.0 or later). Please reopen this issue if you need further assistance.