Class CloseableUtils

java.lang.Object
org.apache.druid.utils.CloseableUtils

public final class CloseableUtils extends Object
Methods in this class could have belonged to Closer, but not editing that class to keep its source close to Guava source.
  • Method Details

    • forIterable

      public static Closer forIterable(Iterable<? extends Closeable> closeables)
    • closeAll

      public static void closeAll(Closeable first, Closeable... others) throws IOException
      Call method instead of code like first.close(); second.close(); to have safety of Closer, 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 , from first to last.
      Throws:
      IOException
    • closeInCatch

      public static <E extends Throwable> RuntimeException closeInCatch(E caught, @Nullable Closeable closeable) throws E
      Like Closeable.close(), but guaranteed to throw . Will add any exceptions encountered during closing to using Throwable.addSuppressed(Throwable). Should be used like throw 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)
      Like closeInCatch(E, java.io.Closeable) but wraps in a RuntimeException if it is a checked exception.
    • closeAndWrapExceptions

      public static void closeAndWrapExceptions(@Nullable Closeable closeable)
      Like Closeable.close() but wraps IOExceptions in RuntimeExceptions.
    • closeAndSuppressExceptions

      public static void closeAndSuppressExceptions(@Nullable Closeable closeable, Consumer<Throwable> chomper)
      Like Closeable.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.