changeset 961:0e898fece0ec

runners: touch the result file early.
author shade
date Mon, 18 Aug 2014 15:13:15 +0400
parents 3d44d68e45be
children b6b8161b42a2
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, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Sat Aug 16 15:00:18 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java	Mon Aug 18 15:13:15 2014 +0400
@@ -170,6 +170,18 @@
             }
         }
 
+        // If user requested the result file in one way or the other, touch the result file,
+        // and prepare to write it out after the run.
+        String resultFile = null;
+        if (options.getResult().hasValue() || options.getResultFormat().hasValue()) {
+            resultFile = options.getResult().orElse(Defaults.RESULT_FILE);
+            try {
+                FileUtils.touch(resultFile);
+            } catch (IOException e) {
+                throw new RunnerException("Can not touch the result file: " + resultFile);
+            }
+        }
+
         SortedSet<BenchmarkListEntry> benchmarks = list.find(out, options.getIncludes(), options.getExcludes());
 
         if (benchmarks.isEmpty()) {
@@ -232,11 +244,11 @@
         out.flush();
         out.close();
 
-        // If user requested the result file in one way or the other, write it out.
-        if (options.getResult().hasValue() || options.getResultFormat().hasValue()) {
+        // If user requested the result file, write it out.
+        if (resultFile != null) {
             ResultFormatFactory.getInstance(
                         options.getResultFormat().orElse(Defaults.RESULT_FORMAT),
-                        options.getResult().orElse(Defaults.RESULT_FILE)
+                        resultFile
             ).writeOut(results);
         }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Sat Aug 16 15:00:18 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Mon Aug 18 15:13:15 2014 +0400
@@ -221,4 +221,15 @@
         }
     }
 
+    public static void touch(String f) throws IOException {
+        File file = new File(f);
+        try {
+            if (file.createNewFile() || file.canWrite()) {
+                return;
+            }
+        } catch (IOException e) {
+            // fall-through
+        }
+        throw new IOException("The file is not writable: " + f);
+    }
 }