From 42baf977ba31748e89a7bad1149270433e6953d9 Mon Sep 17 00:00:00 2001 From: Severin Gehwolf Date: Thu, 9 Jan 2025 19:16:45 +0100 Subject: [PATCH] Review feedback --- .../runtimelink/JimageDiffGenerator.java | 16 +++++---- .../runtimeImage/JimageDiffGeneratorTest.java | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/JimageDiffGenerator.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/JimageDiffGenerator.java index 4fe2527de9a75..9ec1fa55175af 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/JimageDiffGenerator.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/JimageDiffGenerator.java @@ -128,12 +128,11 @@ private boolean compareStreams(InputStream is1, InputStream is2) { byte[] buf2 = new byte[1024]; int bytesRead1, bytesRead2; try (is1; is2) { - while ((bytesRead1 = is1.readNBytes(buf1, 0, buf1.length)) != 0) { + while (true) { + bytesRead1 = is1.readNBytes(buf1, 0, buf1.length); bytesRead2 = is2.readNBytes(buf2, 0, buf2.length); - if (bytesRead2 == 0) { - // first input stream didn't return the end of stream - // but the second did? - return false; + if (bytesRead1 == 0 || bytesRead2 == 0) { + break; // no more bytes for a stream } if (bytesRead1 != bytesRead2) { return false; @@ -143,10 +142,15 @@ private boolean compareStreams(InputStream is1, InputStream is2) { return false; } } + // Streams are equal only if we've read them both to the end + if (bytesRead1 == 0 && + bytesRead2 == 0) { + return true; + } } catch (IOException e) { throw new UncheckedIOException("IO exception when comparing bytes", e); } - return true; + return false; } } diff --git a/test/jdk/tools/jlink/runtimeImage/JimageDiffGeneratorTest.java b/test/jdk/tools/jlink/runtimeImage/JimageDiffGeneratorTest.java index 95fc781c3a420..0176e6d07a19d 100644 --- a/test/jdk/tools/jlink/runtimeImage/JimageDiffGeneratorTest.java +++ b/test/jdk/tools/jlink/runtimeImage/JimageDiffGeneratorTest.java @@ -241,8 +241,44 @@ public void testNotEqualLength() throws Exception { assertEquals(result.get(0).getResourceBytes(), bytesBase[0]); } + /* + * Expect a difference since entry 'a' on the optimized version is + * one byte longer. + */ + @Test + public void testBytesDifferExactBufferSize() throws Exception { + List entriesBase = List.of("a", "b", "c", "d"); + byte[][] bytesBase = new byte[][] { + { }, /* a */ + { 0x08, 0x04, 0x04 }, /* b */ + { 0x09, 0x11, 0x11 }, /* c */ + { 0x11, 0x12, 0x31 }, /* d */ + }; + byte[][] bytesOpt = new byte[][] { + { }, /* a */ + { 0x08, 0x04, 0x04 }, /* b */ + { 0x09, 0x11, 0x11 }, /* c */ + { 0x11, 0x12, 0x31 }, /* d */ + }; + bytesBase[0] = genBytesOfSize(1024); // exact buffer size + bytesOpt[0] = genBytesOfSize(1024 + 1); // buffer size + 1 + + ImageResource base = new BasicImageResource(entriesBase, bytesBase); + ImageResource opt = new BasicImageResource(entriesBase, bytesOpt); + JimageDiffGenerator gen = new JimageDiffGenerator(); + List result = gen.generateDiff(base, opt); + assertEquals(result.size(), 1); + assertEquals(result.get(0).getKind(), ResourceDiff.Kind.MODIFIED); + assertEquals(result.get(0).getName(), "a"); + assertEquals(result.get(0).getResourceBytes(), bytesBase[0]); + } + private byte[] generateBytes() { int size = 1024 + 254; + return genBytesOfSize(size); + } + + private byte[] genBytesOfSize(int size) { byte[] result = new byte[size]; for (int i = 0; i < size; i++) { result[i] = (byte)(i % Byte.MAX_VALUE);