Skip to content

Commit cb9c20a

Browse files
author
duke
committed
Backport f696d9c521fa13969cb81381dc8586bcdccf67d9
1 parent 8c455fd commit cb9c20a

16 files changed

+74
-20
lines changed

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/JimageDiffGenerator.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package jdk.tools.jlink.internal.runtimelink;
2424

25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.UncheckedIOException;
2528
import java.util.ArrayList;
2629
import java.util.Arrays;
2730
import java.util.HashSet;
@@ -47,6 +50,7 @@ public class JimageDiffGenerator {
4750
public interface ImageResource extends AutoCloseable {
4851
public List<String> getEntries();
4952
public byte[] getResourceBytes(String name);
53+
public InputStream getResource(String name);
5054
}
5155

5256
/**
@@ -69,7 +73,6 @@ public List<ResourceDiff> generateDiff(ImageResource base, ImageResource image)
6973
resources.addAll(image.getEntries());
7074
baseResources = base.getEntries();
7175
for (String item: baseResources) {
72-
byte[] baseBytes = base.getResourceBytes(item);
7376
// First check that every item in the base image exist in
7477
// the optimized image as well. If it does not, it's a removed
7578
// item in the optimized image.
@@ -80,19 +83,18 @@ public List<ResourceDiff> generateDiff(ImageResource base, ImageResource image)
8083
ResourceDiff.Builder builder = new ResourceDiff.Builder();
8184
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.REMOVED)
8285
.setName(item)
83-
.setResourceBytes(baseBytes)
86+
.setResourceBytes(base.getResourceBytes(item))
8487
.build();
8588
diffs.add(diff);
8689
continue;
8790
}
8891
// Verify resource bytes are equal if present in both images
89-
boolean contentEquals = Arrays.equals(baseBytes, image.getResourceBytes(item));
90-
if (!contentEquals) {
92+
if (!compareStreams(base.getResource(item), image.getResource(item))) {
9193
// keep track of original bytes (non-optimized)
9294
ResourceDiff.Builder builder = new ResourceDiff.Builder();
9395
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.MODIFIED)
9496
.setName(item)
95-
.setResourceBytes(baseBytes)
97+
.setResourceBytes(base.getResourceBytes(item))
9698
.build();
9799
diffs.add(diff);
98100
}
@@ -110,4 +112,51 @@ public List<ResourceDiff> generateDiff(ImageResource base, ImageResource image)
110112
return diffs;
111113
}
112114

115+
/**
116+
* Compare the contents of the two input streams (byte-by-byte).
117+
*
118+
* @param is1 The first input stream
119+
* @param is2 The second input stream
120+
* @return {@code true} iff the two streams contain the same number of
121+
* bytes and each byte of the streams are equal. {@code false}
122+
* otherwise.
123+
*/
124+
private boolean compareStreams(InputStream is1, InputStream is2) {
125+
byte[] buf1 = new byte[1024];
126+
byte[] buf2 = new byte[1024];
127+
int bytesRead1, bytesRead2 = 0;
128+
try {
129+
try (is1; is2) {
130+
while ((bytesRead1 = is1.read(buf1)) != -1 &&
131+
(bytesRead2 = is2.read(buf2)) != -1) {
132+
if (bytesRead1 != bytesRead2) {
133+
return false;
134+
}
135+
if (bytesRead1 == buf1.length) {
136+
if (!Arrays.equals(buf1, buf2)) {
137+
return false;
138+
}
139+
} else {
140+
for (int i = 0; i < bytesRead1; i++) {
141+
if (buf1[i] != buf2[i]) {
142+
return false;
143+
}
144+
}
145+
}
146+
}
147+
// ensure we read both to the end
148+
if (bytesRead1 == -1) {
149+
bytesRead2 = is2.read(buf2);
150+
if (bytesRead2 != -1) {
151+
return false;
152+
}
153+
return true;
154+
}
155+
}
156+
} catch (IOException e) {
157+
throw new UncheckedIOException("IO exception when comparing bytes", e);
158+
}
159+
return false;
160+
}
161+
113162
}

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/runtimelink/ResourcePoolReader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package jdk.tools.jlink.internal.runtimelink;
2525

