Skip to content

Commit 3598f66

Browse files
committed
Add org.apache.commons.io.IOUtils.closeQuietly(AutoCloseable[,
Consumer<Exception>]) Only adding these two for Commons VFS
1 parent 02ea2f8 commit 3598f66

2 files changed

Lines changed: 434 additions & 9 deletions

File tree

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

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,69 @@ private static void closeQ(final Closeable closeable) {
779779
closeQuietly(closeable, (Consumer<Exception>) null);
780780
}
781781

782+
/**
783+
* Closes a {@link AutoCloseable} unconditionally.
784+
* <p>
785+
* Equivalent to {@link AutoCloseable#close()}, except any exceptions will be ignored. This is typically used in finally blocks.
786+
* <p>
787+
* Example code:
788+
* </p>
789+
*
790+
* <pre>
791+
* AutoCloseable closeable = null;
792+
* try {
793+
* closeable = new FileReader(&quot;foo.txt&quot;);
794+
* // process closeable
795+
* closeable.close();
796+
* } catch (Exception e) {
797+
* // error handling
798+
* } finally {
799+
* IOUtils.closeQuietly(closeable);
800+
* }
801+
* </pre>
802+
* <p>
803+
* Closing all streams:
804+
* </p>
805+
*
806+
* <pre>
807+
* try {
808+
* return IOUtils.copy(inputStream, outputStream);
809+
* } finally {
810+
* IOUtils.closeQuietly(inputStream);
811+
* IOUtils.closeQuietly(outputStream);
812+
* }
813+
* </pre>
814+
* <p>
815+
* Also consider using a try-with-resources statement where appropriate.
816+
* </p>
817+
*
818+
* @param closeable the objects to close, may be null or already closed.
819+
* @since 2.23.0
820+
* @see Throwable#addSuppressed(Throwable)
821+
*/
822+
public static void closeQuietly(final AutoCloseable closeable) {
823+
closeQuietly(closeable, (Consumer<Exception>) null);
824+
}
825+
826+
/**
827+
* Closes the given {@link AutoCloseable} as a null-safe operation while consuming IOException by the given {@code consumer}.
828+
*
829+
* @param closeable The resource to close, may be null.
830+
* @param consumer Consumes the Exception thrown by {@link AutoCloseable#close()}.
831+
* @since 2.23.0
832+
*/
833+
public static void closeQuietly(final AutoCloseable closeable, final Consumer<Exception> consumer) {
834+
if (closeable != null) {
835+
try {
836+
closeable.close();
837+
} catch (final Exception e) {
838+
if (consumer != null) {
839+
consumer.accept(e);
840+
}
841+
}
842+
}
843+
}
844+
782845
/**
783846
* Closes a {@link Closeable} unconditionally.
784847
* <p>
@@ -885,15 +948,7 @@ public static void closeQuietly(final Closeable... closeables) {
885948
* @since 2.7
886949
*/
887950
public static void closeQuietly(final Closeable closeable, final Consumer<Exception> consumer) {
888-
if (closeable != null) {
889-
try {
890-
closeable.close();
891-
} catch (final Exception e) {
892-
if (consumer != null) {
893-
consumer.accept(e);
894-
}
895-
}
896-
}
951+
closeQuietly((AutoCloseable) closeable, consumer);
897952
}
898953

899954
/**

0 commit comments

Comments
 (0)