commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stephen Colebourne" <scolebou...@btopenworld.com>
Subject Re: [io] copyAndClose
Date Sun, 06 Feb 2005 10:31:09 GMT
Well, for a start your example could be simplified.
> public static void copyAndClose(final InputStream in,
>         final OutputStream out)
>         throws IOException {
>     try {
>         CopyUtils.copy(in, out);
>         out.flush();
>
>     } finally {
           IOUtils.copyAndClose(in);
           IOUtils.copyAndClose(out);
>     }
> }

Even so, this is quite a good suggestion. The main downside is that it 
doubles the number of methods. (CopyUtils is being deprecated, with the 
methods moving to IOUtils with better semantics and names to simplify the 
overall API)

Were you interested in writing the patch and tests for IOUtils?

Stephen


----- Original Message ----- 
From: "B. K. Oxley (binkley)" <binkley@alumni.rice.edu>
> It would be nice to include utility methods such as this in the io library 
> to compliment the good set of primitives already there.
>
> The method in question with Javadoc added:
> /**
>  * Copies <var>in</var> to <var>out</var>, flushing <var>out</var>
>  * and closing both streams even in the face of exceptions.  This is
>  * usually sufficient to ensure proper copying for arbitrary streams
>  * and avoid data lose (<cite>e.g.</cite>, file streams can lose
>  * data if you do not flush them before closing).
>  * <p/>
>  * If more than one operation throws an exception, propagates only
>  * the most recent exception.  Java swallows earlier exceptions.
>  *
>  * @param in the input stream data source
>  * @param out the output stream data sink
>  *
>  * @throws IOException if any I/O operation fails
>  */
> public static void copyAndClose(final InputStream in,
>         final OutputStream out)
>         throws IOException {
>     try {
>         CopyUtils.copy(in, out);
>         out.flush();
>
>     } finally {
>         try {
>             out.close();
>
>         } finally {
>             in.close();
>         }
>     }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message