26+
import java.io.InputStream;
2627
import java.util.List;
2728
import java.util.Objects;
2829

@@ -54,4 +55,9 @@ public byte[] getResourceBytes(String name) {
5455
return pool.findEntry(name).orElseThrow().contentBytes();
5556
}
5657

58+
@Override
59+
public InputStream getResource(String name) {
60+
return pool.findEntry(name).orElseThrow().content();
61+
}
62+
5763
}

test/jdk/tools/jlink/runtimeImage/AddOptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* jdk.jlink/jdk.tools.jimage
4141
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4242
* jdk.test.lib.process.ProcessTools
43-
* @run main/othervm -Xmx1400m AddOptionsTest
43+
* @run main/othervm -Xmx1g AddOptionsTest
4444
*/
4545
public class AddOptionsTest extends AbstractLinkableRuntimeTest {
4646

test/jdk/tools/jlink/runtimeImage/BasicJlinkMissingJavaBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* jdk.jlink/jdk.tools.jimage
4242
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4343
* jdk.test.lib.process.ProcessTools
44-
* @run main/othervm -Xmx1400m BasicJlinkMissingJavaBase
44+
* @run main/othervm -Xmx1g BasicJlinkMissingJavaBase
4545
*/
4646
public class BasicJlinkMissingJavaBase extends AbstractLinkableRuntimeTest {
4747

test/jdk/tools/jlink/runtimeImage/BasicJlinkTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* jdk.jlink/jdk.tools.jimage
4040
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4141
* jdk.test.lib.process.ProcessTools
42-
* @run main/othervm -Xmx1400m BasicJlinkTest false
42+
* @run main/othervm -Xmx1g BasicJlinkTest false
4343
*/
4444
public class BasicJlinkTest extends AbstractLinkableRuntimeTest {
4545

test/jdk/tools/jlink/runtimeImage/CustomModuleJlinkTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* jdk.jlink/jdk.tools.jimage
4040
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4141
* jdk.test.lib.process.ProcessTools
42-
* @run main/othervm -Xmx1400m CustomModuleJlinkTest
42+
* @run main/othervm -Xmx1g CustomModuleJlinkTest
4343
*/
4444
public class CustomModuleJlinkTest extends AbstractLinkableRuntimeTest {
4545

test/jdk/tools/jlink/runtimeImage/GenerateJLIClassesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* jdk.jlink/jdk.tools.jimage
4040
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4141
* jdk.test.lib.process.ProcessTools
42-
* @run main/othervm -Xmx1400m GenerateJLIClassesTest
42+
* @run main/othervm -Xmx1g GenerateJLIClassesTest
4343
*/
4444
public class GenerateJLIClassesTest extends AbstractLinkableRuntimeTest {
4545

test/jdk/tools/jlink/runtimeImage/JavaSEReproducibleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* jdk.jlink/jdk.tools.jimage
4141
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4242
* jdk.test.lib.process.ProcessTools
43-
* @run main/othervm -Xmx1400m JavaSEReproducibleTest
43+
* @run main/othervm -Xmx1g JavaSEReproducibleTest
4444
*/
4545
public class JavaSEReproducibleTest extends AbstractLinkableRuntimeTest {
4646

test/jdk/tools/jlink/runtimeImage/KeepPackagedModulesFailTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* jdk.jlink/jdk.tools.jimage
4242
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4343
* jdk.test.lib.process.ProcessTools
44-
* @run main/othervm -Xmx1400m KeepPackagedModulesFailTest
44+
* @run main/othervm -Xmx1g KeepPackagedModulesFailTest
4545
*/
4646
public class KeepPackagedModulesFailTest extends AbstractLinkableRuntimeTest {
4747

test/jdk/tools/jlink/runtimeImage/ModifiedFilesExitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* jdk.jlink/jdk.tools.jimage
4141
* @build tests.* jdk.test.lib.process.OutputAnalyzer
4242
* jdk.test.lib.process.ProcessTools
43-
* @run main/othervm -Xmx1400m ModifiedFilesExitTest
43+
* @run main/othervm -Xmx1g ModifiedFilesExitTest
4444
*/
4545
public class ModifiedFilesExitTest extends ModifiedFilesTest {
4646

0 commit comments

Comments
 (0)