11package org .mendrugo .fibula ;
22
3+ import org .openjdk .jmh .runner .OutputFormatAdapter ;
4+ import org .openjdk .jmh .runner .format .OutputFormat ;
5+ import org .openjdk .jmh .util .FileUtils ;
6+ import org .openjdk .jmh .util .InputStreamDrainer ;
7+ import org .openjdk .jmh .util .TempFile ;
8+
39import java .io .File ;
10+ import java .io .FileOutputStream ;
411import java .io .IOException ;
512import java .io .UncheckedIOException ;
613import java .nio .file .Path ;
916import java .util .Arrays ;
1017import java .util .List ;
1118
12- class NativeImage
19+ final class NativeImage
1320{
1421 private static final File EXECUTABLE = findExecutable ();
1522
16- private final Execute execute ;
23+ private final boolean verbosePrint ;
24+ private final OutputFormat out ;
1725
18- public NativeImage (Execute execute )
26+ public NativeImage (boolean verbosePrint , OutputFormat out )
1927 {
20- this .execute = execute ;
28+ this .verbosePrint = verbosePrint ;
29+ this .out = out ;
2130 }
2231
23- int getJavaVersion ()
32+ static int getJavaVersion ()
2433 {
2534 if (!EXECUTABLE .exists ())
2635 {
@@ -46,7 +55,46 @@ void execute(String... args)
4655 final List <String > commandString = new ArrayList <>();
4756 commandString .add (EXECUTABLE .getAbsolutePath ());
4857 commandString .addAll (Arrays .asList (args ));
49- execute .execute (commandString );
58+
59+ try
60+ {
61+ final TempFile tmpFile = FileUtils .weakTempFile ("nativeimage" );
62+
63+ try (FileOutputStream fos = new FileOutputStream (tmpFile .file ()))
64+ {
65+ final ProcessBuilder processBuilder = new ProcessBuilder (commandString );
66+ final Process process = processBuilder .start ();
67+
68+ final InputStreamDrainer errDrainer = new InputStreamDrainer (process .getErrorStream (), fos );
69+ final InputStreamDrainer outDrainer = new InputStreamDrainer (process .getInputStream (), fos );
70+
71+ if (verbosePrint )
72+ {
73+ errDrainer .addOutputStream (new OutputFormatAdapter (out ));
74+ outDrainer .addOutputStream (new OutputFormatAdapter (out ));
75+ }
76+
77+ errDrainer .start ();
78+ outDrainer .start ();
79+
80+ process .waitFor ();
81+
82+ errDrainer .join ();
83+ outDrainer .join ();
84+ }
85+ catch (InterruptedException e )
86+ {
87+ out .println ("<interrupted waiting for native image: " + e .getMessage () + ">" );
88+ out .println ("" );
89+ throw new RuntimeException (e );
90+ }
91+
92+ tmpFile .delete ();
93+ }
94+ catch (IOException e )
95+ {
96+ throw new UncheckedIOException (e );
97+ }
5098 }
5199
52100 private static File findExecutable ()
0 commit comments