Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Jan 9, 2025
1 parent 5b98866 commit 42baf97
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

}
36 changes: 36 additions & 0 deletions test/jdk/tools/jlink/runtimeImage/JimageDiffGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<ResourceDiff> 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);
Expand Down

0 comments on commit 42baf97

Please sign in to comment.