4545import java .nio .charset .Charset ;
4646import java .nio .file .Files ;
4747import java .util .ArrayList ;
48- import java .util .Arrays ;
4948import java .util .Collection ;
5049import java .util .List ;
5150import java .util .Objects ;
5251import java .util .function .Consumer ;
52+ import java .util .stream .Stream ;
5353
5454import org .apache .commons .io .function .IOConsumer ;
5555import org .apache .commons .io .input .QueueInputStream ;
@@ -394,6 +394,25 @@ public static void close(final Closeable... closeables) throws IOException {
394394 IOConsumer .forEach (closeables , IOUtils ::close );
395395 }
396396
397+ /**
398+ * Closes the entries in the given {@link Stream} as null-safe operations,
399+ * and closes the underlying {@code Stream}.
400+ *
401+ * @param <T> The element type.
402+ * @param closeables The resource(s) to close, may be null.
403+ * @throws IOExceptionList if an I/O error occurs.
404+ * @since 2.12.0
405+ */
406+ public static <T extends Closeable > void close (final Stream <T > closeables ) throws IOExceptionList {
407+ if (closeables != null ) {
408+ try {
409+ IOConsumer .forEachIndexed (closeables , IOUtils ::close );
410+ } finally {
411+ closeables .close ();
412+ }
413+ }
414+ }
415+
397416 /**
398417 * Closes the given {@link Closeable} as a null-safe operation.
399418 *
@@ -415,13 +434,36 @@ public static void close(final Closeable closeable, final IOConsumer<IOException
415434 }
416435
417436 /**
418- * Closes the given {@link Closeable} as a null-safe operation.
437+ * Closes the entries in the given {@link Stream} as null-safe operations,
438+ * and closes the underlying {@code Stream}.
419439 *
440+ * @param <T> The element type.
441+ * @param consumer Consume the IOException thrown by {@link Closeable#close()}.
420442 * @param closeables The resource(s) to close, may be null.
443+ * @throws IOException if an I/O error occurs.
444+ */
445+ public static <T extends Closeable > void close (final IOConsumer <IOException > consumer , final Stream <T > closeables ) throws IOException {
446+ if (closeables != null ) {
447+ try {
448+ close (closeables );
449+ } catch (final IOException e ) {
450+ if (consumer != null ) {
451+ consumer .accept (e );
452+ }
453+ } finally {
454+ closeables .close ();
455+ }
456+ }
457+ }
458+
459+ /**
460+ * Closes the given {@link Closeable} as a null-safe operation.
461+ *
421462 * @param consumer Consume the IOException thrown by {@link Closeable#close()}.
463+ * @param closeables The resource(s) to close, may be null.
422464 * @throws IOException if an I/O error occurs.
423465 */
424- public static void close (final Closeable [] closeables , final IOConsumer <IOException > consumer ) throws IOException {
466+ public static void close (final IOConsumer <IOException > consumer , final Closeable ... closeables ) throws IOException {
425467 if (closeables != null ) {
426468 try {
427469 close (closeables );
@@ -538,7 +580,7 @@ public static void closeQuietly(final Closeable closeable) {
538580 */
539581 public static void closeQuietly (final Closeable ... closeables ) {
540582 if (closeables != null ) {
541- Arrays . stream (closeables ). forEach ( IOUtils ::closeQuietly );
583+ IOConsumer . forEachQuietly (closeables , IOUtils ::closeQuietly );
542584 }
543585 }
544586
@@ -561,6 +603,64 @@ public static void closeQuietly(final Closeable closeable, final Consumer<IOExce
561603 }
562604 }
563605
606+ /**
607+ * Closes the given {@link Stream} as a null-safe operation while consuming IOException by the given {@code consumer},
608+ * and closes the underlying {@code Stream}.
609+ *
610+ * @param <T> The element type.
611+ * @param closeables The resource(s) to close, may be null.
612+ * @since 2.12.0
613+ */
614+ public static <T extends Closeable > void closeQuietly (final Stream <T > closeables ) {
615+ closeQuietly (null , closeables );
616+ }
617+
618+ /**
619+ * Closes the given {@link Stream} as a null-safe operation while consuming IOException by the given {@code consumer},
620+ * and closes the underlying {@code Stream}.
621+ *
622+ * @param <T> The element type.
623+ * @param consumer Consume the IOException thrown by {@link Closeable#close()}.
624+ * @param closeables The resource(s) to close, may be null.
625+ * @since 2.12.0
626+ */
627+ public static <T extends Closeable > void closeQuietly (final Consumer <IOException > consumer , final Stream <T > closeables ) {
628+ if (closeables != null ) {
629+ try {
630+ close (closeables );
631+ } catch (final IOException e ) {
632+ if (consumer != null ) {
633+ consumer .accept (e );
634+ }
635+ } finally {
636+ try {
637+ closeables .close ();
638+ } catch (Exception e ) {
639+ // Do nothing.
640+ }
641+ }
642+ }
643+ }
644+
645+ /**
646+ * Closes the given {@link Closeable}s as a null-safe operation while consuming IOException by the given {@code consumer}.
647+ *
648+ * @param consumer Consume the IOException thrown by {@link Closeable#close()}.
649+ * @param closeables The resource(s) to close, may be null.
650+ * @since 2.12.0
651+ */
652+ public static void closeQuietly (final Consumer <IOException > consumer , final Closeable ... closeables ) {
653+ if (closeables != null ) {
654+ try {
655+ close (closeables );
656+ } catch (final IOException e ) {
657+ if (consumer != null ) {
658+ consumer .accept (e );
659+ }
660+ }
661+ }
662+ }
663+
564664 /**
565665 * Closes an {@code InputStream} unconditionally.
566666 * <p>
0 commit comments