commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1565317 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/FileUtils.java main/java/org/apache/commons/io/IOUtils.java test/java/org/apache/commons/io/IOUtilsTestCase.java
Date Thu, 06 Feb 2014 16:02:20 GMT
Author: ggregory
Date: Thu Feb  6 16:02:20 2014
New Revision: 1565317

URL: http://svn.apache.org/r1565317
Log:
[IO-426] Add API IOUtils.closeQuietly(Closeable...)

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1565317&r1=1565316&r2=1565317&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Thu Feb  6 16:02:20 2014
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="2014-??-??" description="New features and bug fixes.">
+      <action issue="IO-426" dev="ggregory" type="add">
+         Add API IOUtils.closeQuietly(Closeable...)
+      </action>
       <action issue="IO-424" dev="ggregory" type="fix" due-to="Ville Skyttä">
          Javadoc fixes, mostly to appease 1.8.0
       </action>

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1565317&r1=1565316&r2=1565317&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Thu Feb  6
16:02:20 2014
@@ -1161,10 +1161,7 @@ public class FileUtils {
                 pos += bytesCopied;
             }
         } finally {
-            IOUtils.closeQuietly(output);
-            IOUtils.closeQuietly(fos);
-            IOUtils.closeQuietly(input);
-            IOUtils.closeQuietly(fis);
+            IOUtils.closeQuietly(output, fos, input, fis);
         }
 
         final long srcLen = srcFile.length(); // TODO See IO-386

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1565317&r1=1565316&r2=1565317&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java Thu Feb  6 16:02:20
2014
@@ -281,24 +281,37 @@ public class IOUtils {
     /**
      * Closes a <code>Closeable</code> unconditionally.
      * <p>
-     * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored.
-     * This is typically used in finally blocks.
+     * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This
is typically used in
+     * finally blocks.
      * <p>
      * Example code:
+     * 
      * <pre>
-     *   Closeable closeable = null;
-     *   try {
-     *       closeable = new FileReader("foo.txt");
-     *       // process closeable
-     *       closeable.close();
-     *   } catch (Exception e) {
-     *       // error handling
-     *   } finally {
-     *       IOUtils.closeQuietly(closeable);
-     *   }
+     * Closeable closeable = null;
+     * try {
+     *     closeable = new FileReader(&quot;foo.txt&quot;);
+     *     // process closeable
+     *     closeable.close();
+     * } catch (Exception e) {
+     *     // error handling
+     * } finally {
+     *     IOUtils.closeQuietly(closeable);
+     * }
      * </pre>
-     *
-     * @param closeable the object to close, may be null or already closed
+     * 
+     * Closing all streams:
+     * 
+     * <pre>
+     * try {
+     *     return IOUtils.copy(inputStream, outputStream);
+     * } finally {
+     *     IOUtils.closeQuietly(inputStream);
+     *     IOUtils.closeQuietly(outputStream);
+     * }
+     * </pre>
+     * 
+     * @param closeable
+     *            the objects to close, may be null or already closed
      * @since 2.0
      */
     public static void closeQuietly(final Closeable closeable) {
@@ -312,6 +325,51 @@ public class IOUtils {
     }
 
     /**
+     * Closes a <code>Closeable</code> unconditionally.
+     * <p>
+     * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This
is typically used in
+     * finally blocks.
+     * <p>
+     * Example code:
+     * 
+     * <pre>
+     * Closeable closeable = null;
+     * try {
+     *     closeable = new FileReader(&quot;foo.txt&quot;);
+     *     // process closeable
+     *     closeable.close();
+     * } catch (Exception e) {
+     *     // error handling
+     * } finally {
+     *     IOUtils.closeQuietly(closeable);
+     * }
+     * </pre>
+     * 
+     * Closing all streams:
+     * 
+     * <pre>
+     * try {
+     *     return IOUtils.copy(inputStream, outputStream);
+     * } finally {
+     *     IOUtils.closeQuietly(inputStream);
+     *     IOUtils.closeQuietly(outputStream);
+     * }
+     * </pre>
+     * 
+     * @param closeables
+     *            the objects to close, may be null or already closed
+     * @since 2.5
+     */
+    public static void closeQuietly(final Closeable... closeables) {
+        if (closeables == null) {
+            return;
+        }
+        for (final Closeable closeable : closeables) {
+            closeQuietly(closeable);
+        }
+    }
+
+    /**
      * Closes a <code>Socket</code> unconditionally.
      * <p>
      * Equivalent to {@link Socket#close()}, except any exceptions will be ignored.

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java?rev=1565317&r1=1565316&r2=1565317&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java Thu Feb
 6 16:02:20 2014
@@ -124,6 +124,15 @@ public class IOUtilsTestCase extends Fil
         }
     }
 
+    public void testCloseQuietly_AllCloseableIOException() {
+        final Closeable closeable = new Closeable() {
+            public void close() throws IOException {
+                throw new IOException();
+            }
+        };
+        IOUtils.closeQuietly(closeable, null, closeable);
+    }
+
     public void testCloseQuietly_CloseableIOException() {
         IOUtils.closeQuietly(new Closeable() {
             public void close() throws IOException {
@@ -614,8 +623,7 @@ public class IOUtilsTestCase extends Fil
                 // expected
             }
         } finally {
-            IOUtils.closeQuietly(input);
-            IOUtils.closeQuietly(fileInputStream);
+            IOUtils.closeQuietly(input, fileInputStream);
         }}
 
     public void testReadFully_InputStream_ByteArray() throws Exception {
@@ -684,8 +692,7 @@ public class IOUtilsTestCase extends Fil
                 // expected
             }
         } finally {
-            IOUtils.closeQuietly(input);
-            IOUtils.closeQuietly(fileInputStream);
+            IOUtils.closeQuietly(input, fileInputStream);
         }
     }
 
@@ -802,8 +809,7 @@ public class IOUtilsTestCase extends Fil
             assertEquals(10, IOUtils.skip(fileChannel, 20));
             assertEquals(0, IOUtils.skip(fileChannel, 10));
         } finally {
-            IOUtils.closeQuietly(fileChannel);
-            IOUtils.closeQuietly(fileInputStream);
+            IOUtils.closeQuietly(fileChannel, fileInputStream);
         }
     }
 
@@ -848,8 +854,7 @@ public class IOUtilsTestCase extends Fil
                 // expected
             }
         } finally {
-            IOUtils.closeQuietly(fileChannel);
-            IOUtils.closeQuietly(fileInputStream);
+            IOUtils.closeQuietly(fileChannel, fileInputStream);
         }
     }
 



Mime
View raw message