You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Mark (JIRA)" <ji...@apache.org> on 2016/03/11 13:32:44 UTC

[jira] [Issue Comment Deleted] (IO-500) IOUtils: plz include close* functions from lucene util's IOUtils class

     [ https://issues.apache.org/jira/browse/IO-500?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Mark updated IO-500:
--------------------
    Comment: was deleted

(was: Yeah, I'm not arguing to just copy and paste it. But it would be nice to have a one-liner for closing multiple Closeables, allowing for null arguments, and maybe having the option to suppress the IOExceptions or rethrow them after handling all of the Closeables.)

> IOUtils: plz include close* functions from lucene util's IOUtils class
> ----------------------------------------------------------------------
>
>                 Key: IO-500
>                 URL: https://issues.apache.org/jira/browse/IO-500
>             Project: Commons IO
>          Issue Type: Improvement
>          Components: Utilities
>            Reporter: Mark
>            Priority: Minor
>
> {code}
>   /**
>    * Closes all given <tt>Closeable</tt>s.  Some of the
>    * <tt>Closeable</tt>s 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.
>    * 
>    * @param objects
>    *          objects to call <tt>close()</tt> on
>    */
>   public static void close(Closeable... objects) throws IOException {
>     close(Arrays.asList(objects));
>   }
>   
>   /**
>    * Closes all given <tt>Closeable</tt>s.
>    * @see #close(Closeable...)
>    */
>   public static void close(Iterable<? extends Closeable> objects) throws IOException {
>     Throwable th = null;
>     for (Closeable object : objects) {
>       try {
>         if (object != null) {
>           object.close();
>         }
>       } catch (Throwable t) {
>         addSuppressed(th, t);
>         if (th == null) {
>           th = t;
>         }
>       }
>     }
>     reThrow(th);
>   }
>   /**
>    * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
>    * Some of the <tt>Closeable</tt>s may be null, they are ignored.
>    * 
>    * @param objects
>    *          objects to call <tt>close()</tt> on
>    */
>   public static void closeWhileHandlingException(Closeable... objects) {
>     closeWhileHandlingException(Arrays.asList(objects));
>   }
>   
>   /**
>    * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
>    * @see #closeWhileHandlingException(Closeable...)
>    */
>   public static void closeWhileHandlingException(Iterable<? extends Closeable> objects) {
>     for (Closeable object : objects) {
>       try {
>         if (object != null) {
>           object.close();
>         }
>       } catch (Throwable t) {
>       }
>     }
>   }
>   
>   /** adds a Throwable to the list of suppressed Exceptions of the first Throwable
>    * @param exception this exception should get the suppressed one added
>    * @param suppressed the suppressed exception
>    */
>   private static void addSuppressed(Throwable exception, Throwable suppressed) {
>     if (exception != null && suppressed != null) {
>       exception.addSuppressed(suppressed);
>     }
>   }
>   {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)