org.apache.lucene.util
Class IOUtils

java.lang.Object
  extended by org.apache.lucene.util.IOUtils

public final class IOUtils
extends Object

This class emulates the new Java 7 "Try-With-Resources" statement. Remove once Lucene is on Java 7.

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

Method Summary
static void close(Closeable... objects)
          Closes all given Closeables.
static void close(Iterable<? extends Closeable> objects)
           
static void closeWhileHandlingException(Closeable... objects)
          Closes all given Closeables, suppressing all thrown exceptions.
static
<E extends Exception>
void
closeWhileHandlingException(E priorException, Closeable... objects)
          Closes all given Closeables, suppressing all thrown exceptions.
static
<E extends Exception>
void
closeWhileHandlingException(E priorException, Iterable<Closeable> objects)
           
static void closeWhileHandlingException(Iterable<? extends Closeable> objects)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

closeWhileHandlingException

public static <E extends Exception> void closeWhileHandlingException(E priorException,
                                                                     Closeable... objects)
                                        throws E extends Exception,
                                               IOException

Closes all given Closeables, suppressing all thrown exceptions. Some of the Closeables may be null, they are ignored. After everything is closed, method either throws priorException, if one is supplied, or the first of suppressed exceptions, or completes normally.

Sample usage:

 Closeable resource1 = null, resource2 = null, resource3 = null;
 ExpectedException priorE = null;
 try {
   resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException
   ..do..stuff.. // May throw ExpectedException
 } catch (ExpectedException e) {
   priorE = e;
 } finally {
   closeSafely(priorE, resource1, resource2, resource3);
 }
 

Parameters:
priorException - null or an exception that will be rethrown after method completion
objects - objects to call close() on
Throws:
E extends Exception
IOException

closeWhileHandlingException

public static <E extends Exception> void closeWhileHandlingException(E priorException,
                                                                     Iterable<Closeable> objects)
                                        throws E extends Exception,
                                               IOException
Throws:
E extends Exception
IOException
See Also:
closeWhileHandlingException(Exception, Closeable...)

close

public static void close(Closeable... objects)
                  throws IOException
Closes all given Closeables. Some of the Closeables may be null; they are ignored. After everything is closed, the method either throws the first exception it hit while closing, or completes normally if there were no exceptions.

Parameters:
objects - objects to call close() on
Throws:
IOException

close

public static void close(Iterable<? extends Closeable> objects)
                  throws IOException
Throws:
IOException
See Also:
close(Closeable...)

closeWhileHandlingException

public static void closeWhileHandlingException(Closeable... objects)
                                        throws IOException
Closes all given Closeables, suppressing all thrown exceptions. Some of the Closeables may be null, they are ignored.

Parameters:
objects - objects to call close() on
Throws:
IOException

closeWhileHandlingException

public static void closeWhileHandlingException(Iterable<? extends Closeable> objects)
                                        throws IOException
Throws:
IOException
See Also:
closeWhileHandlingException(Closeable...)


Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.