commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (IO-500) IOUtils: plz include close* functions from lucene util's IOUtils class
Date Fri, 11 Mar 2016 12:15:50 GMT

    [ https://issues.apache.org/jira/browse/IO-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15190849#comment-15190849
] 

Mark commented on IO-500:
-------------------------

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)

Mime
View raw message