This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 57313b3ed3b356d9a81103f4dbcc1eb38b9f2c0b
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Tue Jun 21 13:40:37 2022 -0400

    Add IOUtils.closeQuietly(Collection<Closeable>).
    
    Add IOUtils.closeQuietly(Stream<Closeable>).
    Internal refactoring to avoid type casts.
---
 src/changes/changes.xml                            |  6 ++-
 src/main/java/org/apache/commons/io/IOUtils.java   | 61 ++++++++++++++++++----
 .../java/org/apache/commons/io/IOUtilsTest.java    | 16 +++---
 .../serialization/AbstractCloseableListTest.java   | 10 +---
 4 files changed, 67 insertions(+), 26 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fd142081..2b1a69bf 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -334,7 +334,7 @@ The <action> type attribute can be add,update,fix,remove.
           - PathUtils.getAclFileAttributeView(Path, LinkOption...)
           - PathUtils.getDosFileAttributeView(Path, LinkOption...)
           - PathUtils.getPosixFileAttributeView(Path, LinkOption...)
-      </action>      
+      </action>
       <action issue="IO-747" dev="mgrigorov" type="add">
         Make commons-io a JPMS module by adding module-info.class.
       </action>
@@ -389,6 +389,10 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add IOBiConsumer.noop().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add IOUtils.closeQuietly(Collection&lt;Closeable&gt;).
+        Add IOUtils.closeQuietly(Stream&lt;Closeable&gt;).
+      </action>
       <!-- UPDATE -->
       <action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
         Bump actions/cache from 2.1.6 to 3.0.4 #307, #337.
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java 
b/src/main/java/org/apache/commons/io/IOUtils.java
index 1e1b2a95..7ac55595 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -51,6 +51,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Objects;
 import java.util.function.Consumer;
+import java.util.stream.Stream;
 
 import org.apache.commons.io.function.IOConsumer;
 import org.apache.commons.io.input.QueueInputStream;
@@ -472,6 +473,13 @@ public class IOUtils {
         closeQuietly(closeable, null);
     }
 
+    /**
+     * Avoids the need to type cast.
+     */
+    private static void closeQ(final Closeable closeable) {
+        closeQuietly(closeable, null);
+    }
+
     /**
      * Closes a {@link Closeable} unconditionally.
      * <p>
@@ -520,7 +528,7 @@ public class IOUtils {
      */
     public static void closeQuietly(final Closeable... closeables) {
         if (closeables != null) {
-            Arrays.stream(closeables).forEach(IOUtils::closeQuietly);
+            closeQuietly(Arrays.stream(closeables));
         }
     }
 
@@ -573,7 +581,39 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final InputStream input) {
-        closeQuietly((Closeable) input);
+        closeQ(input);
+    }
+
+    /**
+     * Closes an iterable of {@link Closeable} unconditionally.
+     * <p>
+     * Equivalent calling {@link Closeable#close()} on each element, except 
any exceptions will be ignored.
+     * </p>
+     *
+     * @param closeables the objects to close, may be null or already closed
+     * @see #closeQuietly(Closeable)
+     * @since 2.12.0
+     */
+    public static void closeQuietly(final Iterable<Closeable> closeables) {
+        if (closeables != null) {
+            closeables.forEach(IOUtils::closeQuietly);
+        }
+    }
+
+    /**
+     * Closes a stream of {@link Closeable} unconditionally.
+     * <p>
+     * Equivalent calling {@link Closeable#close()} on each element, except 
any exceptions will be ignored.
+     * </p>
+     *
+     * @param closeables the objects to close, may be null or already closed
+     * @see #closeQuietly(Closeable)
+     * @since 2.12.0
+     */
+    public static void closeQuietly(final Stream<Closeable> closeables) {
+        if (closeables != null) {
+            closeables.forEach(IOUtils::closeQuietly);
+        }
     }
 
     /**
@@ -607,7 +647,7 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final OutputStream output) {
-        closeQuietly((Closeable) output);
+        closeQ(output);
     }
 
     /**
@@ -640,7 +680,7 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final Reader reader) {
-        closeQuietly((Closeable) reader);
+        closeQ(reader);
     }
 
     /**
@@ -673,7 +713,7 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final Selector selector) {
-        closeQuietly((Closeable) selector);
+        closeQ(selector);
     }
 
     /**
@@ -706,7 +746,7 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final ServerSocket serverSocket) {
-        closeQuietly((Closeable) serverSocket);
+        closeQ(serverSocket);
     }
 
     /**
@@ -739,7 +779,7 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final Socket socket) {
-        closeQuietly((Closeable) socket);
+        closeQ(socket);
     }
 
     /**
@@ -771,7 +811,7 @@ public class IOUtils {
      * @see Throwable#addSuppressed(java.lang.Throwable)
      */
     public static void closeQuietly(final Writer writer) {
-        closeQuietly((Closeable) writer);
+        closeQ(writer);
     }
 
     /**
@@ -787,8 +827,7 @@ public class IOUtils {
      * @throws IOException if an I/O error occurs.
      * @since 2.8.0
      */
