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 14d6f4c [IO-636] 14d6f4c is described below commit 14d6f4c6dbb429ebb915b530ea61fe911d36b20b Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Nov 5 12:35:51 2019 -0500 [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>). --- src/changes/changes.xml | 3 +- src/main/java/org/apache/commons/io/FileUtils.java | 2 +- src/main/java/org/apache/commons/io/IOUtils.java | 37 +++++++++++--- .../java/org/apache/commons/io/LineIterator.java | 2 +- .../java/org/apache/commons/io/file/PathUtils.java | 2 +- .../org/apache/commons/io/function/IOConsumer.java | 58 ++++++++++++++++++++++ .../apache/commons/io/input/ProxyInputStream.java | 6 +-- .../commons/io/output/ProxyOutputStream.java | 6 +-- .../org/apache/commons/io/output/ProxyWriter.java | 6 +-- 9 files changed, 95 insertions(+), 27 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e08dd81..245f748 100644 --- a/src/changes/changes.xml +++ b/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<IOException>) + Add and reuse org.apache.commons.io.IOUtils.closeQuitely(Closeable, Consumer<IOException>). + Add and reuse org.apache.commons.io.IOUtils.close(Closeable, IOConsumer<IOException>). </action> </release> diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index 375748d..105ea8b 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -1847,7 +1847,7 @@ public class FileUtils { 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; } } diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index 6a7c178..b7f4b2c 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -50,6 +50,7 @@ import java.util.List; 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; @@ -113,28 +114,28 @@ public class IOUtils { * The system directory separator character. */ public static final char DIR_SEPARATOR = File.separatorChar; - + /** * The Unix directory separator character. */ public static final char DIR_SEPARATOR_UNIX = '/'; - + /** * The Windows directory separator character. */ public static final char DIR_SEPARATOR_WINDOWS = '\\'; - + /** * Represents the end-of-file (or stream). * @since 2.5 (made public) */ public static final int EOF = -1; - + /** * The system line separator string. */ public static final String LINE_SEPARATOR; - + /** * The Unix line separator string. */ @@ -164,7 +165,7 @@ public class IOUtils { * did not create a smaller one) */ private static char[] SKIP_CHAR_BUFFER; - + static { // avoid security issues try (final StringBuilderWriter buf = new StringBuilderWriter(4); @@ -343,7 +344,26 @@ public class IOUtils { */ @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 class IOUtils { * * @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(); diff --git a/src/main/java/org/apache/commons/io/LineIterator.java b/src/main/java/org/apache/commons/io/LineIterator.java index df9dbc0..c3ca9c3 100644 --- a/src/main/java/org/apache/commons/io/LineIterator.java +++ b/src/main/java/org/apache/commons/io/LineIterator.java @@ -103,7 +103,7 @@ public class LineIterator implements Iterator<String>, Closeable { } } } catch(final IOException ioe) { - IOUtils.close(this, e -> ioe.addSuppressed(e)); + IOUtils.closeQuietly(this, e -> ioe.addSuppressed(e)); throw new IllegalStateException(ioe); } } diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index ef58503..a6af570 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -65,7 +65,7 @@ public final class PathUtils { /** * Copies a file to a directory. - * + * * @param sourceFile The source file * @param targetDirectory The target directory. * @param copyOptions Specifies how the copying should be done. diff --git a/src/main/java/org/apache/commons/io/function/IOConsumer.java b/src/main/java/org/apache/commons/io/function/IOConsumer.java new file mode 100644 index 0000000..8474533 --- /dev/null +++ b/src/main/java/org/apache/commons/io/function/IOConsumer.java @@ -0,0 +1,58 @@ +/* + * 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); + }; + } +} diff --git a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java index 2ad32eb..d016740 100644 --- a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java @@ -144,11 +144,7 @@ public abstract class ProxyInputStream extends FilterInputStream { */ @Override public void close() throws IOException { - try { - in.close(); - } catch (final IOException e) { - handleIOException(e); - } + IOUtils.close(in, e -> handleIOException(e)); } /** diff --git a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java index 94c2fdd..a1b10c0 100644 --- a/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/ProxyOutputStream.java @@ -114,11 +114,7 @@ public class ProxyOutputStream extends FilterOutputStream { */ @Override public void close() throws IOException { - try { - out.close(); - } catch (final IOException e) { - handleIOException(e); - } + IOUtils.close(out, e -> handleIOException(e)); } /** diff --git a/src/main/java/org/apache/commons/io/output/ProxyWriter.java b/src/main/java/org/apache/commons/io/output/ProxyWriter.java index 5d62ba8..5ddd18d 100644 --- a/src/main/java/org/apache/commons/io/output/ProxyWriter.java +++ b/src/main/java/org/apache/commons/io/output/ProxyWriter.java @@ -205,11 +205,7 @@ public class ProxyWriter extends FilterWriter { */ @Override public void close() throws IOException { - try { - out.close(); - } catch (final IOException e) { - handleIOException(e); - } + IOUtils.close(out, e -> handleIOException(e)); } /**