Class KiwiCloseables

java.lang.Object
org.kiwiproject.beta.base.KiwiCloseables

@Beta public final class KiwiCloseables extends Object
Static utilities related to Closeable and other objects that can be "closed", whether they implement Closeable or not.
  • Method Details

    • closeAllQuietly

      public static void closeAllQuietly(Object... objects)
      Close all the given objects, suppressing any exceptions. Suppressed exceptions are logged at WARN level.

      Before using this method, consider whether it is safe to ignore exceptions thrown while closing an I/O resource. It might be safe when using "input" objects such as when reading a file or an input stream, but it generally is not safe when writing a file or to an output stream. Consider using closeAll(Object...).

      The objects may be instances of KiwiCloseables.CloseDescriptor to provide a custom name for the "close" method. Otherwise, each object is assumed to use the default method name "close".

      Parameters:
      objects - the objects to close
      See Also:
    • closeQuietly

      public static void closeQuietly(KiwiCloseables.CloseDescriptor descriptor)
      Close an object described by the KiwiCloseables.CloseDescriptor, and suppress any exception. Suppressed exceptions are logged at WARN level.

      Before using this method, consider whether it is safe to ignore exceptions thrown while closing an I/O resource. It might be safe when using "input" objects such as when reading a file or an input stream, but it generally is not safe when writing a file or to an output stream. Consider using close(CloseDescriptor).

      If the KiwiCloseables.CloseDescriptor refers to a non-public method, this method will attempt to invoke it. But this may result in an IllegalAccessException.

      Parameters:
      descriptor - the description of an object and its "close" method
      See Also:
    • closeQuietly

      public static void closeQuietly(@Nullable Closeable closeable)
      Close the given instance of Closeable, and suppress any exception. Suppressed exceptions are logged at WARN level.

      Before using this method, consider whether it is safe to ignore exceptions thrown while closing an I/O resource. It might be safe when using "input" objects such as when reading a file or an input stream, but it generally is not safe when writing a file or to an output stream. Consider using close(Closeable).

      Parameters:
      closeable - the Closeable to close
      See Also:
    • closeAll

      public static List<KiwiCloseables.CloseResult> closeAll(Object... objects)
      Close all the given objects, and return a single KiwiCloseables.CloseResult corresponding to each input object in order.

      The objects may be instances of KiwiCloseables.CloseDescriptor to provide a custom name for the "close" method. Otherwise, each object is assumed to use the default method name "close".

      Parameters:
      objects - the objects to close
      Returns:
      a list of KiwiCloseables.CloseResult containing the result for each attempted close operation
    • close

      Close an object described by the KiwiCloseables.CloseDescriptor.

      If the KiwiCloseables.CloseDescriptor refers to a non-public method, this method will attempt to invoke it. But this may result in an IllegalAccessException.

      Parameters:
      descriptor - the description of an object and its "close" method
      Returns:
      the KiwiCloseables.CloseResult
    • close

      public static KiwiCloseables.CloseResult close(Closeable closeable)
      Close the given instance of Closeable.
      Parameters:
      closeable - the Closeable to close
      Returns:
      the KiwiCloseables.CloseResult