-    public static long consume(final InputStream input)
-            throws IOException {
+    public static long consume(final InputStream input) throws IOException {
         return copyLarge(input, NullOutputStream.INSTANCE, getByteArray());
     }
 
@@ -3297,6 +3336,7 @@ public class IOUtils {
         write(data, output, Charsets.toCharset(charsetName));
     }
 
+
     /**
      * Writes chars from a {@link CharSequence} to a {@link Writer}.
      *
@@ -3312,7 +3352,6 @@ public class IOUtils {
         }
     }
 
-
     /**
      * Writes chars from a {@link String} to bytes on an
      * {@link OutputStream} using the default character encoding of the
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java 
b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index ab140520..c2cba682 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -57,12 +57,15 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Stream;
 
 import org.apache.commons.io.function.IOConsumer;
+import org.apache.commons.io.input.BrokenInputStream;
 import org.apache.commons.io.input.CircularInputStream;
 import org.apache.commons.io.input.NullInputStream;
 import org.apache.commons.io.input.StringInputStream;
 import org.apache.commons.io.output.AppendableWriter;
+import org.apache.commons.io.output.BrokenOutputStream;
 import org.apache.commons.io.output.CountingOutputStream;
 import org.apache.commons.io.output.NullOutputStream;
 import org.apache.commons.io.output.NullWriter;
@@ -339,18 +342,19 @@ public class IOUtilsTest {
 
     @Test
     public void testCloseQuietly_AllCloseableIOException() {
-        final Closeable closeable = () -> {
-            throw new IOException();
-        };
+        final Closeable closeable = BrokenInputStream.INSTANCE;
         assertDoesNotThrow(() -> IOUtils.closeQuietly(closeable, null, 
closeable));
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(Arrays.asList(closeable, 
null, closeable)));
+        assertDoesNotThrow(() -> IOUtils.closeQuietly(Stream.of(closeable, 
null, closeable)));
     }
 
     @Test
     public void testCloseQuietly_CloseableIOException() {
         assertDoesNotThrow(() -> {
-            IOUtils.closeQuietly(() -> {
-                throw new IOException();
-            });
+            IOUtils.closeQuietly(BrokenInputStream.INSTANCE);
+        });
+        assertDoesNotThrow(() -> {
+            IOUtils.closeQuietly(BrokenOutputStream.INSTANCE);
         });
     }
 
diff --git 
a/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
 
b/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
index 8efa7a3f..71bfbd27 100644
--- 
a/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
+++ 
b/src/test/java/org/apache/commons/io/serialization/AbstractCloseableListTest.java
@@ -19,10 +19,10 @@
 package org.apache.commons.io.serialization;
 
 import java.io.Closeable;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 
@@ -34,13 +34,7 @@ public abstract class AbstractCloseableListTest {
 
     @AfterEach
     public void cleanup() {
-        for (final Closeable c : closeableList) {
-            try {
-                c.close();
-            } catch (final IOException ignored) {
-                // ignore
-            }
-        }
+        IOUtils.closeQuietly(closeableList);
     }
 
     /**

Reply via email to