Add and reuse org.apache.commons.io.IOUtils.closeQuitely(Closeable,
Consumer<IOException>).
Add and reuse org.apache.commons.io.IOUtils.close(Closeable,
IOConsumer<IOException>).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -162,7 +162,8 @@ The <action> type attribute can be add,update,fix,remove.
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&lt;IOException&gt;)
+ Add and reuse org.apache.commons.io.IOUtils.closeQuitely(Closeable, Consumer&lt;IOException&gt;).
+ Add and reuse org.apache.commons.io.IOUtils.close(Closeable, IOConsumer&lt;IOException&gt;).
</action>
</release>

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -1847,7 +1847,7 @@ public static LineIterator lineIterator(final File file, final String encoding)
inputStream = openInputStream(file);
return IOUtils.lineIterator(inputStream, encoding);
} catch (final IOException | RuntimeException ex) {
- IOUtils.close(inputStream, e -> ex.addSuppressed(e));
+ IOUtils.closeQuietly(inputStream, e -> ex.addSuppressed(e));
throw ex;
}
}

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
import java.util.Objects;
import java.util.function.Consumer;
+import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.output.AppendableWriter;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.io.output.StringBuilderWriter;
@@ -343,7 +344,26 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
*/
@Deprecated
public static void closeQuietly(final Closeable closeable) {
- close(closeable, null);
+ closeQuietly(closeable, (Consumer<IOException>) null);
+ }
+
+ /**
+ * Closes the given {@link Closeable} as a null-safe operation while consuming IOException by the given {@code consumer}.
+ *
+ * @param closeable The resource to close, may be null.
+ * @param consumer Consumes the IOException thrown by {@link Closeable#close()}.
+ * @since 2.7
+ */
+ public static void closeQuietly(final Closeable closeable, final Consumer<IOException> consumer) {
+ if (closeable != null) {
+ try {
+ closeable.close();
+ } catch (IOException e) {
+ if (consumer != null) {
+ consumer.accept(e);
+ }
+ }
+ }
}
/**
@@ -364,9 +384,10 @@ public static void close(final Closeable closeable) throws IOException {
*
* @param closeable The resource to close, may be null.
* @param consumer Consume the IOException thrown by {@link Closeable#close()}.
+ * @throws IOException if an I/O error occurs.
* @since 2.7
*/
- public static void close(final Closeable closeable, final Consumer<IOException> consumer) {
+ public static void close(final Closeable closeable, final IOConsumer<IOException> consumer) throws IOException {
if (closeable != null) {
try {
closeable.close();

MODIFY
src/main/java/org/apache/commons/io/LineIterator.java
src/main/java/org/apache/commons/io/LineIterator.java
@@ -103,7 +103,7 @@ public boolean hasNext() {
}
}
} catch(final IOException ioe) {
- IOUtils.close(this, e -> ioe.addSuppressed(e));
+ IOUtils.closeQuietly(this, e -> ioe.addSuppressed(e));
throw new IllegalStateException(ioe);
}
}

ADD
src/main/java/org/apache/commons/io/function/IOConsumer.java
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.function;
+
+import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Consumer;
+
+/**
+ * Like {@link Consumer} but throws {@link IOException}.
+ *
+ * @param <T> the type of the input to the operations.
+ * @since 2.7
+ */
+@FunctionalInterface
+public interface IOConsumer<T> {
+
+ /**
+ * Performs this operation on the given argument.
+ *
+ * @param t the input argument
+ * @throws IOException if an I/O error occurs.
+ */
+ void accept(T t) throws IOException;
+
+ /**
+ * Returns a composed {@code IoConsumer} that performs, in sequence, this operation followed by the {@code after}
+ * operation. If performing either operation throws an exception, it is relayed to the caller of the composed
+ * operation. If performing this operation throws an exception, the {@code after} operation will not be performed.
+ *
+ * @param after the operation to perform after this operation
+ * @return a composed {@code Consumer} that performs in sequence this operation followed by the {@code after}
+ * operation
+ * @throws NullPointerException if {@code after} is null
+ */
+ default IOConsumer<T> andThen(IOConsumer<? super T> after) {
+ Objects.requireNonNull(after);
+ return (T t) -> {
+ accept(t);
+ after.accept(t);
+ };
+ }
+}

MODIFY
src/main/java/org/apache/commons/io/input/ProxyInputStream.java
src/main/java/org/apache/commons/io/input/ProxyInputStream.java
@@ -144,11 +144,7 @@ public int available() throws IOException {
*/
@Override
public void close() throws IOException {
- try {
- in.close();
- } catch (final IOException e) {
- handleIOException(e);
- }
+ IOUtils.close(in, e -> handleIOException(e));
}
/**

MODIFY
src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
src/main/java/org/apache/commons/io/output/ProxyOutputStream.java
@@ -114,11 +114,7 @@ public void flush() throws IOException {
*/
@Override
public void close() throws IOException {
- try {
- out.close();
- } catch (final IOException e) {
- handleIOException(e);
- }
+ IOUtils.close(out, e -> handleIOException(e));
}
/**

MODIFY
src/main/java/org/apache/commons/io/output/ProxyWriter.java
src/main/java/org/apache/commons/io/output/ProxyWriter.java
@@ -205,11 +205,7 @@ public void flush() throws IOException {
*/
@Override
public void close() throws IOException {
- try {
- out.close();
- } catch (final IOException e) {
- handleIOException(e);
- }
+ IOUtils.close(out, e -> handleIOException(e));
}
/**

[IO-636]

- Add and reuse org.apache.commons.io.IOUtils.closeQuitely(Closeable,
Consumer<IOException>).
- [IO-636] Add and reuse org.apache.commons.io.IOUtils.close(Closeable,
IOConsumer<IOException>).