From 51cd741573330352fc7b7395b1c8aeca8a4bdcf5 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Tue, 14 Apr 2026 04:45:15 +0000 Subject: [PATCH] 8381670: Revert the changes to GZIPInputStream related to InputStream.available() usage Reviewed-by: lancea, iris --- .../java/util/zip/GZIPInputStream.java | 76 +++++---------- .../zip/GZIP/GZIPInputStreamAvailable.java | 93 ------------------- 2 files changed, 21 insertions(+), 148 deletions(-) delete mode 100644 test/jdk/java/util/zip/GZIP/GZIPInputStreamAvailable.java diff --git a/src/java.base/share/classes/java/util/zip/GZIPInputStream.java b/src/java.base/share/classes/java/util/zip/GZIPInputStream.java index ebcb9e3204c..72fb8036f08 100644 --- a/src/java.base/share/classes/java/util/zip/GZIPInputStream.java +++ b/src/java.base/share/classes/java/util/zip/GZIPInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2026, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -79,11 +79,7 @@ public class GZIPInputStream extends InflaterInputStream { super(in, createInflater(in, size), size); usesDefaultInflater = true; try { - // we don't expect the stream to be at EOF - // and if it is, then we want readHeader to - // raise an exception, so we pass "true" for - // the "failOnEOF" param. - readHeader(in, true); + readHeader(in); } catch (IOException ioe) { this.inf.end(); throw ioe; @@ -194,40 +190,12 @@ public class GZIPInputStream extends InflaterInputStream { /* * Reads GZIP member header and returns the total byte number * of this member header. - * If failOnEOF is false and if the given InputStream has already - * reached EOF when this method was invoked, then this method returns - * -1 (indicating that there's no GZIP member header). - * In all other cases of malformed header or EOF being detected - * when reading the header, this method will throw an IOException. */ - private int readHeader(InputStream this_in, boolean failOnEOF) throws IOException { + private int readHeader(InputStream this_in) throws IOException { CheckedInputStream in = new CheckedInputStream(this_in, crc); crc.reset(); - - int magic; - if (!failOnEOF) { - // read an unsigned short value representing the GZIP magic header. - // this is the same as calling readUShort(in), except that here, - // when reading the first byte, we don't raise an EOFException - // if the stream has already reached EOF. - - // read unsigned byte - int b = in.read(); - if (b == -1) { // EOF - crc.reset(); - return -1; // represents no header bytes available - } - checkUnexpectedByte(b); - // read the next unsigned byte to form the unsigned - // short. we throw the usual EOFException/ZipException - // from this point on if there is no more data or - // the data doesn't represent a header. - magic = (readUByte(in) << 8) | b; - } else { - magic = readUShort(in); - } // Check header magic - if (magic != GZIP_MAGIC) { + if (readUShort(in) != GZIP_MAGIC) { throw new ZipException("Not in GZIP format"); } // Check compression method @@ -290,21 +258,23 @@ public class GZIPInputStream extends InflaterInputStream { (readUInt(in) != (inf.getBytesWritten() & 0xffffffffL))) throw new ZipException("Corrupt GZIP trailer"); + // If there are more bytes available in "in" or + // the leftover in the "inf" is > 26 bytes: + // this.trailer(8) + next.header.min(10) + next.trailer(8) // try concatenated case - int m = 8; // this.trailer - try { - int numNextHeaderBytes = readHeader(in, false); // next.header (if available) - if (numNextHeaderBytes == -1) { - return true; // end of stream reached + if (this.in.available() > 0 || n > 26) { + int m = 8; // this.trailer + try { + m += readHeader(in); // next.header + } catch (IOException ze) { + return true; // ignore any malformed, do nothing } - m += numNextHeaderBytes; - } catch (IOException ze) { - return true; // ignore any malformed, do nothing + inf.reset(); + if (n > m) + inf.setInput(buf, len - n + m, n - m); + return false; } - inf.reset(); - if (n > m) - inf.setInput(buf, len - n + m, n - m); - return false; + return true; } /* @@ -331,16 +301,12 @@ public class GZIPInputStream extends InflaterInputStream { if (b == -1) { throw new EOFException(); } - checkUnexpectedByte(b); - return b; - } - - private void checkUnexpectedByte(final int b) throws IOException { if (b < -1 || b > 255) { - // report the InputStream type which returned this unexpected byte + // Report on this.in, not argument in; see read{Header, Trailer}. throw new IOException(this.in.getClass().getName() - + ".read() returned value out of range -1..255: " + b); + + ".read() returned value out of range -1..255: " + b); } + return b; } private byte[] tmpbuf = new byte[128]; diff --git a/test/jdk/java/util/zip/GZIP/GZIPInputStreamAvailable.java b/test/jdk/java/util/zip/GZIP/GZIPInputStreamAvailable.java deleted file mode 100644 index f63015e0930..00000000000 --- a/test/jdk/java/util/zip/GZIP/GZIPInputStreamAvailable.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* @test - * @bug 7036144 - * @summary Test concatenated gz streams when available() returns zero - * @run junit GZIPInputStreamAvailable - */ - -import org.junit.jupiter.api.Test; - -import java.io.*; -import java.util.*; -import java.util.zip.*; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -public class GZIPInputStreamAvailable { - - public static final int NUM_COPIES = 100; - - @Test - public void testZeroAvailable() throws IOException { - - // Create some uncompressed data and then repeat it NUM_COPIES times - byte[] uncompressed1 = "this is a test".getBytes("ASCII"); - byte[] uncompressedN = repeat(uncompressed1, NUM_COPIES); - - // Compress the original data and then repeat that NUM_COPIES times - byte[] compressed1 = deflate(uncompressed1); - byte[] compressedN = repeat(compressed1, NUM_COPIES); - - // (a) Read back inflated data from a stream where available() is accurate and verify - byte[] readback1 = inflate(new ByteArrayInputStream(compressedN)); - assertArrayEquals(uncompressedN, readback1); - - // (b) Read back inflated data from a stream where available() always returns zero and verify - byte[] readback2 = inflate(new ZeroAvailableStream(new ByteArrayInputStream(compressedN))); - assertArrayEquals(uncompressedN, readback2); - } - - public static byte[] repeat(byte[] data, int count) { - byte[] repeat = new byte[data.length * count]; - int off = 0; - for (int i = 0; i < count; i++) { - System.arraycopy(data, 0, repeat, off, data.length); - off += data.length; - } - return repeat; - } - - public static byte[] deflate(byte[] data) throws IOException { - ByteArrayOutputStream buf = new ByteArrayOutputStream(); - try (GZIPOutputStream out = new GZIPOutputStream(buf)) { - out.write(data); - } - return buf.toByteArray(); - } - - public static byte[] inflate(InputStream in) throws IOException { - return new GZIPInputStream(in).readAllBytes(); - } - - public static class ZeroAvailableStream extends FilterInputStream { - public ZeroAvailableStream(InputStream in) { - super(in); - } - @Override - public int available() { - return 0; - } - } -}