Class IoTools
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic 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 -
Method Summary
Modifier and TypeMethodDescriptionstatic <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>Wraps the designatedAutoCloseablein an unconditionalAutoCloseablethat logs any exception thrown byAutoCloseable.close().static <C extends AutoCloseable>
IoTools.Quiet<C>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>Wraps the designatedAutoCloseablein a conditionalAutoCloseablethat in turn wraps any checked exceptions thrown byAutoCloseable.close()in aRuntimeException.
-
Constructor Details
-
IoTools
public IoTools()
-
-
Method Details
-
close
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
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
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
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
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:
-
closeLogged
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
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
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
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
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:
-
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:
-
unchecked
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:
-
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:
-