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

public final Class Closeables

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Imports
com.google.common.annotations.GwtIncompatible, .J2ktIncompatible, .VisibleForTesting, java.io.Closeable, .IOException, .InputStream, .Reader, java.util.logging.Level, .Logger, javax.annotation.CheckForNull

Utility methods for working with Closeable objects.
Author
Michael Lancaster
Since
1.0

Field Summary

Modifier and TypeField and Description
pack-priv static final Logger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
close(Closeable
the Closeable object to be closed, or null, in which case this method does nothing
closeable
,
boolean
if true, don't propagate IO exceptions thrown by the close methods
swallowIOException
)

Closes a Closeable, with control over whether an IOException may be thrown.

public static void
closeQuietly(InputStream
the input stream to be closed, or null in which case this method does nothing
inputStream
)

Closes the given InputStream, logging any IOException that's thrown rather than propagating it.

public static void
closeQuietly(Reader
the reader to be closed, or null in which case this method does nothing
reader
)

Closes the given Reader, logging any IOException that's thrown rather than propagating it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
pack-priv static final Logger logger
Annotations
@VisibleForTesting

Constructor Detail

Closeablesback to summary
private Closeables()

Method Detail

closeback to summary
public static void close(Closeable closeable, boolean swallowIOException) throws IOException

Closes a Closeable, with control over whether an IOException may be thrown. This is primarily useful in a finally block, where a thrown exception needs to be logged but not propagated (otherwise the original exception will be lost).

If swallowIOException is true then we never throw IOException but merely log it.

Example:

public void useStreamNicely() throws IOException {
  SomeStream stream = new SomeStream("foo");
  boolean threw = true;
  try {
    // ... code which does something with the stream ...
    threw = false;
  } finally {
    // If an exception occurs, rethrow it only if threw==false:
    Closeables.close(stream, threw);
  }
}
Parameters
closeable:Closeable

the Closeable object to be closed, or null, in which case this method does nothing

swallowIOException:boolean

if true, don't propagate IO exceptions thrown by the close methods

Exceptions
IOException:
if swallowIOException is false and close throws an IOException.
closeQuietlyback to summary
public static void closeQuietly(InputStream inputStream)

Closes the given InputStream, logging any IOException that's thrown rather than propagating it.

While it's not safe in the general case to ignore exceptions that are thrown when closing an I/O resource, it should generally be safe in the case of a resource that's being used only for reading, such as an InputStream. Unlike with writable resources, there's no chance that a failure that occurs when closing the stream indicates a meaningful problem such as a failure to flush all bytes to the underlying resource.

Parameters
inputStream:InputStream

the input stream to be closed, or null in which case this method does nothing

Since
17.0
closeQuietlyback to summary
public static void closeQuietly(Reader reader)

Closes the given Reader, logging any IOException that's thrown rather than propagating it.

While it's not safe in the general case to ignore exceptions that are thrown when closing an I/O resource, it should generally be safe in the case of a resource that's being used only for reading, such as a Reader. Unlike with writable resources, there's no chance that a failure that occurs when closing the reader indicates a meaningful problem such as a failure to flush all bytes to the underlying resource.

Parameters
reader:Reader

the reader to be closed, or null in which case this method does nothing

Since
17.0