Skip to content

Commit 3b77766

Browse files
committed
The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts
Exception, not just IOException
1 parent e24dcb1 commit 3b77766

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
5555
<action dev="ggregory" type="fix" due-to="Gary Gregory">Deprecate DeferredFileOutputStream.getStream() in favor of getOutputStream().</action>
5656
<action dev="ggregory" type="fix" issue="IO-868" due-to="Julian Reschke, Gary Gregory">Improve Javadoc for a BoundedInputStream builder() throwing IOException.</action>
5757
<action dev="ggregory" type="fix" issue="IO-868" due-to="Julian Reschke, Gary Gregory">Improve Javadoc for all implementations of AbstractOriginSupplier#get().</action>
58+
<action dev="ggregory" type="fix" due-to="Gary Gregory">The Consumer to IOUtils.closeQuietly(Closeable, Consumer) now accepts Exception, not just IOException.</action>
5859
<!-- ADD -->
5960
<action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action>
6061
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.</action>

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,14 +575,14 @@ public static void closeQuietly(final Closeable... closeables) {
575575
* Closes the given {@link Closeable} as a null-safe operation while consuming IOException by the given {@code consumer}.
576576
*
577577
* @param closeable The resource to close, may be null.
578-
* @param consumer Consumes the IOException thrown by {@link Closeable#close()}.
578+
* @param consumer Consumes the Exception thrown by {@link Closeable#close()}.
579579
* @since 2.7
580580
*/
581-
public static void closeQuietly(final Closeable closeable, final Consumer<IOException> consumer) {
581+
public static void closeQuietly(final Closeable closeable, final Consumer<Exception> consumer) {
582582
if (closeable != null) {
583583
try {
584584
closeable.close();
585-
} catch (final IOException e) {
585+
} catch (final Exception e) {
586586
if (consumer != null) {
587587
consumer.accept(e);
588588
}

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import java.nio.file.Path;
6262
import java.util.Arrays;
6363
import java.util.List;
64+
import java.util.concurrent.atomic.AtomicBoolean;
65+
import java.util.function.Consumer;
6466
import java.util.function.Supplier;
6567
import java.util.stream.Stream;
6668

@@ -364,9 +366,40 @@ public void testCloseQuietly_AllCloseableIOException() {
364366
}
365367

366368
@Test
367-
public void testCloseQuietly_CloseableIOException() {
369+
public void testCloseQuietly_CloseableException() {
370+
// IOException
368371
assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenInputStream.INSTANCE));
369372
assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenOutputStream.INSTANCE));
373+
// IOException subclass
374+
assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream((Throwable) new EOFException())));
375+
// RuntimeException
376+
assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new RuntimeException())));
377+
// RuntimeException subclass
378+
assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new UnsupportedOperationException())));
379+
}
380+
381+
@Test
382+
public void testCloseQuietly_CloseableExceptionConsumer() {
383+
final AtomicBoolean b = new AtomicBoolean();
384+
final Consumer<Exception> consumer = e -> b.set(true);
385+
// IOException
386+
assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenInputStream.INSTANCE, consumer));
387+
assertTrue(b.get());
388+
b.set(false);
389+
assertDoesNotThrow(() -> IOUtils.closeQuietly(BrokenOutputStream.INSTANCE, consumer));
390+
assertTrue(b.get());
391+
b.set(false);
392+
// IOException subclass
393+
assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream((Throwable) new EOFException()), consumer));
394+
assertTrue(b.get());
395+
b.set(false);
396+
// RuntimeException
397+
assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new RuntimeException()), consumer));
398+
assertTrue(b.get());
399+
b.set(false);
400+
// RuntimeException subclass
401+
assertDoesNotThrow(() -> IOUtils.closeQuietly(new BrokenOutputStream(new UnsupportedOperationException()), consumer));
402+
assertTrue(b.get());
370403
}
371404

372405
@SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"

0 commit comments

Comments
 (0)