BrotliInputStream.java issue when using both read() followed by read(byte b[]) #461

Closed
opened 2026-01-29 20:44:16 +00:00 by claunia · 5 comments
Owner

Originally created by @dannygonzalez on GitHub (Jun 7, 2023).

We are currently using maven to pull in the brotli library into our Java application. The latest version seems to be 0.1.2.
I'm not sure how this is so far behind the released version but nevertheless the code for BrotliInputStream.java doesn't seem to have changed.

There is an issue with BrotliInputStream where if you do one or more read() calls followed by read(byte b[]), the read(byte b[]) call will incorrectly return -1 even though there was more data succesfully read in to the buffer.

It should return the number of bytes read into the buffer, not -1.

void tesBrotliInputStream() throws IOException
{
    // Brotli compressed string 'Hello World'
    final byte[] brotliCompressedBytes = {0x21, 0x28, 0x00, 0x04, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x03};
    InputStream is = new ByteArrayInputStream(brotliCompressedBytes);

    is = new BrotliInputStream(is);
    
    // problem is due to this read()
    is.read();

    byte[] buffer = new byte[32];
    int numRead = is.read(buffer);
    
    // numRead should be 10 but is -1 here due to the preceding is.read()
    System.out.println("numRead should be 10 but is " + numRead);
    System.out.println("Uncompressed: " + new String(buffer));
    
    is.close();
}

Should BrotliInputStream.java line 121 be:
remainingBufferBytes = read(buffer, 0, 1);

instead of:
remainingBufferBytes = read(buffer, 0, buffer.length);

Originally created by @dannygonzalez on GitHub (Jun 7, 2023). We are currently using maven to pull in the brotli library into our Java application. The latest version seems to be [0.1.2](https://mvnrepository.com/artifact/org.brotli/parent/0.1.2). I'm not sure how this is so far behind the released version but nevertheless the code for BrotliInputStream.java doesn't seem to have changed. There is an issue with BrotliInputStream where if you do one or more `read()` calls followed by `read(byte b[])`, the `read(byte b[])` call will incorrectly return -1 even though there was more data succesfully read in to the buffer. It should return the number of bytes read into the buffer, not -1. ``` void tesBrotliInputStream() throws IOException { // Brotli compressed string 'Hello World' final byte[] brotliCompressedBytes = {0x21, 0x28, 0x00, 0x04, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x03}; InputStream is = new ByteArrayInputStream(brotliCompressedBytes); is = new BrotliInputStream(is); // problem is due to this read() is.read(); byte[] buffer = new byte[32]; int numRead = is.read(buffer); // numRead should be 10 but is -1 here due to the preceding is.read() System.out.println("numRead should be 10 but is " + numRead); System.out.println("Uncompressed: " + new String(buffer)); is.close(); } ``` Should BrotliInputStream.java line 121 be: `remainingBufferBytes = read(buffer, 0, 1);` instead of: `remainingBufferBytes = read(buffer, 0, buffer.length);`
claunia added the release-v1.1 label 2026-01-29 20:44:16 +00:00
Author
Owner

@eustas commented on GitHub (Jun 7, 2023):

Will take a look soon

@eustas commented on GitHub (Jun 7, 2023): Will take a look soon
Author
Owner

@eustas commented on GitHub (Jun 7, 2023):

Executed test on fresh build. Result:

numRead should be 10 but is 10
Uncompressed: ello world

Perhaps we just need to publish a fresh version. Likely 1.0 =)

@eustas commented on GitHub (Jun 7, 2023): Executed test on fresh build. Result: ``` numRead should be 10 but is 10 Uncompressed: ello world ``` Perhaps we just need to publish a fresh version. Likely `1.0` =)
Author
Owner

@dannygonzalez commented on GitHub (Jun 7, 2023):

Thanks for testing @eustas
Would it be possible to publish the latest version?

@dannygonzalez commented on GitHub (Jun 7, 2023): Thanks for testing @eustas Would it be possible to publish the latest version?
Author
Owner

@eustas commented on GitHub (Jun 7, 2023):

Sure!

@eustas commented on GitHub (Jun 7, 2023): Sure!
Author
Owner

@eustas commented on GitHub (Aug 3, 2023):

Moved to #1056

@eustas commented on GitHub (Aug 3, 2023): Moved to #1056
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#461