Package org.apache.commons.lang3
Class AutoCloseables
- java.lang.Object
-
- org.apache.commons.lang3.AutoCloseables
-
public class AutoCloseables extends java.lang.ObjectStatic operations onAutoCloseable.For
Closeable-specific methods, see Apache Commons IO's IOUtils.- Since:
- 3.21.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclose(java.lang.AutoCloseable closeable)Closes the givenAutoCloseableas a null-safe operation.static voidclose(java.lang.AutoCloseable closeable, FailableConsumer<java.lang.Exception,java.lang.Exception> consumer)Closes the givenAutoCloseableas a null-safe operation.static voidcloseQuietly(java.lang.AutoCloseable closeable)Closes anAutoCloseable, never throwing anException.static voidcloseQuietly(java.lang.AutoCloseable closeable, java.util.function.Consumer<java.lang.Exception> consumer)Closes the givenAutoCloseableas a null-safe operation while consuming Exception by the givenconsumer.static voidcloseQuietly(java.lang.Iterable<java.lang.AutoCloseable> closeables)Closes an iterable ofAutoCloseable, never throwing anException.static <T extends java.lang.Throwable>
TcloseQuietlySuppress(java.io.Closeable closeable, T throwable)Closes aCloseableunconditionally and adds any exception thrown by theclose()to the given Throwable.
-
-
-
Method Detail
-
close
public static void close(java.lang.AutoCloseable closeable) throws java.lang.ExceptionCloses the givenAutoCloseableas a null-safe operation.- Parameters:
closeable- The resource to close, may be null.- Throws:
java.lang.Exception- Thrown if an error occurs.
-
close
public static void close(java.lang.AutoCloseable closeable, FailableConsumer<java.lang.Exception,java.lang.Exception> consumer) throws java.lang.ExceptionCloses the givenAutoCloseableas a null-safe operation.- Parameters:
closeable- The resource to close, may be null.consumer- Consume the Exception thrown byAutoCloseable.close().- Throws:
java.lang.Exception- Thrown if the consumer throws an exception.
-
closeQuietly
public static void closeQuietly(java.lang.AutoCloseable closeable)
Closes anAutoCloseable, never throwing anException.Equivalent to
AutoCloseable.close(), except any exceptions will be ignored.- Parameters:
closeable- The objects to close, may be null or already closed.- See Also:
Throwable.addSuppressed(Throwable)
-
closeQuietly
public static void closeQuietly(java.lang.AutoCloseable closeable, java.util.function.Consumer<java.lang.Exception> consumer)Closes the givenAutoCloseableas a null-safe operation while consuming Exception by the givenconsumer.- Parameters:
closeable- The resource to close, may be null.consumer- Consumes the Exception thrown byAutoCloseable.close().
-
closeQuietly
public static void closeQuietly(java.lang.Iterable<java.lang.AutoCloseable> closeables)
Closes an iterable ofAutoCloseable, never throwing anException.Equivalent calling
AutoCloseable.close()on each element, except any exceptions will be ignored.- Parameters:
closeables- The objects to close, may be null or already closed.- See Also:
closeQuietly(AutoCloseable)
-
closeQuietlySuppress
public static <T extends java.lang.Throwable> T closeQuietlySuppress(java.io.Closeable closeable, T throwable)Closes aCloseableunconditionally and adds any exception thrown by theclose()to the given Throwable.For example:
AutoCloseable autoCloseable = ...; try { // process autoCloseable. } catch (Exception e) { // Handle exception. throw AutoCloseables.closeQuietlySuppress(autoCloseable, e); }Also consider using a try-with-resources statement where appropriate.
- Type Parameters:
T- The Throwable type.- Parameters:
closeable- The object to close, may be null or already closed.throwable- Add the exception throw by the closeable to the given Throwable.- Returns:
- The given Throwable.
- See Also:
Throwable.addSuppressed(Throwable)
-
-