Top Description Inners Fields Constructors Methods
com.google.common.io

public final Class Closer

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

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 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:

An exception that is suppressed is not thrown. The method of suppression used depends on the version of Java the code is running on:

Author
Colin Decker
Since
14.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Closer.LoggingSuppressor

Suppresses exceptions by logging them.

pack-priv static class
Closer.SuppressingSuppressor

Suppresses exceptions by adding them to the exception that will be thrown using JDK7's addSuppressed(Throwable) mechanism.

pack-priv static interface
Closer.Suppressor

Suppression strategy interface.

Field Summary

Modifier and TypeField and Description
private final Deque<Closeable>
private static final Closer.Suppressor
SUPPRESSOR

The suppressor implementation to use for the current Java version.

pack-priv final Closer.Suppressor
private Throwable

Constructor Summary

AccessConstructor and Description
pack-priv
Closer(Closer.Suppressor suppressor)

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.io.Closeable.close.

Closes all Closeable instances that have been added to this Closer.
public static Closer
create()

Creates a new Closer.

public <C extends @Nullable Closeable> C

Returns:

the given closeable
register
(C closeable)

Registers the given closeable to be closed when this Closer is closed.

public RuntimeException

Returns:

this method does not return; it always throws
rethrow
(Throwable e)

Stores the given throwable and rethrows it.

public <X extends Exception> RuntimeException

Returns:

this method does not return; it always throws
rethrow
(Throwable e, Class<X> declaredType)

Stores the given throwable and rethrows it.

public <X1 extends Exception, X2 extends Exception> RuntimeException

Returns:

this method does not return; it always throws
rethrow
(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2)

Stores the given throwable and rethrows it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

stackback to summary
private final Deque<Closeable> stack
SUPPRESSORback to summary
private static final Closer.Suppressor SUPPRESSOR

The suppressor implementation to use for the current Java version.

suppressorback to summary
pack-priv final Closer.Suppressor suppressor
Annotations
@VisibleForTesting
thrownback to summary
private Throwable thrown
Annotations
@CheckForNull

Constructor Detail

Closerback to summary
pack-priv Closer(Closer.Suppressor suppressor)
Annotations
@VisibleForTesting

Method Detail

closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Closes all 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.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.Closeable.close.

if an I/O error occurs

createback to summary
public static Closer create()

Creates a new Closer.

registerback to summary
public <C extends @Nullable Closeable> C register(C closeable)

Registers the given closeable to be closed when this Closer is closed.

Returns:C

the given closeable

Annotations
@CanIgnoreReturnValue
@ParametricNullness
rethrowback to summary
public RuntimeException rethrow(Throwable e) throws IOException

Stores the given throwable and rethrows it. It will be rethrown as is if it is an 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.

Returns:RuntimeException

this method does not return; it always throws

Exceptions
IOException:
when the given throwable is an IOException
rethrowback to summary
public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, Class-:X

Stores the given throwable and rethrows it. It will be rethrown as is if it is an 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.

Returns:RuntimeException

this method does not return; it always throws

Exceptions
IOException:
when the given throwable is an IOException
Class-:X:
when the given throwable is of the declared type X
rethrowback to summary
public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, Class-:X1, Class-:X2

Stores the given throwable and rethrows it. It will be rethrown as is if it is an 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.

Returns:RuntimeException

this method does not return; it always throws

Exceptions
IOException:
when the given throwable is an IOException
Class-:X1:
when the given throwable is of the declared type X1
Class-:X2:
when the given throwable is of the declared type X2
com.google.common.io back to summary

pack-priv final Class Closer.LoggingSuppressor

extends Object
implements Suppressor
Class Inheritance
All Implemented Interfaces
com.google.common.io.Closer.Suppressor
Annotations
@VisibleForTesting

Suppresses exceptions by logging them.

Field Summary

Modifier and TypeField and Description
pack-priv static final Closer.LoggingSuppressor

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
suppress(Closeable closeable, Throwable thrown, Throwable suppressed)

Implements com.google.common.io.Closer.Suppressor.suppress.

Suppresses the given exception (suppressed) which was thrown when attempting to close the given closeable.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final Closer.LoggingSuppressor INSTANCE

Constructor Detail

LoggingSuppressorback to summary
pack-priv LoggingSuppressor()

Method Detail

suppressback to summary
public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed)

Implements com.google.common.io.Closer.Suppressor.suppress.

Doc from com.google.common.io.Closer.Suppressor.suppress.

Suppresses the given exception (suppressed) which was thrown when attempting to close the given closeable. thrown is the exception that is actually being thrown from the method. Implementations of this method should not throw under any circumstances.

Annotations
@Override
com.google.common.io back to summary

pack-priv final Class Closer.SuppressingSuppressor

extends Object
implements Suppressor
Class Inheritance
All Implemented Interfaces
com.google.common.io.Closer.Suppressor
Annotations
@VisibleForTesting

Suppresses exceptions by adding them to the exception that will be thrown using JDK7's addSuppressed(Throwable) mechanism.

Field Summary

Modifier and TypeField and Description
private final Method

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
suppress(Closeable closeable, Throwable thrown, Throwable suppressed)

Implements com.google.common.io.Closer.Suppressor.suppress.

Suppresses the given exception (suppressed) which was thrown when attempting to close the given closeable.
pack-priv static Closer.SuppressingSuppressor
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addSuppressedback to summary
private final Method addSuppressed

Constructor Detail

SuppressingSuppressorback to summary
private SuppressingSuppressor(Method addSuppressed)

Method Detail

suppressback to summary
public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed)

Implements com.google.common.io.Closer.Suppressor.suppress.

Doc from com.google.common.io.Closer.Suppressor.suppress.

Suppresses the given exception (suppressed) which was thrown when attempting to close the given closeable. thrown is the exception that is actually being thrown from the method. Implementations of this method should not throw under any circumstances.

Annotations
@Override
tryCreateback to summary
pack-priv static Closer.SuppressingSuppressor tryCreate()
Annotations
@CheckForNull
com.google.common.io back to summary

pack-priv Interface Closer.Suppressor

Known Direct Implementers
com.google.common.io.Closer.LoggingSuppressor, com.google.common.io.Closer.SuppressingSuppressor
Annotations
@VisibleForTesting

Suppression strategy interface.

Method Summary

Modifier and TypeMethod and Description
public void
suppress(Closeable closeable, Throwable thrown, Throwable suppressed)

Suppresses the given exception (suppressed) which was thrown when attempting to close the given closeable.

Method Detail

suppressback to summary
public void suppress(Closeable closeable, Throwable thrown, Throwable suppressed)

Suppresses the given exception (suppressed) which was thrown when attempting to close the given closeable. thrown is the exception that is actually being thrown from the method. Implementations of this method should not throw under any circumstances.