Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.io;
 
 
 
 import  javax.annotation.Nullable;

Utility methods for working with Closeable objects.

Author(s):
Michael Lancaster
Since:
1.0
 
 public final class Closeables {
   @VisibleForTesting static final Logger logger
       = Logger.getLogger(Closeables.class.getName());
 
   private Closeables() {}

  
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 the Closeable object to be closed, or null, in which case this method does nothing
swallowIOException if true, don't propagate IO exceptions thrown by the close methods
Throws:
IOException if swallowIOException is false and close throws an IOException.
 
   public static void close(@Nullable Closeable closeable,
       boolean swallowIOExceptionthrows IOException {
     if (closeable == null) {
       return;
     }
     try {
       closeable.close();
     } catch (IOException e) {
       if (swallowIOException) {
         .log(.,
             "IOException thrown while closing Closeable."e);
       } else {
         throw e;
       }
     }
   }

  
Equivalent to calling close(closeable, true), but with no IOException in the signature.

Deprecated:
Where possible, use the try-with-resources statement if using JDK7 or Closer on JDK6 to close one or more Closeable objects. This method is deprecated because it is easy to misuse and may swallow IO exceptions that really should be thrown and handled. See Guava issue 1118 for a more detailed explanation of the reasons for deprecation and see Closing Resources for more information on the problems with closing Closeable objects and some of the preferred solutions for handling it correctly. This method is scheduled to be removed in Guava 16.0.
Parameters:
closeable the Closeable object to be closed, or null, in which case this method does nothing
  public static void closeQuietly(@Nullable Closeable closeable) {
    try {
      close(closeabletrue);
    } catch (IOException e) {
      .log(."IOException should not have been thrown."e);
    }
  }
New to GrepCode? Check out our FAQ X