Class IoTools
- java.lang.Object
-
- de.aristaflow.adept2.util.io.IoTools
-
public class IoTools extends Object
A class providing several IO-related tool methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIoTools.Quiet<C extends AutoCloseable>This class wraps anAutoCloseableand logs the exception from closing.static classIoTools.Unchecked<C extends AutoCloseable>This class wraps anAutoCloseableand wraps the exception from closing in aRuntimeExceptionif necessary.
-
Constructor Summary
Constructors Constructor Description IoTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <C extends Closeable>
Cclose(C closeable)Closes the designatedCloseableand returnsnull.static voidclose(AutoCloseable... closeables)Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.static voidclose(Collection<? extends Object> objects)Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.static voidcloseAuto(AutoCloseable... closeables)Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.static voidcloseAuto(Collection<? extends Object> objects)Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.static voidcloseLogged(Logger logger, AutoCloseable... closeables)Closes theAutoCloseables in the designated objects logging all exceptions asLevel.INFOto the designated logger.static voidcloseLogged(Logger logger, Collection<? extends Object> objects)Closes theAutoCloseables in the designated objects logging all exceptions asLevel.INFOto the designated logger.static voidcloseUnchecked(AutoCloseable... closeables)Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.static voidcloseUnchecked(Collection<? extends Object> objects)Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.static <C extends AutoCloseable>
IoTools.Quiet<C>quiet(Logger logger, C closeable)Wraps the designatedAutoCloseablein an unconditionalAutoCloseablethat logs any exception thrown byAutoCloseable.close().static <C extends AutoCloseable>
IoTools.Quiet<C>quiet(Logger logger, Supplier<Boolean> condition, C closeable)Wraps the designatedAutoCloseablein a conditionalAutoCloseablethat logs any exception thrown byAutoCloseable.close().static <C extends AutoCloseable>
IoTools.Unchecked<C>unchecked(C closeable)Wraps the designatedAutoCloseablein an unconditionalAutoCloseablethat in turn wraps any checked exceptions thrown byAutoCloseable.close()in aRuntimeException.static <C extends AutoCloseable>
IoTools.Unchecked<C>unchecked(Supplier<Boolean> condition, C closeable)Wraps the designatedAutoCloseablein a conditionalAutoCloseablethat in turn wraps any checked exceptions thrown byAutoCloseable.close()in aRuntimeException.
-
-
-
Method Detail
-
close
public static <C extends Closeable> C close(C closeable) throws IOException
Closes the designatedCloseableand returnsnull. This way, aCloseablecan be closed and removed in one step preventing the field to be closed again.Usually you only need this for implementing
Closeable.close()orCleanup.- Type Parameters:
C- The type of closeable.- Parameters:
closeable- theCloseableto be closed- Returns:
- Always
null. - Throws:
IOException- ifCloseable.close()throws an exception
-
close
public static void close(AutoCloseable... closeables) throws IOException
Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed. If the first exception is a checked exception other thanIOException, it will be wrapped in one.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
closeables- TheAutoCloseablesto be closed.- Throws:
IOException- The first occurredIOExceptionhaving all following exceptions chained.RuntimeException- The first occurredRuntimeExceptionhaving all following exceptions chained.- See Also:
close(Collection)
-
close
public static void close(Collection<? extends Object> objects) throws IOException
Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed. If the first exception is a checked exception other thanIOException, it will be wrapped in one.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
objects- TheAutoCloseablesto be closed. Other kinds of objects will just be ignored. This may benull.- Throws:
IOException- The first occurredIOExceptionhaving all following exceptions chained.RuntimeException- The first occurredRuntimeExceptionhaving all following exceptions chained.
-
closeUnchecked
public static void closeUnchecked(AutoCloseable... closeables)
Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed. If the first exception is a checked exception, it will be wrapped in anRuntimeException.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
closeables- TheAutoCloseablesto be closed while accumulating occurringExceptions and rethrowing them in aRuntimeException.- See Also:
closeUnchecked(Collection)
-
closeUnchecked
public static void closeUnchecked(Collection<? extends Object> objects)
Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed. If the first exception is a checked exception, it will be wrapped in anRuntimeException.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
objects- The objects possibly beingAutoCloseableto be closed while accumulating occurringExceptions and rethrowing them in aRuntimeException.- See Also:
close(Collection)
-
closeLogged
public static void closeLogged(Logger logger, AutoCloseable... closeables)
Closes theAutoCloseables in the designated objects logging all exceptions asLevel.INFOto the designated logger. The objects will be identified in the log message via theirObject.toString().In case of
Errorsit will still be attempted to close allAutoCloseablesanyway. But otherwise the first error will be rethrown at the end and subsequent errors will besuppressed.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
logger- The logger to which to log all occurredIOExceptions andRuntimeExceptions.closeables- TheAutoCloseablesto be closed while logging occurring exceptions.- See Also:
closeLogged(Logger, Collection)
-
closeLogged
public static void closeLogged(Logger logger, Collection<? extends Object> objects)
Closes theAutoCloseables in the designated objects logging all exceptions asLevel.INFOto the designated logger. The objects will be identified in the log message via theirObject.toString().In case of
Errorsit will still be attempted to close allAutoCloseablesanyway. But otherwise the first error will be rethrown at the end and subsequent errors will besuppressed.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
logger- The logger to which to log all occurredIOExceptions andRuntimeExceptions.objects- The objects possibly beingAutoCloseableto be closed while logging occurring exceptions.
-
closeAuto
public static void closeAuto(AutoCloseable... closeables) throws Exception
Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.Usually you only need this for implementing
AutoCloseable.close()orCleanup.- Parameters:
closeables- TheAutoCloseablesto be closed while accumulating occurringExceptions andErrors.- Throws:
Exception- The first occurred exception having all following chained.- See Also:
closeAuto(Collection)
-
closeAuto
public static void closeAuto(Collection<? extends Object> objects) throws Exception
Closes theAutoCloseables in the designated objects, delaying any encounteredExceptionsorErrorsuntil the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed.Usually you only need this for implementing
AutoCloseable.close()orCleanup.
-
quiet
public static <C extends AutoCloseable> IoTools.Quiet<C> quiet(Logger logger, C closeable)
Wraps the designatedAutoCloseablein an unconditionalAutoCloseablethat logs any exception thrown byAutoCloseable.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:
closeLogged(Logger, AutoCloseable...)
-
quiet
public static <C extends AutoCloseable> IoTools.Quiet<C> quiet(Logger logger, Supplier<Boolean> condition, C closeable)
Wraps the designatedAutoCloseablein a conditionalAutoCloseablethat logs any exception thrown byAutoCloseable.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.condition- The optional condition allowing for conditional closing ornullfor unconditional closing.closeable- The closeable which to close quietly.- Returns:
- A wrapper for closing the designated closeable quietly.
- See Also:
closeLogged(Logger, AutoCloseable...)
-
unchecked
public static <C extends AutoCloseable> IoTools.Unchecked<C> unchecked(C closeable)
Wraps the designatedAutoCloseablein an unconditionalAutoCloseablethat in turn wraps any checked exceptions thrown byAutoCloseable.close()in aRuntimeException. This allows for usage in a try-with-resources.- Type Parameters:
C- The type of the closeable which to close quietly.- Parameters:
closeable- The closeable which to close quietly.- Returns:
- A wrapper for closing the designated closeable quietly.
- See Also:
closeUnchecked(AutoCloseable...)
-
unchecked
public static <C extends AutoCloseable> IoTools.Unchecked<C> unchecked(Supplier<Boolean> condition, C closeable)
Wraps the designatedAutoCloseablein a conditionalAutoCloseablethat in turn wraps any checked exceptions thrown byAutoCloseable.close()in aRuntimeException. This allows for usage in a try-with-resources.- Type Parameters:
C- The type of the closeable which to close quietly.- Parameters:
condition- The optional condition allowing for conditional closing ornullfor unconditional closing.closeable- The closeable which to close quietly.- Returns:
- A wrapper for closing the designated closeable quietly.
- See Also:
closeUnchecked(AutoCloseable...)
-
-