Package org.apache.druid.utils
Class CloseableUtils
java.lang.Object
org.apache.druid.utils.CloseableUtils
Methods in this class could have belonged to
Closer, but not editing
that class to keep its source close to Guava source.-
Method Summary
Modifier and TypeMethodDescriptionstatic voidCall 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>
voidClose all the provided , from first to last.static voidcloseAndSuppressExceptions(Closeable closeable, Consumer<Throwable> chomper) LikeCloseable.close()but sends any exceptions to the provided Consumer and then returns quietly.static voidcloseAndWrapExceptions(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 in aRuntimeExceptionif it is a checked exception.static <E extends Throwable>
RuntimeExceptioncloseInCatch(E caught, Closeable closeable) LikeCloseable.close(), but guaranteed to throw .static CloserforIterable(Iterable<? extends Closeable> closeables)
-
Method Details
-
forIterable
-
closeAll
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
Close all the provided , from first to last.- Throws:
IOException
-
closeInCatch
public static <E extends Throwable> RuntimeException closeInCatch(E caught, @Nullable Closeable closeable) throws E LikeCloseable.close(), but guaranteed to throw . Will add any exceptions encountered during closing to 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 in aRuntimeExceptionif it is a checked exception. -
closeAndWrapExceptions
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.
-