1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.io; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import com.google.common.annotations.J2ktIncompatible; 19 import com.google.common.annotations.VisibleForTesting; 20 import java.io.Closeable; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.Reader; 24 import java.util.logging.Level; 25 import java.util.logging.Logger; 26 import javax.annotation.CheckForNull; 27 28 /** 29 * Utility methods for working with {@link Closeable} objects. 30 * 31 * @author Michael Lancaster 32 * @since 1.0 33 */ 34 @J2ktIncompatible 35 @GwtIncompatible 36 @ElementTypesAreNonnullByDefault 37 public final class Closeables { 38 @VisibleForTesting static final Logger logger = Logger.getLogger(Closeables.class.getName()); 39 Closeables()40 private Closeables() {} 41 42 /** 43 * Closes a {@link Closeable}, with control over whether an {@code IOException} may be thrown. 44 * This is primarily useful in a finally block, where a thrown exception needs to be logged but 45 * not propagated (otherwise the original exception will be lost). 46 * 47 * <p>If {@code swallowIOException} is true then we never throw {@code IOException} but merely log 48 * it. 49 * 50 * <p>Example: 51 * 52 * <pre>{@code 53 * public void useStreamNicely() throws IOException { 54 * SomeStream stream = new SomeStream("foo"); 55 * boolean threw = true; 56 * try { 57 * // ... code which does something with the stream ... 58 * threw = false; 59 * } finally { 60 * // If an exception occurs, rethrow it only if threw==false: 61 * Closeables.close(stream, threw); 62 * } 63 * } 64 * }</pre> 65 * 66 * @param closeable the {@code Closeable} object to be closed, or null, in which case this method 67 * does nothing 68 * @param swallowIOException if true, don't propagate IO exceptions thrown by the {@code close} 69 * methods 70 * @throws IOException if {@code swallowIOException} is false and {@code close} throws an {@code 71 * IOException}. 72 */ close(@heckForNull Closeable closeable, boolean swallowIOException)73 public static void close(@CheckForNull Closeable closeable, boolean swallowIOException) 74 throws IOException { 75 if (closeable == null) { 76 return; 77 } 78 try { 79 closeable.close(); 80 } catch (IOException e) { 81 if (swallowIOException) { 82 logger.log(Level.WARNING, "IOException thrown while closing Closeable.", e); 83 } else { 84 throw e; 85 } 86 } 87 } 88 89 /** 90 * Closes the given {@link InputStream}, logging any {@code IOException} that's thrown rather than 91 * propagating it. 92 * 93 * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing an 94 * I/O resource, it should generally be safe in the case of a resource that's being used only for 95 * reading, such as an {@code InputStream}. Unlike with writable resources, there's no chance that 96 * a failure that occurs when closing the stream indicates a meaningful problem such as a failure 97 * to flush all bytes to the underlying resource. 98 * 99 * @param inputStream the input stream to be closed, or {@code null} in which case this method 100 * does nothing 101 * @since 17.0 102 */ closeQuietly(@heckForNull InputStream inputStream)103 public static void closeQuietly(@CheckForNull InputStream inputStream) { 104 try { 105 close(inputStream, true); 106 } catch (IOException impossible) { 107 throw new AssertionError(impossible); 108 } 109 } 110 111 /** 112 * Closes the given {@link Reader}, logging any {@code IOException} that's thrown rather than 113 * propagating it. 114 * 115 * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing an 116 * I/O resource, it should generally be safe in the case of a resource that's being used only for 117 * reading, such as a {@code Reader}. Unlike with writable resources, there's no chance that a 118 * failure that occurs when closing the reader indicates a meaningful problem such as a failure to 119 * flush all bytes to the underlying resource. 120 * 121 * @param reader the reader to be closed, or {@code null} in which case this method does nothing 122 * @since 17.0 123 */ closeQuietly(@heckForNull Reader reader)124 public static void closeQuietly(@CheckForNull Reader reader) { 125 try { 126 close(reader, true); 127 } catch (IOException impossible) { 128 throw new AssertionError(impossible); 129 } 130 } 131 } 132