[Japi-cvs] SF.net SVN: japi: [53] trunk/src/app/net/sf/japi/io/Copier.java
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-04-13 21:42:46
|
Revision: 53 Author: christianhujer Date: 2006-04-13 14:42:38 -0700 (Thu, 13 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=53&view=rev Log Message: ----------- Added I/O Copier. Added Paths: ----------- trunk/src/app/net/sf/japi/io/Copier.java Added: trunk/src/app/net/sf/japi/io/Copier.java =================================================================== --- trunk/src/app/net/sf/japi/io/Copier.java (rev 0) +++ trunk/src/app/net/sf/japi/io/Copier.java 2006-04-13 21:42:38 UTC (rev 53) @@ -0,0 +1,122 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.io; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +/** A Runnable that copies from an InputStream to an OutputStream. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + */ +public class Copier implements Runnable { + + /** Default buffer size when copying. */ + public static final int DEFAULT_BUF_SIZE = 8192; + + /** Default automatic flush. */ + public static final boolean DEFAULT_AUTO_FLUSH = true; + + /** Default automatic close. */ + public static final boolean DEFAULT_AUTO_CLOSE = true; + + /** The InputStream to read from. */ + private final InputStream in; + + /** The OutputStream to write to. */ + private final OutputStream out; + + /** The buffer size to use. */ + private final int bufSize; + + /** Whether to flush automatically. */ + private final boolean autoFlush; + + /** Whether to close streams automatically after copying. */ + private final boolean autoClose; + + /** Create a Copier with default buffer size and autoFlush. + * @param in the InputStream to read from + * @param out the OutputStream to write to + */ + public Copier(final InputStream in, final OutputStream out) { + this(in, out, DEFAULT_BUF_SIZE, DEFAULT_AUTO_FLUSH, DEFAULT_AUTO_CLOSE); + } + + /** Create a Copier with specified buffer size and automatic flush behaviour. + * @param in the InputStream to read from + * @param out the OutputStream to write to + * @param bufSize buffer size to use while copying + */ + public Copier(final InputStream in, final OutputStream out, final int bufSize) { + this(in, out, bufSize, DEFAULT_AUTO_FLUSH, DEFAULT_AUTO_CLOSE); + } + + /** Create a Copier with specified buffer size and specified flush behaviour. + * @param in the InputStream to read from + * @param out the OutputStream to write to + * @param bufSize buffer size to use while copying + * @param autoFlush whether to flush automatically (true for automatic flush, false for flush on close) + * @param autoClose whether to close the streams automatically (true for automatic close, false for no close) + */ + public Copier(final InputStream in, final OutputStream out, final int bufSize, final boolean autoFlush, final boolean autoClose) { + this.in = in; + this.out = out; + this.bufSize = bufSize; + this.autoFlush = autoFlush; + this.autoClose = autoClose; + } + + /** Start the copier in a new thread. + * @return the newly created thread + */ + public Thread start() { + final Thread thread = new Thread(this); + thread.start(); + return thread; + } + + /** {@inheritDoc} */ + public void run() { + final byte[] buf = new byte[bufSize]; + try { + for (int bytesRead; (bytesRead = in.read(buf)) != -1;) { + out.write(buf, 0, bytesRead); + if (autoFlush) { + out.flush(); + } + } + } catch (final IOException e) { + System.err.println(e); + } finally { + //noinspection CatchGenericClass,OverlyBroadCatchBlock + try { out.flush(); } catch (final Exception ignore) { /* ignore */ } + if (autoClose) { + //noinspection CatchGenericClass,OverlyBroadCatchBlock + try { out.close(); } catch (final Exception ignore) { /* ignore */ } + //noinspection CatchGenericClass,OverlyBroadCatchBlock + try { in.close(); } catch (final Exception ignore) { /* ignore */ } + } + } + } + +} // class Copier Property changes on: trunk/src/app/net/sf/japi/io/Copier.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |