changeset 887:edf1134d97f3

runner: properly close stdOut/stdErr temporary files.
author shade
date Tue, 01 Jul 2014 20:49:56 +0400
parents e1d00e02caa8
children f8da27da18fd
files jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java
diffstat 2 files changed, 25 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jul 01 20:41:46 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Tue Jul 01 20:49:56 2014 +0400
@@ -605,12 +605,17 @@
 
     private Multimap<BenchmarkParams, BenchmarkResult> doFork(BinaryLinkServer reader, String[] commandString,
                                                               File stdOut, File stdErr, boolean printOut, boolean printErr) {
+        FileOutputStream fosErr = null;
+        FileOutputStream fosOut = null;
         try {
             Process p = Runtime.getRuntime().exec(commandString);
 
+            fosErr = new FileOutputStream(stdErr);
+            fosOut = new FileOutputStream(stdOut);
+
             // drain streams, else we might lock up
-            InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream(), new FileOutputStream(stdErr));
-            InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream(), new FileOutputStream(stdOut));
+            InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream(), fosErr);
+            InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream(), fosOut);
 
             if (printErr) {
                 errDrainer.addOutputStream(System.err);
@@ -657,6 +662,9 @@
             throw new BenchmarkException(ex);
         } catch (InterruptedException ex) {
             throw new BenchmarkException(ex);
+        } finally {
+            FileUtils.safelyClose(fosErr);
+            FileUtils.safelyClose(fosOut);
         }
 
         BenchmarkException exception = reader.getException();
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Tue Jul 01 20:41:46 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Tue Jul 01 20:49:56 2014 +0400
@@ -213,4 +213,19 @@
 
         fis.close();
     }
+
+    public static void safelyClose(OutputStream out) {
+        if (out != null) {
+            try {
+                out.flush();
+            } catch (IOException e) {
+                // ignore
+            }
+            try {
+                out.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
 }