Package org.apache.druid.utils
Class CloseableUtils
- java.lang.Object
-
- org.apache.druid.utils.CloseableUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeAll(Closeable first, Closeable... others)
Call method instead of code like first.close(); second.close(); to have safety ofCloser
, but without associated boilerplate code of creating a Closer and registering objects in it.static <T extends Closeable>
voidcloseAll(Iterable<T> closeables)
Close all the provided {@param closeables}, from first to last.static void
closeAndSuppressExceptions(Closeable closeable, Consumer<Throwable> chomper)
LikeCloseable.close()
but sends any exceptions to the provided Consumer and then returns quietly.static void
closeAndWrapExceptions(Closeable closeable)
LikeCloseable.close()
but wraps IOExceptions in RuntimeExceptions.static <E extends Throwable>
RuntimeExceptioncloseAndWrapInCatch(E caught, Closeable closeable)
LikecloseInCatch(E, java.io.Closeable)
but wraps {@param caught} in aRuntimeException
if it is a checked exception.static <E extends Throwable>
RuntimeExceptioncloseInCatch(E caught, Closeable closeable)
LikeCloseable.close()
, but guaranteed to throw {@param caught}.
-
-
-
Method Detail
-
closeAll
public static void closeAll(Closeable first, Closeable... others) throws IOException
Call method instead of code like first.close(); second.close(); to have safety ofCloser
, but without associated boilerplate code of creating a Closer and registering objects in it.- Throws:
IOException
-
closeAll
public static <T extends Closeable> void closeAll(Iterable<T> closeables) throws IOException
Close all the provided {@param closeables}, from first to last.- Throws:
IOException
-
closeInCatch
public static <E extends Throwable> RuntimeException closeInCatch(E caught, @Nullable Closeable closeable) throws E extends Throwable
LikeCloseable.close()
, but guaranteed to throw {@param caught}. Will add any exceptions encountered during closing to {@param caught} usingThrowable.addSuppressed(Throwable)
. Should be used likethrow CloseableUtils.closeInCatch(e, closeable)
. (The "throw" is important for reachability detection.)- Throws:
E extends Throwable
-
closeAndWrapInCatch
public static <E extends Throwable> RuntimeException closeAndWrapInCatch(E caught, @Nullable Closeable closeable)
LikecloseInCatch(E, java.io.Closeable)
but wraps {@param caught} in aRuntimeException
if it is a checked exception.
-
closeAndWrapExceptions
public static void closeAndWrapExceptions(@Nullable Closeable closeable)
LikeCloseable.close()
but wraps IOExceptions in RuntimeExceptions.
-
closeAndSuppressExceptions
public static void closeAndSuppressExceptions(@Nullable Closeable closeable, Consumer<Throwable> chomper)
LikeCloseable.close()
but sends any exceptions to the provided Consumer and then returns quietly. If the Consumer throws an exception, that exception is thrown by this method. So if your intent is to chomp exceptions, you should avoid writing a Consumer that might throw an exception.
-
-