public class CloseableHolder<T extends Closeable> extends Object implements Closeable
For example, in the following code, if writeSomethingToTempFile() throws an exception, the tempFile will be deleted, but otherwise the ownership is passed to doSomethingAsyncWithTempFile().
try (CloseableHolder<NamedTemporaryFile> tempFile = new CloseableHolder<>(createTempFile())) {
writeSomethingToTempFile(tempFile.get());
doSomethingAsyncWithTempFile(tempFile.release());
}
Constructor and Description |
---|
CloseableHolder(T value) |
public CloseableHolder(T value)
public T get()
public T release()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException