Skip to content

Commit

Permalink
IOUtils chain together IOException for multiple Closeable instances, …
Browse files Browse the repository at this point in the history
…add overload
  • Loading branch information
wodencafe committed Nov 5, 2021
1 parent bc80b9a commit d97b791
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,19 @@ public static void close(final Closeable closeable) throws IOException {
*/
public static void close(final Closeable... closeables) throws IOException {
if (closeables != null) {
IOException ioexception = null;
for (final Closeable closeable : closeables) {
close(closeable);
try {
close(closeable);
} catch (IOException ex) {
if (ioexception == null) {
ioexception = new IOException("IOUtils.close failed");
}
ioexception.addSuppressed(ex);
}
}
if (ioexception != null) {
throw ioexception;
}
}
}
Expand All @@ -418,6 +429,25 @@ public static void close(final Closeable closeable, final IOConsumer<IOException
}
}

/**
* Closes the given {@link Closeable} as a null-safe operation.
*
* @param closeables The resource(s) to close, may be null.
* @param consumer Consume the IOException thrown by {@link Closeable#close()}.
* @throws IOException if an I/O error occurs.
*/
public static void close(final Closeable[] closeables, final IOConsumer<IOException> consumer) throws IOException {
if (closeables != null) {
try {
close(closeables);
} catch (final IOException e) {
if (consumer != null) {
consumer.accept(e);
}
}
}
}

/**
* Closes a URLConnection.
*
Expand Down

0 comments on commit d97b791

Please sign in to comment.