From 05cf0e051ffdd36eb3152cc8511581a49c24308a Mon Sep 17 00:00:00 2001 From: Rahil Shah Date: Mon, 25 Jul 2022 10:11:49 -0400 Subject: [PATCH] Fix java/util/zip/DeflateIn_InflateOut.skipBytes Test java/util/zip/DeflateIn_InflateOut.skipBytes verifies functionality of DeflaterInputStream.skipBytes. While testing skipping of bytes, test uses different input data with same data length, and compares the compressed sizes of these data to verify skipping bytes. As per zlib specification which is what is used by Deflater in java, length of compressed data is highly dependable on input data contents, so it will be incorrect to assume that compression ratio would be constant for same length input data. For more details please see explaination in https://github.com/eclipse-openj9/openj9/issues/14948#issuecomment-1194616794. Signed-off-by: Rahil Shah --- .../java/util/zip/DeflateIn_InflateOut.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/test/jdk/java/util/zip/DeflateIn_InflateOut.java b/test/jdk/java/util/zip/DeflateIn_InflateOut.java index dd69a7773eb..4bc1a98bd29 100644 --- a/test/jdk/java/util/zip/DeflateIn_InflateOut.java +++ b/test/jdk/java/util/zip/DeflateIn_InflateOut.java @@ -41,9 +41,7 @@ public class DeflateIn_InflateOut { private static ByteArrayOutputStream baos; private static InflaterOutputStream ios; - private static void reset() { - new Random(new Date().getTime()).nextBytes(data); - + private static void resetStreams() { bais = new ByteArrayInputStream(data); dis = new DeflaterInputStream(bais); @@ -51,11 +49,16 @@ private static void reset() { ios = new InflaterOutputStream(baos); } + private static void resetAll() { + new Random(new Date().getTime()).nextBytes(data); + resetStreams(); + } + /** Check byte arrays read/write. */ private static void ArrayReadWrite() throws Throwable { byte[] buf = new byte[512]; - reset(); + resetAll(); check(dis.available() == 1); for (;;) { int len = dis.read(buf, 0, buf.length); @@ -74,7 +77,7 @@ private static void ArrayReadWrite() throws Throwable { private static void ArrayReadByteWrite() throws Throwable { byte[] buf = new byte[512]; - reset(); + resetAll(); for (;;) { int len = dis.read(buf, 0, buf.length); if (len <= 0) { @@ -100,7 +103,7 @@ private static void ByteReadArrayWrite() throws Throwable { byte[] buf = new byte[8192]; int off = 0; - reset(); + resetAll(); int datum = dis.read(); while (datum != -1) { if (off == 8192) { @@ -126,7 +129,7 @@ private static void ByteReadByteWrite() throws Throwable { byte[] buf = new byte[512]; boolean reachEOF = false; - reset(); + resetAll(); while (dis.available() == 1) { int datum = dis.read(); if (datum == -1) { @@ -149,7 +152,7 @@ private static void SkipBytes() throws Throwable { int numReadable = 0; // Count number of bytes that are read - reset(); + resetAll(); check(dis.available() == 1); for (;;) { int count = dis.read(buf, 0, buf.length); @@ -162,7 +165,7 @@ private static void SkipBytes() throws Throwable { check(dis.available() == 0); // Verify that skipping the first several bytes works. - reset(); + resetStreams(); int numNotSkipped = 0; int numSkipBytes = 2053; // arbitrarily chosen prime check(dis.skip(numSkipBytes) == numSkipBytes); @@ -177,7 +180,7 @@ private static void SkipBytes() throws Throwable { check(numNotSkipped + numSkipBytes == numReadable); // Verify that skipping some bytes mid-stream works. - reset(); + resetStreams(); numNotSkipped = 0; numSkipBytes = 8887; // arbitrarily chosen prime for (int i = 0; ; i++) { @@ -195,7 +198,7 @@ private static void SkipBytes() throws Throwable { check(numNotSkipped + numSkipBytes == numReadable); // Verify that skipping the last N bytes works. - reset(); + resetStreams(); numNotSkipped = 0; numSkipBytes = 6449; // arbitrarily chosen prime for (int i = 0; ; i++) {