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 class
IoTools.Quiet<C extends AutoCloseable>
This class wraps anAutoCloseable
and logs the exception from closing.static class
IoTools.Unchecked<C extends AutoCloseable>
This class wraps anAutoCloseable
and wraps the exception from closing in aRuntimeException
if 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 designatedCloseable
and returnsnull
.static void
close(AutoCloseable... closeables)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed
.static void
close(Collection<? extends Object> objects)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed
.static void
closeAuto(AutoCloseable... closeables)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed
.static void
closeAuto(Collection<? extends Object> objects)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed
.static void
closeLogged(Logger logger, AutoCloseable... closeables)
Closes theAutoCloseable
s in the designated objects logging all exceptions asLevel.INFO
to the designated logger.static void
closeLogged(Logger logger, Collection<? extends Object> objects)
Closes theAutoCloseable
s in the designated objects logging all exceptions asLevel.INFO
to the designated logger.static void
closeUnchecked(AutoCloseable... closeables)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until the end, where the first exception or error will be thrown with all subsequent exceptions or errors beingsuppressed
.static void
closeUnchecked(Collection<? extends Object> objects)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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 designatedAutoCloseable
in anAutoCloseable
that logs any exception thrown byAutoCloseable.close()
.static <C extends AutoCloseable>
IoTools.Unchecked<C>unchecked(C closeable)
Wraps the designatedAutoCloseable
in anAutoCloseable
that 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 designatedCloseable
and returnsnull
. This way, aCloseable
can 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
- theCloseable
to be closed- Returns:
- Always
null
. - Throws:
IOException
- ifCloseable.close()
throws an exception
-
close
public static void close(AutoCloseable... closeables) throws IOException
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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
- TheAutoCloseables
to be closed.- Throws:
IOException
- The first occurredIOException
having all following exceptions chained.RuntimeException
- The first occurredRuntimeException
having all following exceptions chained.- See Also:
close(Collection)
-
close
public static void close(Collection<? extends Object> objects) throws IOException
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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
- TheAutoCloseables
to be closed. Other kinds of objects will just be ignored. This may benull
.- Throws:
IOException
- The first occurredIOException
having all following exceptions chained.RuntimeException
- The first occurredRuntimeException
having all following exceptions chained.
-
closeUnchecked
public static void closeUnchecked(AutoCloseable... closeables)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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
- TheAutoCloseables
to be closed while accumulating occurringException
s and rethrowing them in aRuntimeException
.- See Also:
closeUnchecked(Collection)
-
closeUnchecked
public static void closeUnchecked(Collection<? extends Object> objects)
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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 beingAutoCloseable
to be closed while accumulating occurringException
s and rethrowing them in aRuntimeException
.- See Also:
close(Collection)
-
closeLogged
public static void closeLogged(Logger logger, AutoCloseable... closeables)
Closes theAutoCloseable
s in the designated objects logging all exceptions asLevel.INFO
to the designated logger. The objects will be identified in the log message via theirObject.toString()
.In case of
Errors
it will still be attempted to close allAutoCloseables
anyway. 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 occurredIOException
s andRuntimeException
s.closeables
- TheAutoCloseables
to be closed while logging occurring exceptions.- See Also:
closeLogged(Logger, Collection)
-
closeLogged
public static void closeLogged(Logger logger, Collection<? extends Object> objects)
Closes theAutoCloseable
s in the designated objects logging all exceptions asLevel.INFO
to the designated logger. The objects will be identified in the log message via theirObject.toString()
.In case of
Errors
it will still be attempted to close allAutoCloseables
anyway. 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 occurredIOException
s andRuntimeException
s.objects
- The objects possibly beingAutoCloseable
to be closed while logging occurring exceptions.
-
closeAuto
public static void closeAuto(AutoCloseable... closeables) throws Exception
Closes theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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
- TheAutoCloseables
to be closed while accumulating occurringException
s andError
s.- 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 theAutoCloseable
s in the designated objects, delaying any encounteredExceptions
orErrors
until 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 designatedAutoCloseable
in anAutoCloseable
that 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:
IoTools#close(Logger, AutoCloseable...)
-
unchecked
public static <C extends AutoCloseable> IoTools.Unchecked<C> unchecked(C closeable)
Wraps the designatedAutoCloseable
in anAutoCloseable
that 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...)
-
-