[IO-426] Add API IOUtils.closeQuietly(Closeable...)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1565317 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bc42005..5e54007 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -47,6 +47,9 @@
<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>
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 113a05c..36f7d16 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1161,10 +1161,7 @@
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
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index e132ad7..b7a6059 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -281,24 +281,37 @@
/**
* 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("foo.txt");
+ * // 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 @@
}
/**
+ * 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("foo.txt");
+ * // 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.
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
index f202f5f..4474e30 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -124,6 +124,15 @@
}
}
+ 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 @@
// expected
}
} finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(fileInputStream);
+ IOUtils.closeQuietly(input, fileInputStream);
}}
public void testReadFully_InputStream_ByteArray() throws Exception {
@@ -684,8 +692,7 @@
// expected
}
} finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(fileInputStream);
+ IOUtils.closeQuietly(input, fileInputStream);
}
}
@@ -802,8 +809,7 @@
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 @@
// expected
}
} finally {
- IOUtils.closeQuietly(fileChannel);
- IOUtils.closeQuietly(fileInputStream);
+ IOUtils.closeQuietly(fileChannel, fileInputStream);
}
}