|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.common.io.Closer
@Beta public final class Closer
A Closeable
that collects Closeable
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 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:
Closeable
resource that is successfully registered will be closed later.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.Throwable.addSuppressed(Throwable)
.
Method Summary | ||
---|---|---|
void |
close()
Closes all Closeable instances that have been added to this Closer . |
|
static Closer |
create()
Creates a new Closer . |
|
|
register(C closeable)
Registers the given closeable to be closed when this Closer is
closed. |
|
java.lang.RuntimeException |
rethrow(java.lang.Throwable e)
Stores the given throwable and rethrows it. |
|
|
rethrow(java.lang.Throwable e,
java.lang.Class<X> declaredType)
Stores the given throwable and rethrows it. |
|
|
rethrow(java.lang.Throwable e,
java.lang.Class<X1> declaredType1,
java.lang.Class<X2> declaredType2)
Stores the given throwable and rethrows it. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static Closer create()
Closer
.
public <C extends java.io.Closeable> C register(C closeable)
closeable
to be closed when this Closer
is
closed.
closeable
public java.lang.RuntimeException rethrow(java.lang.Throwable e) throws java.io.IOException
IOException
, RuntimeException
or Error
. Otherwise, it will be rethrown
wrapped in a RuntimeException
. 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.
java.io.IOException
- when the given throwable is an IOExceptionpublic <X extends java.lang.Exception> java.lang.RuntimeException rethrow(java.lang.Throwable e, java.lang.Class<X> declaredType) throws java.io.IOException, X extends java.lang.Exception
IOException
, RuntimeException
, Error
or a checked exception of the
given type. Otherwise, it will be rethrown wrapped in a RuntimeException
. 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.
java.io.IOException
- when the given throwable is an IOException
X
- when the given throwable is of the declared type X
X extends java.lang.Exception
public <X1 extends java.lang.Exception,X2 extends java.lang.Exception> java.lang.RuntimeException rethrow(java.lang.Throwable e, java.lang.Class<X1> declaredType1, java.lang.Class<X2> declaredType2) throws java.io.IOException, X1 extends java.lang.Exception, X2 extends java.lang.Exception
IOException
, RuntimeException
, Error
or a checked exception of either
of the given types. Otherwise, it will be rethrown wrapped in a RuntimeException
.
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.
java.io.IOException
- when the given throwable is an IOException
X1
- when the given throwable is of the declared type X1
X2
- when the given throwable is of the declared type X2
X1 extends java.lang.Exception
public void close() throws java.io.IOException
Closeable
instances that have been added to this Closer
. If an
exception was thrown in the try block and passed to one of the exceptionThrown
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.
close
in interface java.io.Closeable
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |