Skip to content

Commit 86e60dd

Browse files
author
Gary Gregory
committed
[IO-636] Add and reuse org.apache.commons.io.IOUtils.close(Closeable,
Consumer<IOException>)
1 parent 388974f commit 86e60dd

4 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ The <action> type attribute can be add,update,fix,remove.
161161
<action issue="IO-635" dev="ggregory" type="add" due-to="Gary Gregory">
162162
Add org.apache.commons.io.IOUtils.close(Closeable).
163163
</action>
164+
<action issue="IO-636" dev="ggregory" type="add" due-to="Gary Gregory">
165+
Add and reuse org.apache.commons.io.IOUtils.close(Closeable, Consumer&lt;IOException&gt;)
166+
</action>
164167
</release>
165168

166169
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,12 +1847,7 @@ public static LineIterator lineIterator(final File file, final String encoding)
18471847
inputStream = openInputStream(file);
18481848
return IOUtils.lineIterator(inputStream, encoding);
18491849
} catch (final IOException | RuntimeException ex) {
1850-
try {
1851-
IOUtils.close(inputStream);
1852-
}
1853-
catch (final IOException e) {
1854-
ex.addSuppressed(e);
1855-
}
1850+
IOUtils.close(inputStream, e -> ex.addSuppressed(e));
18561851
throw ex;
18571852
}
18581853
}

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Collection;
4949
import java.util.List;
5050
import java.util.Objects;
51+
import java.util.function.Consumer;
5152

5253
import org.apache.commons.io.output.AppendableWriter;
5354
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -342,11 +343,7 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
342343
*/
343344
@Deprecated
344345
public static void closeQuietly(final Closeable closeable) {
345-
try {
346-
close(closeable);
347-
} catch (final IOException ioe) {
348-
// ignore
349-
}
346+
close(closeable, null);
350347
}
351348

352349
/**
@@ -362,6 +359,25 @@ public static void close(final Closeable closeable) throws IOException {
362359
}
363360
}
364361

362+
/**
363+
* Closes the given {@link Closeable} as a null-safe operation.
364+
*
365+
* @param closeable The resource to close, may be null.
366+
* @param consumer Consume the IOException thrown by {@link Closeable#close()}.
367+
* @since 2.7
368+
*/
369+
public static void close(final Closeable closeable, final Consumer<IOException> consumer) {
370+
if (closeable != null) {
371+
try {
372+
closeable.close();
373+
} catch (IOException e) {
374+
if (consumer != null) {
375+
consumer.accept(e);
376+
}
377+
}
378+
}
379+
}
380+
365381
/**
366382
* Closes a URLConnection.
367383
*

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ public boolean hasNext() {
103103
}
104104
}
105105
} catch(final IOException ioe) {
106-
try {
107-
close();
108-
} catch (final IOException e) {
109-
ioe.addSuppressed(e);
110-
}
106+
IOUtils.close(this, e -> ioe.addSuppressed(e));
111107
throw new IllegalStateException(ioe);
112108
}
113109
}

0 commit comments

Comments
 (0)