Class Closer
- java.lang.Object
-
- com.google.common.io.Closer
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
@Beta @GwtIncompatible public final class Closer extends Object implements Closeable
ACloseable
that collectsCloseable
resources and closes them all when it is closed. This is intended to approximately emulate the behavior of Java 7's try-with-resources statement in JDK6-compatible code. Running on Java 7, code using this should be approximately equivalent in behavior to the same code written with try-with-resources. Running on Java 6, exceptions that cannot be thrown must be logged rather than being added to the thrown exception as a suppressed exception.This class is intended to be used in the following pattern:
Closer closer = Closer.create(); try { InputStream in = closer.register(openInputStream()); OutputStream out = closer.register(openOutputStream()); // do stuff } catch (Throwable e) { // ensure that any checked exception types other than IOException that could be thrown are // provided here, e.g. throw closer.rethrow(e, CheckedException.class); throw closer.rethrow(e); } finally { closer.close(); }
Note that this try-catch-finally block is not equivalent to a try-catch-finally block using try-with-resources. To get the equivalent of that, you must wrap the above code in another try block in order to catch any exception that may be thrown (including from the call to
close()
).This pattern ensures the following:
- Each
Closeable
resource that is successfully registered will be closed later. - If a
Throwable
is thrown in the try block, no exceptions that occur when attempting to close resources will be thrown from the finally block. The throwable from the try block will be thrown. - If no exceptions or errors were thrown in the try block, the first exception thrown by an attempt to close a resource will be thrown.
- Any exception caught when attempting to close a resource that is not thrown (because another exception is already being thrown) is suppressed.
An exception that is suppressed is not thrown. The method of suppression used depends on the version of Java the code is running on:
- Java 7+: Exceptions are suppressed by adding them to the exception that will
be thrown using
Throwable.addSuppressed(Throwable)
. - Java 6: Exceptions are suppressed by logging them instead.
- Since:
- 14.0
- Author:
- Colin Decker
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes allCloseable
instances that have been added to thisCloser
.static Closer
create()
Creates a newCloser
.<C extends @Nullable Closeable>
Cregister(C closeable)
RuntimeException
rethrow(Throwable e)
Stores the given throwable and rethrows it.<X extends Exception>
RuntimeExceptionrethrow(Throwable e, Class<X> declaredType)
Stores the given throwable and rethrows it.<X1 extends Exception,X2 extends Exception>
RuntimeExceptionrethrow(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2)
Stores the given throwable and rethrows it.
-
-
-
Method Detail
-
register
@CanIgnoreReturnValue public <C extends @Nullable Closeable> C register(C closeable)
- Returns:
- the given
closeable
-
rethrow
public RuntimeException rethrow(Throwable e) throws IOException
Stores the given throwable and rethrows it. It will be rethrown as is if it is anIOException
,RuntimeException
orError
. Otherwise, it will be rethrown wrapped in aRuntimeException
. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.This method always throws, and as such should be called as
throw closer.rethrow(e);
to ensure the compiler knows that it will throw.- Returns:
- this method does not return; it always throws
- Throws:
IOException
- when the given throwable is an IOException
-
rethrow
public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, X extends Exception
Stores the given throwable and rethrows it. It will be rethrown as is if it is anIOException
,RuntimeException
,Error
or a checked exception of the given type. Otherwise, it will be rethrown wrapped in aRuntimeException
. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.This method always throws, and as such should be called as
throw closer.rethrow(e, ...);
to ensure the compiler knows that it will throw.- Returns:
- this method does not return; it always throws
- Throws:
IOException
- when the given throwable is an IOExceptionX
- when the given throwable is of the declared type XX extends Exception
-
rethrow
public <X1 extends Exception,X2 extends Exception> RuntimeException rethrow(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1 extends Exception, X2 extends Exception
Stores the given throwable and rethrows it. It will be rethrown as is if it is anIOException
,RuntimeException
,Error
or a checked exception of either of the given types. Otherwise, it will be rethrown wrapped in aRuntimeException
. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.This method always throws, and as such should be called as
throw closer.rethrow(e, ...);
to ensure the compiler knows that it will throw.- Returns:
- this method does not return; it always throws
- Throws:
IOException
- when the given throwable is an IOExceptionX1
- when the given throwable is of the declared type X1X2
- when the given throwable is of the declared type X2X1 extends Exception
-
close
public void close() throws IOException
Closes allCloseable
instances that have been added to thisCloser
. If an exception was thrown in the try block and passed to one of theexceptionThrown
methods, any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the first exception to be thrown from an attempt to close a closeable will be thrown and any additional exceptions that are thrown after that will be suppressed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if an I/O error occurs
-
-