5050import java .util .Objects ;
5151import java .util .function .Consumer ;
5252
53+ import org .apache .commons .io .function .IOConsumer ;
5354import org .apache .commons .io .output .AppendableWriter ;
5455import org .apache .commons .io .output .ByteArrayOutputStream ;
5556import org .apache .commons .io .output .StringBuilderWriter ;
@@ -113,28 +114,28 @@ public class IOUtils {
113114 * The system directory separator character.
114115 */
115116 public static final char DIR_SEPARATOR = File .separatorChar ;
116-
117+
117118 /**
118119 * The Unix directory separator character.
119120 */
120121 public static final char DIR_SEPARATOR_UNIX = '/' ;
121-
122+
122123 /**
123124 * The Windows directory separator character.
124125 */
125126 public static final char DIR_SEPARATOR_WINDOWS = '\\' ;
126-
127+
127128 /**
128129 * Represents the end-of-file (or stream).
129130 * @since 2.5 (made public)
130131 */
131132 public static final int EOF = -1 ;
132-
133+
133134 /**
134135 * The system line separator string.
135136 */
136137 public static final String LINE_SEPARATOR ;
137-
138+
138139 /**
139140 * The Unix line separator string.
140141 */
@@ -164,7 +165,7 @@ public class IOUtils {
164165 * did not create a smaller one)
165166 */
166167 private static char [] SKIP_CHAR_BUFFER ;
167-
168+
168169 static {
169170 // avoid security issues
170171 try (final StringBuilderWriter buf = new StringBuilderWriter (4 );
@@ -343,7 +344,26 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
343344 */
344345 @ Deprecated
345346 public static void closeQuietly (final Closeable closeable ) {
346- close (closeable , null );
347+ closeQuietly (closeable , (Consumer <IOException >) null );
348+ }
349+
350+ /**
351+ * Closes the given {@link Closeable} as a null-safe operation while consuming IOException by the given {@code consumer}.
352+ *
353+ * @param closeable The resource to close, may be null.
354+ * @param consumer Consumes the IOException thrown by {@link Closeable#close()}.
355+ * @since 2.7
356+ */
357+ public static void closeQuietly (final Closeable closeable , final Consumer <IOException > consumer ) {
358+ if (closeable != null ) {
359+ try {
360+ closeable .close ();
361+ } catch (IOException e ) {
362+ if (consumer != null ) {
363+ consumer .accept (e );
364+ }
365+ }
366+ }
347367 }
348368
349369 /**
@@ -364,9 +384,10 @@ public static void close(final Closeable closeable) throws IOException {
364384 *
365385 * @param closeable The resource to close, may be null.
366386 * @param consumer Consume the IOException thrown by {@link Closeable#close()}.
387+ * @throws IOException if an I/O error occurs.
367388 * @since 2.7
368389 */
369- public static void close (final Closeable closeable , final Consumer <IOException > consumer ) {
390+ public static void close (final Closeable closeable , final IOConsumer <IOException > consumer ) throws IOException {
370391 if (closeable != null ) {
371392 try {
372393 closeable .close ();
0 commit comments