Class Closeables
- java.lang.Object
-
- com.google.common.io.Closeables
-
@GwtIncompatible public final class Closeables extends java.lang.Object
Utility methods for working withCloseableobjects.- Since:
- 1.0
- Author:
- Michael Lancaster
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclose(java.io.Closeable closeable, boolean swallowIOException)Closes aCloseable, with control over whether anIOExceptionmay be thrown.static voidcloseQuietly(java.io.InputStream inputStream)Closes the givenInputStream, logging anyIOExceptionthat's thrown rather than propagating it.static voidcloseQuietly(java.io.Reader reader)Closes the givenReader, logging anyIOExceptionthat's thrown rather than propagating it.
-
-
-
Method Detail
-
close
public static void close(@CheckForNull java.io.Closeable closeable, boolean swallowIOException) throws java.io.IOException
Closes aCloseable, with control over whether anIOExceptionmay 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
swallowIOExceptionis true then we never throwIOExceptionbut 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- theCloseableobject to be closed, or null, in which case this method does nothingswallowIOException- if true, don't propagate IO exceptions thrown by theclosemethods- Throws:
java.io.IOException- ifswallowIOExceptionis false andclosethrows anIOException.
-
closeQuietly
public static void closeQuietly(@CheckForNull java.io.InputStream inputStream)
Closes the givenInputStream, logging anyIOExceptionthat'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- the input stream to be closed, ornullin which case this method does nothing- Since:
- 17.0
-
closeQuietly
public static void closeQuietly(@CheckForNull java.io.Reader reader)
Closes the givenReader, logging anyIOExceptionthat'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- the reader to be closed, ornullin which case this method does nothing- Since:
- 17.0
-
-