This is an automated email from the ASF dual-hosted git repository.
sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new aefc8f4 IO-681 IOUtils.close(Closeable) - allow a list
aefc8f4 is described below
commit aefc8f42b027b33eb7b7d3346daef46e21384535
Author: Sebb <[email protected]>
AuthorDate: Thu Aug 6 23:01:19 2020 +0100
IO-681 IOUtils.close(Closeable) - allow a list
---
src/changes/changes.xml | 3 +++
src/main/java/org/apache/commons/io/IOUtils.java | 10 ++++++----
src/test/java/org/apache/commons/io/IOUtilsTestCase.java | 9 +++++++++
3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d6e5335..6fcb369 100644
--- a/src/changes/changes.xml
+++ b/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.">
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java
b/src/main/java/org/apache/commons/io/IOUtils.java
index 55c2cfd..a9b1122 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -367,13 +367,15 @@ public class IOUtils {
/**
* 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 {
- if (closeable != null) {
- closeable.close();
+ public static void close(final Closeable... closeables) throws IOException
{
+ for(Closeable closeable : closeables) {
+ if (closeable != null) {
+ closeable.close();
+ }
}
}
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
index 20aba38..82ae8f4 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -146,6 +146,15 @@ public class IOUtilsTestCase {
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