Change in GZIPOutputStream behavior in Java 17?

345 views Asked by At

Was there an undocumented change to GZIP behavior in Java 17? I've looked at the Java update notes and they make no mention of compression to gzip changes. I've created a simple program to demonstrate the change:

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
GZIPOutputStream compressorStream = new GZIPOutputStream(byteStream);
IOUtils.write("test1 test2 test3".getBytes(StandardCharsets.UTF_8), compressorStream);
compressorStream.finish();
for (byte b : byteStream.toByteArray()) {
    System.out.print(String.format("%02x ", b));
}

When I run this on Java 8 vs Java 17, it produces different results:

 Java 8: 1f 8b 08 00 00 00 00 00 00 ff 2b 49 2d 2e 31 54 28 01 92 46 60 d2 18 00 e8 5e b8 b9 11 00 00 00
Java 17: 1f 8b 08 00 00 00 00 00 00 ff 2b 49 2d 2e 31 54 28 49 2d 2e 31 02 93 c6 00 e8 5e b8 b9 11 00 00 00 

As you can see, not only are the byte values different, the number of bytes in the resulting array is even different!

Does anyone know why this is happening? Was there any documentation from the Java developers explaining this change in behavior?

(Java 8 version: 1.8.0_333, Java 17 version: 17.0.8)

2

There are 2 answers

0
Mark Adler On

There is no problem. Both streams are correct, and both decompress to "test1 test2 test3".

1
Shashi On

In gzip compression, a 10-byte header is used, and the 10th byte specifies information about the operating system. Starting from Java 16 and onwards, this 10th byte has been modified to the value 255, whereas it was previously set to 0 before Java 16. This alteration in the 10th byte accounts for the variation observed in the compressed output. reference: https://bugs.openjdk.org/browse/JDK-8244706