diff --git a/test/jdk/build/AbsPathsInImage.java b/test/jdk/build/AbsPathsInImage.java index 1aa7e59941e8e..54170d780ec10 100644 --- a/test/jdk/build/AbsPathsInImage.java +++ b/test/jdk/build/AbsPathsInImage.java @@ -42,7 +42,7 @@ * @requires !vm.debug * @comment ASAN keeps the 'unwanted' paths in the binaries because of its build options * @requires !vm.asan - * @run main/othervm -Xmx900m AbsPathsInImage + * @run main AbsPathsInImage */ public class AbsPathsInImage { @@ -51,6 +51,7 @@ public class AbsPathsInImage { public static final String DIR_PROPERTY = "jdk.test.build.AbsPathsInImage.dir"; private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("windows"); private static final boolean IS_LINUX = System.getProperty("os.name").toLowerCase().contains("linux"); + private static final int DEFAULT_BUFFER_SIZE = 8192; private boolean matchFound = false; @@ -180,7 +181,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO } private void scanFile(Path file, List searchPatterns) throws IOException { - List matches = scanBytes(Files.readAllBytes(file), searchPatterns); + int bytesRead; + byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; + List matches = new ArrayList<>(); + try (InputStream inputStream = Files.newInputStream(file)) { + while ((bytesRead = inputStream.read(buffer)) != -1) { + matches.addAll(scanBytes(buffer, bytesRead, searchPatterns)); + } + } if (matches.size() > 0) { matchFound = true; System.out.println(file + ":"); @@ -192,10 +200,15 @@ private void scanFile(Path file, List searchPatterns) throws IOException } private void scanZipFile(Path zipFile, List searchPatterns) throws IOException { + ZipEntry zipEntry; + int bytesRead; + byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFile))) { - ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { - List matches = scanBytes(zipInputStream.readAllBytes(), searchPatterns); + List matches = new ArrayList<>(); + while ((bytesRead = zipInputStream.read(buffer)) != -1) { + matches.addAll(scanBytes(buffer, bytesRead, searchPatterns)); + } if (matches.size() > 0) { matchFound = true; System.out.println(zipFile + ", " + zipEntry.getName() + ":"); @@ -208,19 +221,19 @@ private void scanZipFile(Path zipFile, List searchPatterns) throws IOExc } } - private List scanBytes(byte[] data, List searchPatterns) { + private List scanBytes(byte[] data, int dataLength, List searchPatterns) { List matches = new ArrayList<>(); - for (int i = 0; i < data.length; i++) { + for (int i = 0; i < dataLength; i++) { for (byte[] searchPattern : searchPatterns) { boolean found = true; for (int j = 0; j < searchPattern.length; j++) { - if ((i + j >= data.length || data[i + j] != searchPattern[j])) { + if ((i + j >= dataLength || data[i + j] != searchPattern[j])) { found = false; break; } } if (found) { - matches.add(new String(data, charsStart(data, i), charsOffset(data, i, searchPattern.length))); + matches.add(new String(data, charsStart(data, i), charsOffset(data, dataLength, i, searchPattern.length))); // No need to search the same string for multiple patterns break; } @@ -240,9 +253,9 @@ private int charsStart(byte[] data, int startIndex) { return index + 1; } - private int charsOffset(byte[] data, int startIndex, int startOffset) { + private int charsOffset(byte[] data, int dataLength, int startIndex, int startOffset) { int offset = startOffset; - while (startIndex + ++offset < data.length) { + while (startIndex + ++offset < dataLength) { byte datum = data[startIndex + offset]; if (datum < 32 || datum > 126) { break;