Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2007 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.io;
     18 
     19 import java.io.Closeable;
     20 import java.io.IOException;
     21 import java.util.logging.Level;
     22 import java.util.logging.Logger;
     23 
     24 import javax.annotation.Nullable;
     25 
     26 /**
     27  * Utility methods for working with {@link Closeable} objects.
     28  *
     29  * @author Michael Lancaster
     30  * @since 2009.09.15 <b>tentative</b>
     31  */
     32 public final class Closeables {
     33   private static final Logger logger
     34       = Logger.getLogger(Closeables.class.getName());
     35 
     36   private Closeables() {}
     37 
     38   /**
     39    * Closes a {@link Closeable}, with control over whether an
     40    * {@code IOException} may be thrown. This is primarily useful in a
     41    * finally block, where a thrown exception needs to be logged but not
     42    * propagated (otherwise the original exception will be lost).
     43    *
     44    * <p>If {@code swallowIOException} is true then we never throw
     45    * {@code IOException} but merely log it.
     46    *
     47    * <p>Example:
     48    *
     49    * <p><pre>public void useStreamNicely() throws IOException {
     50    * SomeStream stream = new SomeStream("foo");
     51    * boolean threw = true;
     52    * try {
     53    *   // Some code which does something with the Stream. May throw a
     54    *   // Throwable.
     55    *   threw = false; // No throwable thrown.
     56    * } finally {
     57    *   // Close the stream.
     58    *   // If an exception occurs, only rethrow it if (threw==false).
     59    *   Closeables.close(stream, threw);
     60    * }
     61    * </pre>
     62    *
     63    * @param closeable the {@code Closeable} object to be closed, or null,
     64    *     in which case this method does nothing
     65    * @param swallowIOException if true, don't propagate IO exceptions
     66    *     thrown by the {@code close} methods
     67    * @throws IOException if {@code swallowIOException} is false and
     68    *     {@code close} throws an {@code IOException}.
     69    */
     70   public static void close(@Nullable Closeable closeable,
     71       boolean swallowIOException) throws IOException {
     72     if (closeable == null) {
     73       return;
     74     }
     75     try {
     76       closeable.close();
     77     } catch (IOException e) {
     78       if (swallowIOException) {
     79         logger.log(Level.WARNING,
     80             "IOException thrown while closing Closeable.", e);
     81       } else {
     82         throw e;
     83       }
     84     }
     85   }
     86 
     87   /**
     88    * Equivalent to calling {@code close(closeable, true)}, but with no
     89    * IOException in the signature.
     90    * @param closeable the {@code Closeable} object to be closed, or null, in
     91    *      which case this method does nothing
     92    */
     93   public static void closeQuietly(@Nullable Closeable closeable) {
     94     try {
     95       close(closeable, true);
     96     } catch (IOException e) {
     97       logger.log(Level.SEVERE, "IOException should not have been thrown.", e);
     98     }
     99   }
    100 }
    101