|
32 | 32 | import java.io.OutputStream; |
33 | 33 | import java.io.PrintStream; |
34 | 34 | import java.io.StringReader; |
| 35 | +import java.io.UncheckedIOException; |
35 | 36 | import java.io.Writer; |
36 | 37 | import java.nio.file.Path; |
37 | 38 | import java.util.ArrayList; |
|
42 | 43 | import java.util.Objects; |
43 | 44 | import java.util.Optional; |
44 | 45 | import java.util.Set; |
| 46 | +import java.util.concurrent.CompletableFuture; |
| 47 | +import java.util.concurrent.CompletionException; |
45 | 48 | import java.util.function.Supplier; |
46 | 49 | import java.util.regex.Pattern; |
47 | 50 | import java.util.spi.ToolProvider; |
48 | 51 | import java.util.stream.Collectors; |
49 | 52 | import java.util.stream.Stream; |
50 | 53 | import jdk.jpackage.internal.util.function.ThrowingSupplier; |
| 54 | +import jdk.jpackage.internal.util.function.ExceptionBox; |
51 | 55 |
|
52 | 56 | public final class Executor extends CommandArguments<Executor> { |
53 | 57 |
|
@@ -465,9 +469,37 @@ private Result runExecutable() throws IOException, InterruptedException { |
465 | 469 | trace("Execute " + sb.toString() + "..."); |
466 | 470 | Process process = builder.start(); |
467 | 471 |
|
468 | | - final var output = combine( |
469 | | - processProcessStream(outputStreamsControl.stdout(), process.getInputStream()), |
470 | | - processProcessStream(outputStreamsControl.stderr(), process.getErrorStream())); |
| 472 | + var stdoutGobbler = CompletableFuture.<Optional<List<String>>>supplyAsync(() -> { |
| 473 | + try { |
| 474 | + return processProcessStream(outputStreamsControl.stdout(), process.getInputStream()); |
| 475 | + } catch (IOException ex) { |
| 476 | + throw new UncheckedIOException(ex); |
| 477 | + } |
| 478 | + }); |
| 479 | + |
| 480 | + var stderrGobbler = CompletableFuture.<Optional<List<String>>>supplyAsync(() -> { |
| 481 | + try { |
| 482 | + return processProcessStream(outputStreamsControl.stderr(), process.getErrorStream()); |
| 483 | + } catch (IOException ex) { |
| 484 | + throw new UncheckedIOException(ex); |
| 485 | + } |
| 486 | + }); |
| 487 | + |
| 488 | + final CommandOutput output; |
| 489 | + |
| 490 | + try { |
| 491 | + output = combine(stdoutGobbler.join(), stderrGobbler.join()); |
| 492 | + } catch (CompletionException ex) { |
| 493 | + var cause = ex.getCause(); |
| 494 | + switch (cause) { |
| 495 | + case UncheckedIOException uioex -> { |
| 496 | + throw uioex.getCause(); |
| 497 | + } |
| 498 | + default -> { |
| 499 | + throw ExceptionBox.toUnchecked(ExceptionBox.unbox(cause)); |
| 500 | + } |
| 501 | + } |
| 502 | + } |
471 | 503 |
|
472 | 504 | final int exitCode = process.waitFor(); |
473 | 505 | trace("Done. Exit code: " + exitCode); |
|
0 commit comments