Class IoTools


  • public class IoTools
    extends Object
    A class providing several IO-related tool methods.
    • Constructor Detail

      • IoTools

        public IoTools()
    • Method Detail

      • close

        public static <C extends Closeable> C close​(C closeable)
                                             throws IOException
        Closes the designated Closeable and returns null. This way, a Closeable can be closed and removed in one step preventing the field to be closed again.

        Usually you only need this for implementing Closeable.close() or Cleanup.

        Type Parameters:
        C - The type of closeable.
        Parameters:
        closeable - the Closeable to be closed
        Returns:
        Always null.
        Throws:
        IOException - if Closeable.close() throws an exception
      • close

        public static void close​(Collection<? extends Object> objects)
                          throws IOException
        Closes the AutoCloseables in the designated objects, delaying any encountered Exceptions or Errors until the end, where the first exception or error will be thrown with all subsequent exceptions or errors being suppressed. If the first exception is a checked exception other than IOException, it will be wrapped in one.

        Usually you only need this for implementing AutoCloseable.close() or Cleanup.

        Parameters:
        objects - The AutoCloseables to be closed. Other kinds of objects will just be ignored. This may be null.
        Throws:
        IOException - The first occurred IOException having all following exceptions chained.
        RuntimeException - The first occurred RuntimeException having all following exceptions chained.
      • closeUnchecked

        public static void closeUnchecked​(Collection<? extends Object> objects)
        Closes the AutoCloseables in the designated objects, delaying any encountered Exceptions or Errors until the end, where the first exception or error will be thrown with all subsequent exceptions or errors being suppressed. If the first exception is a checked exception, it will be wrapped in an RuntimeException.

        Usually you only need this for implementing AutoCloseable.close() or Cleanup.

        Parameters:
        objects - The objects possibly being AutoCloseable to be closed while accumulating occurring Exceptions and rethrowing them in a RuntimeException.
        See Also:
        close(Collection)
      • closeLogged

        public static void closeLogged​(Logger logger,
                                       Collection<? extends Object> objects)
        Closes the AutoCloseables in the designated objects logging all exceptions as Level.INFO to the designated logger. The objects will be identified in the log message via their Object.toString().

        In case of Errors it will still be attempted to close all AutoCloseables anyway. But otherwise the first error will be rethrown at the end and subsequent errors will be suppressed.

        Usually you only need this for implementing AutoCloseable.close() or Cleanup.

        Parameters:
        logger - The logger to which to log all occurred IOExceptions and RuntimeExceptions.
        objects - The objects possibly being AutoCloseable to be closed while logging occurring exceptions.
      • closeAuto

        public static void closeAuto​(Collection<? extends Object> objects)
                              throws Exception
        Closes the AutoCloseables in the designated objects, delaying any encountered Exceptions or Errors until the end, where the first exception or error will be thrown with all subsequent exceptions or errors being suppressed.

        Usually you only need this for implementing AutoCloseable.close() or Cleanup.

        Parameters:
        objects - The objects possibly being AutoCloseable to be closed while accumulating occurring Exceptions. This may be null.
        Throws:
        Exception - The first occurred exception having all following chained.
      • quiet

        public static <C extends AutoCloseableIoTools.Quiet<C> quiet​(Logger logger,
                                                                       C closeable)
        Wraps the designated AutoCloseable in an AutoCloseable that logs any exception thrown by AutoCloseable.close(). This allows for usage in a try-with-resources.
        Type Parameters:
        C - The type of the closeable which to close quietly.
        Parameters:
        logger - The logger to which to log an occurred exception.
        closeable - The closeable which to close quietly.
        Returns:
        A wrapper for closing the designated closeable quietly.
        See Also:
        IoTools#close(Logger, AutoCloseable...)