hyperxpro / Brotli4j

Brotli4j provides Brotli compression and decompression for Java.
Apache License 2.0
107 stars 35 forks source link

Unable to compress correctly if input data does not start from 0. #126

Closed deshanxiao closed 10 months ago

deshanxiao commented 10 months ago

Describe the bug Unable to compress correctly if input data does not start from 0.

To Reproduce Call Encoder.compress(byte[] x, int offset, int length) will compress all bytes from x (offset > 0)

https://github.com/hyperxpro/Brotli4j/blob/0907a385873def839cb0b88132f62238255bc0d6/brotli4j/src/main/java/com/aayushatharva/brotli4j/encoder/Encoder.java#L73

data.length -> length ?

Expected behavior It should only compress the bytes from start to start + len.

Platform (please complete the following information): ubuntu

Additional context

hyperxpro commented 10 months ago

What is the error? Are you getting corrupted data?

hyperxpro commented 10 months ago

Technically, you should compress all data and then finish the encoding with length 0.

deshanxiao commented 10 months ago

I got no error but the output array looks unexpected in length. Here is a very simple example: @hyperxpro

public class Main2 {
    static {
        Brotli4jLoader.ensureAvailability();
    }

    public static void main(String[] args) throws IOException {
        byte[] totalBytes = "abcdefg".getBytes();
        // compress "defg"
        byte[] compressedBytes = Encoder.compress(totalBytes, 3, 4);
        // len(output) == 7 not 4
        byte[] output = Decoder.decompress(compressedBytes, 0, compressedBytes.length);
    }
}
hyperxpro commented 10 months ago

Okay, looks like a bug.

Would you like to raise a PR at https://github.com/google/brotli?

hyperxpro commented 10 months ago

PTAL

https://github.com/google/brotli/pull/1104