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
The following commit(s) were added to refs/heads/master by this push: new 86e60dd [IO-636] Add and reuse org.apache.commons.io.IOUtils.close(Closeable, Consumer<IOException>) 86e60dd is described below commit 86e60ddb84c8e22d577190065d61e7bc6162db19 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Nov 5 12:01:33 2019 -0500 [IO-636] Add and reuse org.apache.commons.io.IOUtils.close(Closeable, Consumer<IOException>) --- src/changes/changes.xml | 3 +++ src/main/java/org/apache/commons/io/FileUtils.java | 7 +----- src/main/java/org/apache/commons/io/IOUtils.java | 26 +++++++++++++++++----- .../java/org/apache/commons/io/LineIterator.java | 6 +---- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cb053b2..e08dd81 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -161,6 +161,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="IO-635" dev="ggregory" type="add" due-to="Gary Gregory"> Add org.apache.commons.io.IOUtils.close(Closeable). </action> + <action issue="IO-636" dev="ggregory" type="add" due-to="Gary Gregory"> + Add and reuse org.apache.commons.io.IOUtils.close(Closeable, Consumer<IOException>) + </action> </release> <release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported."> diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index da87cf8..375748d 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -1847,12 +1847,7 @@ public class FileUtils { inputStream = openInputStream(file); return IOUtils.lineIterator(inputStream, encoding); } catch (final IOException | RuntimeException ex) { - try { - IOUtils.close(inputStream); - } - catch (final IOException e) { - ex.addSuppressed(e); - } + IOUtils.close(inputStream, e -> ex.addSuppressed(e)); throw ex; } } diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index 597f224..6a7c178 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -48,6 +48,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.function.Consumer; import org.apache.commons.io.output.AppendableWriter; import org.apache.commons.io.output.ByteArrayOutputStream; @@ -342,11 +343,7 @@ public class IOUtils { */ @Deprecated public static void closeQuietly(final Closeable closeable) { - try { - close(closeable); - } catch (final IOException ioe) { - // ignore - } + close(closeable, null); } /** @@ -363,6 +360,25 @@ public class IOUtils { } /** + * Closes the given {@link Closeable} as a null-safe operation. + * + * @param closeable The resource to close, may be null. + * @param consumer Consume the IOException thrown by {@link Closeable#close()}. + * @since 2.7 + */ + public static void close(final Closeable closeable, final Consumer<IOException> consumer) { + if (closeable != null) { + try { + closeable.close(); + } catch (IOException e) { + if (consumer != null) { + consumer.accept(e); + } + } + } + } + + /** * Closes a URLConnection. * * @param conn the connection to close. diff --git a/src/main/java/org/apache/commons/io/LineIterator.java b/src/main/java/org/apache/commons/io/LineIterator.java index 986c72e..df9dbc0 100644 --- a/src/main/java/org/apache/commons/io/LineIterator.java +++ b/src/main/java/org/apache/commons/io/LineIterator.java @@ -103,11 +103,7 @@ public class LineIterator implements Iterator<String>, Closeable { } } } catch(final IOException ioe) { - try { - close(); - } catch (final IOException e) { - ioe.addSuppressed(e); - } + IOUtils.close(this, e -> ioe.addSuppressed(e)); throw new IllegalStateException(ioe); } }