changeset 968:ed4b2a0461b2

runners: protect from accidentally closing the System.out.
author shade
date Wed, 20 Aug 2014 12:25:51 +0400
parents 3c1528368e9f
children b9151815e306
files jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java jmh-core/src/main/java/org/openjdk/jmh/util/UnCloseablePrintStream.java
diffstat 2 files changed, 46 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Mon Aug 18 16:49:50 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Wed Aug 20 12:25:51 2014 +0400
@@ -46,6 +46,7 @@
 import org.openjdk.jmh.util.InputStreamDrainer;
 import org.openjdk.jmh.util.Multimap;
 import org.openjdk.jmh.util.TreeMultimap;
+import org.openjdk.jmh.util.UnCloseablePrintStream;
 import org.openjdk.jmh.util.Utils;
 
 import java.io.BufferedOutputStream;
@@ -105,17 +106,20 @@
 
         // Intercept the System.out when redirection is requested.
         // FIXME: We need to properly give up on stream after we are done with it
-        // FIXME: We need to safeguard from accidentally closing the underlying "out"
+        PrintStream out;
         if (options.getOutput().hasValue()) {
             try {
-                PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(new File(options.getOutput().get()))));
+                out = new PrintStream(options.getOutput().get());
                 System.setOut(out); // override to print everything to file
             } catch (FileNotFoundException ex) {
                 throw new IllegalStateException(ex);
             }
+        } else {
+            // Protect the System.out from accidental closing
+            out = new UnCloseablePrintStream(System.out);
         }
 
-        return OutputFormatFactory.createFormatInstance(System.out, options.verbosity().orElse(Defaults.VERBOSITY));
+        return OutputFormatFactory.createFormatInstance(out, options.verbosity().orElse(Defaults.VERBOSITY));
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/UnCloseablePrintStream.java	Wed Aug 20 12:25:51 2014 +0400
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.util;
+
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+public class UnCloseablePrintStream extends PrintStream {
+    public UnCloseablePrintStream(OutputStream out) {
+        super(out);
+    }
+
+    @Override
+    public void close() {
+        // Do nothing.
+    }
+}