|
41 | 41 | import java.nio.file.Path; |
42 | 42 | import java.nio.file.Paths; |
43 | 43 | import java.nio.file.StandardOpenOption; |
| 44 | +import java.nio.file.attribute.BasicFileAttributes; |
44 | 45 | import java.nio.file.attribute.PosixFilePermission; |
45 | 46 | import java.util.Collections; |
46 | 47 | import java.util.HashMap; |
|
51 | 52 | import java.util.Optional; |
52 | 53 | import java.util.Properties; |
53 | 54 | import java.util.Set; |
| 55 | +import java.util.function.BiPredicate; |
| 56 | +import java.util.function.Consumer; |
| 57 | +import java.util.stream.Stream; |
54 | 58 |
|
55 | 59 | import jdk.tools.jlink.internal.BasicImageWriter; |
56 | 60 | import jdk.tools.jlink.internal.ExecutableImage; |
@@ -202,26 +206,26 @@ public void storeFiles(ResourcePool files) { |
202 | 206 | // launchers in the bin directory need execute permission. |
203 | 207 | // On Windows, "bin" also subdirectories containing jvm.dll. |
204 | 208 | if (Files.isDirectory(bin)) { |
205 | | - Files.find(bin, 2, (path, attrs) -> { |
206 | | - return attrs.isRegularFile() && !path.toString().endsWith(".diz"); |
207 | | - }).forEach(this::setExecutable); |
| 209 | + forEachPath(bin, |
| 210 | + (path, attrs) -> attrs.isRegularFile() && !path.toString().endsWith(".diz"), |
| 211 | + this::setExecutable); |
208 | 212 | } |
209 | 213 |
|
210 | 214 | // jspawnhelper is in lib or lib/<arch> |
211 | 215 | Path lib = root.resolve(LIB_DIRNAME); |
212 | 216 | if (Files.isDirectory(lib)) { |
213 | | - Files.find(lib, 2, (path, attrs) -> { |
214 | | - return path.getFileName().toString().equals("jspawnhelper") |
215 | | - || path.getFileName().toString().equals("jexec"); |
216 | | - }).forEach(this::setExecutable); |
| 217 | + forEachPath(lib, |
| 218 | + (path, attrs) -> path.getFileName().toString().equals("jspawnhelper") |
| 219 | + || path.getFileName().toString().equals("jexec"), |
| 220 | + this::setExecutable); |
217 | 221 | } |
218 | 222 |
|
219 | 223 | // read-only legal notices/license files |
220 | 224 | Path legal = root.resolve(LEGAL_DIRNAME); |
221 | 225 | if (Files.isDirectory(legal)) { |
222 | | - Files.find(legal, 2, (path, attrs) -> { |
223 | | - return attrs.isRegularFile(); |
224 | | - }).forEach(this::setReadOnly); |
| 226 | + forEachPath(legal, |
| 227 | + (path, attrs) -> attrs.isRegularFile(), |
| 228 | + this::setReadOnly); |
225 | 229 | } |
226 | 230 | } |
227 | 231 |
|
@@ -528,34 +532,34 @@ public ExecutableImage getExecutableImage() { |
528 | 532 | private static void patchScripts(ExecutableImage img, List<String> args) throws IOException { |
529 | 533 | Objects.requireNonNull(args); |
530 | 534 | if (!args.isEmpty()) { |
531 | | - Files.find(img.getHome().resolve(BIN_DIRNAME), 2, (path, attrs) -> { |
532 | | - return img.getModules().contains(path.getFileName().toString()); |
533 | | - }).forEach((p) -> { |
534 | | - try { |
535 | | - String pattern = "JLINK_VM_OPTIONS="; |
536 | | - byte[] content = Files.readAllBytes(p); |
537 | | - String str = new String(content, StandardCharsets.UTF_8); |
538 | | - int index = str.indexOf(pattern); |
539 | | - StringBuilder builder = new StringBuilder(); |
540 | | - if (index != -1) { |
541 | | - builder.append(str.substring(0, index)). |
542 | | - append(pattern); |
543 | | - for (String s : args) { |
544 | | - builder.append(s).append(" "); |
545 | | - } |
546 | | - String remain = str.substring(index + pattern.length()); |
547 | | - builder.append(remain); |
548 | | - str = builder.toString(); |
549 | | - try (BufferedWriter writer = Files.newBufferedWriter(p, |
550 | | - StandardCharsets.ISO_8859_1, |
551 | | - StandardOpenOption.WRITE)) { |
552 | | - writer.write(str); |
| 535 | + forEachPath(img.getHome().resolve(BIN_DIRNAME), |
| 536 | + (path, attrs) -> img.getModules().contains(path.getFileName().toString()), |
| 537 | + p -> { |
| 538 | + try { |
| 539 | + String pattern = "JLINK_VM_OPTIONS="; |
| 540 | + byte[] content = Files.readAllBytes(p); |
| 541 | + String str = new String(content, StandardCharsets.UTF_8); |
| 542 | + int index = str.indexOf(pattern); |
| 543 | + StringBuilder builder = new StringBuilder(); |
| 544 | + if (index != -1) { |
| 545 | + builder.append(str.substring(0, index)). |
| 546 | + append(pattern); |
| 547 | + for (String s : args) { |
| 548 | + builder.append(s).append(" "); |
| 549 | + } |
| 550 | + String remain = str.substring(index + pattern.length()); |
| 551 | + builder.append(remain); |
| 552 | + str = builder.toString(); |
| 553 | + try (BufferedWriter writer = Files.newBufferedWriter(p, |
| 554 | + StandardCharsets.ISO_8859_1, |
| 555 | + StandardOpenOption.WRITE)) { |
| 556 | + writer.write(str); |
| 557 | + } |
553 | 558 | } |
| 559 | + } catch (IOException ex) { |
| 560 | + throw new RuntimeException(ex); |
554 | 561 | } |
555 | | - } catch (IOException ex) { |
556 | | - throw new RuntimeException(ex); |
557 | | - } |
558 | | - }); |
| 562 | + }); |
559 | 563 | } |
560 | 564 | } |
561 | 565 |
|
@@ -592,4 +596,11 @@ private static Set<String> retrieveModules(Path root) { |
592 | 596 | } |
593 | 597 | return modules; |
594 | 598 | } |
| 599 | + |
| 600 | + // finds subpaths matching the given criteria (up to 2 levels deep) and applies the given lambda |
| 601 | + private static void forEachPath(Path dir, BiPredicate<Path, BasicFileAttributes> matcher, Consumer<Path> consumer) throws IOException { |
| 602 | + try (Stream<Path> stream = Files.find(dir, 2, matcher)) { |
| 603 | + stream.forEach(consumer); |
| 604 | + } |
| 605 | + } |
595 | 606 | } |
0 commit comments