[Japi-cvs] SF.net SVN: japi:[1338] libs/io/trunk/src/prj/net/sf/japi/io/Copier.java
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2009-06-14 09:20:49
|
Revision: 1338 http://japi.svn.sourceforge.net/japi/?rev=1338&view=rev Author: christianhujer Date: 2009-06-14 09:20:46 +0000 (Sun, 14 Jun 2009) Log Message: ----------- Add documentation and improve exception handling within the copy routine. Modified Paths: -------------- libs/io/trunk/src/prj/net/sf/japi/io/Copier.java Modified: libs/io/trunk/src/prj/net/sf/japi/io/Copier.java =================================================================== --- libs/io/trunk/src/prj/net/sf/japi/io/Copier.java 2009-06-14 09:12:43 UTC (rev 1337) +++ libs/io/trunk/src/prj/net/sf/japi/io/Copier.java 2009-06-14 09:20:46 UTC (rev 1338) @@ -24,9 +24,23 @@ import org.jetbrains.annotations.NotNull; /** A Runnable that copies from an InputStream to an OutputStream. + * + * <h4>Usage example</h4> + * <pre> + * // Copies data from System.in to System.out. + * new Copier(System.in, System.out).start(); + * + * // Copies data from one file to another. + * new Copier(new FileInputStream(inputFilename), new FileOutputStream(outputFilename)).start(); + * </pre> + * + * @note Copying is done in a separate thread. + * The starting thread is not notified of any exceptions that occur. + * * @author <a href="mailto:ch...@ri...">Christian Hujer</a> * @since 0.1 */ +// TODO:2009-06-14:christianhujer:Implement reasonable exception handling. public class Copier implements Runnable { /** Default buffer size when copying. */ @@ -98,20 +112,24 @@ public void run() { final byte[] buf = new byte[bufSize]; try { - for (int bytesRead; (bytesRead = in.read(buf)) != -1;) { - out.write(buf, 0, bytesRead); + try { + for (int bytesRead; (bytesRead = in.read(buf)) != -1;) { + out.write(buf, 0, bytesRead); + if (autoFlush) { + out.flush(); + } + } + } finally { if (autoFlush) { out.flush(); } + if (autoClose) { + out.close(); + in.close(); + } } } catch (final IOException e) { System.err.println(e); - } finally { - try { out.flush(); } catch (final IOException ignore) { /* ignore */ } - if (autoClose) { - try { out.close(); } catch (final IOException ignore) { /* ignore */ } - try { in.close(); } catch (final IOException ignore) { /* ignore */ } - } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |