changeset 888:f8da27da18fd

utils: cleanup with FileUtils.safelyClose.
author shade
date Tue, 01 Jul 2014 20:55:58 +0400
parents edf1134d97f3
children 8c0fee087662
files jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java jmh-generator-asm/src/main/java/org/openjdk/jmh/generators/asm/ASMGeneratorSource.java
diffstat 4 files changed, 29 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue Jul 01 20:49:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/profile/LinuxPerfAsmProfiler.java	Tue Jul 01 20:55:58 2014 +0400
@@ -245,8 +245,7 @@
             errDrainer.join();
             outDrainer.join();
 
-            fos.flush();
-            fos.close();
+            FileUtils.safelyClose(fos);
         } catch (IOException ex) {
             throw new IllegalStateException(ex);
         } catch (InterruptedException ex) {
@@ -526,7 +525,7 @@
                     pwAsm.println(line.code);
                 }
                 pwAsm.flush();
-                asm.close();
+                FileUtils.safelyClose(asm);
 
                 pw.println("Perf-annotated Hotspot log is saved to " + target);
             } catch (IOException e) {
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Tue Jul 01 20:49:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Tue Jul 01 20:55:58 2014 +0400
@@ -30,6 +30,7 @@
 import org.openjdk.jmh.runner.BenchmarkException;
 import org.openjdk.jmh.runner.format.OutputFormat;
 import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.util.FileUtils;
 import org.openjdk.jmh.util.Multimap;
 
 import java.io.IOException;
@@ -83,12 +84,9 @@
 
     public void close() throws IOException {
         oos.writeObject(new FinishingFrame());
-        oos.flush();
-        oos.close();
-        streamErr.flush();
-        streamErr.close();
-        streamOut.flush();
-        streamOut.close();
+        FileUtils.safelyClose(oos);
+        FileUtils.safelyClose(streamErr);
+        FileUtils.safelyClose(streamOut);
         clientSocket.close();
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Tue Jul 01 20:49:56 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/FileUtils.java	Tue Jul 01 20:55:58 2014 +0400
@@ -89,42 +89,10 @@
 
             return temp;
         } finally {
-            if (bos != null) {
-                try {
-                    bos.flush();
-                } catch (IOException e) {
-                    // ignore
-                }
-                try {
-                    bos.close();
-                } catch (IOException e) {
-                    // ignore
-                }
-            }
-
-            if (bis != null) {
-                try {
-                    bis.close();
-                } catch (IOException e) {
-                    // ignore
-                }
-            }
-
-            if (fis != null) {
-                try {
-                    fis.close();
-                } catch (IOException e) {
-                    // ignore
-                }
-            }
-
-            if (fos != null) {
-                try {
-                    fos.close();
-                } catch (IOException e) {
-                    // ignore
-                }
-            }
+            FileUtils.safelyClose(bis);
+            FileUtils.safelyClose(bos);
+            FileUtils.safelyClose(fis);
+            FileUtils.safelyClose(fos);
         }
     }
 
@@ -164,9 +132,7 @@
             }
             return lines;
         } finally {
-            if (fis != null) {
-                fis.close();
-            }
+            FileUtils.safelyClose(fis);
         }
     }
 
@@ -208,10 +174,8 @@
             fos.write(buf, 0, read);
         }
 
-        fos.flush();
-        fos.close();
-
-        fis.close();
+        FileUtils.safelyClose(fos);
+        FileUtils.safelyClose(fis);
     }
 
     public static void safelyClose(OutputStream out) {
@@ -228,4 +192,15 @@
             }
         }
     }
+
+    private static void safelyClose(InputStream in) {
+        if (in != null) {
+            try {
+                in.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+
 }
--- a/jmh-generator-asm/src/main/java/org/openjdk/jmh/generators/asm/ASMGeneratorSource.java	Tue Jul 01 20:49:56 2014 +0400
+++ b/jmh-generator-asm/src/main/java/org/openjdk/jmh/generators/asm/ASMGeneratorSource.java	Tue Jul 01 20:55:58 2014 +0400
@@ -27,6 +27,7 @@
 import org.objectweb.asm.ClassReader;
 import org.openjdk.jmh.generators.core.ClassInfo;
 import org.openjdk.jmh.generators.core.GeneratorSource;
+import org.openjdk.jmh.util.FileUtils;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -49,14 +50,12 @@
     }
 
     public void processClass(File classFile) throws IOException {
-        FileInputStream fos = null;
+        FileInputStream fis = null;
         try {
-            fos = new FileInputStream(classFile);
-            processClass(fos);
+            fis = new FileInputStream(classFile);
+            processClass(fis);
         } finally {
-            if (fos != null) {
-                fos.close();
-            }
+            FileUtils.safelyClose(fis);
         }
     }