IOUtils.close(Closeable) - allow a list
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -116,6 +116,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="update" due-to="Dependabot">
Update spotbugs from 4.0.6 to 4.1.1 #134.
</action>
+ <action dev="sebb" type="add">
+ IO-681 IOUtils.close(Closeable) should allow a list of closeables
+ </action>
</release>
<!-- The release date is the date RC is cut -->
<release version="2.7" date="2020-05-24" description="Java 8 required.">

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
@@ -367,15 +367,17 @@ public static void closeQuietly(final Closeable closeable, final Consumer<IOExce
/**
* Closes the given {@link Closeable} as a null-safe operation.
*
- * @param closeable The resource to close, may be null.
+ * @param closeables The resource(s) to close, may be null.
* @throws IOException if an I/O error occurs.
* @since 2.7
*/
- public static void close(final Closeable closeable) throws IOException {
+ public static void close(final Closeable... closeables) throws IOException {
+ for(Closeable closeable : closeables) {
if (closeable != null) {
closeable.close();
}
}
+ }
/**
* Closes the given {@link Closeable} as a null-safe operation.

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -146,6 +146,15 @@ public void setUp() {
assertThrows(IOException.class, () -> IOUtils.close(new YellOnCloseReader(new StringReader("s"))));
}
+ @Test public void testCloseMulti() {
+ Closeable nulCloseable = null;
+ Closeable [] closeables = {null, null};
+ assertDoesNotThrow(() -> IOUtils.close(nulCloseable,nulCloseable));
+ assertDoesNotThrow(() -> IOUtils.close(closeables));
+ assertDoesNotThrow(() -> IOUtils.close(new StringReader("s"),nulCloseable));
+ assertThrows(IOException.class, () -> IOUtils.close(nulCloseable, new YellOnCloseReader(new StringReader("s"))));
+ }
+
@Test public void testCloseConsumer() {
Closeable nulCloseable = null;
assertDoesNotThrow(() -> IOUtils.close(nulCloseable, null)); // null consumer

IO-681 IOUtils.close(Closeable) - allow a list