saker.build Documentation TaskDoc JavaDoc Packages
public class ResourceCloser implements Closeable
Utility class for holding resources to be closed later.

This class collects AutoCloseable instances to be closed later. This class is useful when users need to keep track of unclosed resources and close them all later.

The closeables added to a closer will be closed in reverse order. I.e. the last one added will be closed first.

This class is thread-safe. Closeable can be added concurrently, and the class can be closed concurrently, multiple times.

For each closeable added to this class they will be closed exactly once when close() is called.

It is recommended that instances are used with try-with-resources statement.

Constructors
public
Creates a new instance without any enclosed closeables.
public
ResourceCloser(Iterable<extends AutoCloseable> closeables)
Creates a new instance initialized with the given closeables.
public
Creates a new instance initialized with the given closeables.
Methods
public void
add(Iterable<extends AutoCloseable> closeables)
Adds the closeables to this resource closer.
public void
add(AutoCloseable... closeables)
Adds the closeables to this resource closer.
public void
Clears any closeables in this resource closer without actually closing the contained closeables.
public void
Closes this stream and releases any system resources associated with it.
Creates a new instance without any enclosed closeables.
public ResourceCloser(Iterable<extends AutoCloseable> closeables)
Creates a new instance initialized with the given closeables.
closeablesThe closeables.
public ResourceCloser(AutoCloseable... closeables)
Creates a new instance initialized with the given closeables.
closeablesThe closeables.
public void add(Iterable<extends AutoCloseable> closeables)
Adds the closeables to this resource closer.
closeablesThe closeables.
public void add(AutoCloseable... closeables)
Adds the closeables to this resource closer.
closeablesThe closeables.
public void clearWithoutClosing()
Clears any closeables in this resource closer without actually closing the contained closeables.
public void close() throws IOException
Closes this stream and releases any system resources associated with it.If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable.close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Any exceptions thrown by the added closeables will be rethrown as IOException instances.

IOExceptionif an I/O error occurs