From: <bra...@us...> - 2010-01-12 22:24:11
|
Revision: 2944 http://archive-access.svn.sourceforge.net/archive-access/?rev=2944&view=rev Author: bradtofel Date: 2010-01-12 22:24:04 +0000 (Tue, 12 Jan 2010) Log Message: ----------- FEATURE: added copyStream() methods to drain bytes from an InputStream to an OutputStream Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java 2010-01-12 22:17:44 UTC (rev 2943) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java 2010-01-12 22:24:04 UTC (rev 2944) @@ -24,7 +24,13 @@ */ package org.archive.wayback.util; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + public class ByteOp { + public final static int BUFFER_SIZE = 4096; + public static byte[] copy(byte[] src, int offset, int length) { byte[] copy = new byte[length]; System.arraycopy(src, offset, copy, 0, length); @@ -41,4 +47,28 @@ } return true; } + /** + * Write all bytes from is to os. Does not close either stream. + * @param is to copy bytes from + * @param os to copy bytes to + * @throws IOException for usual reasons + */ + public static void copyStream(InputStream is, OutputStream os) + throws IOException { + copyStream(is,os,BUFFER_SIZE); + } + /** + * Write all bytes from is to os. Does not close either stream. + * @param is to copy bytes from + * @param os to copy bytes to + * @param size number of bytes to buffer between read and write operations + * @throws IOException for usual reasons + */ + public static void copyStream(InputStream is, OutputStream os, int size) + throws IOException { + byte[] buffer = new byte[size]; + for (int r = -1; (r = is.read(buffer, 0, size)) != -1;) { + os.write(buffer, 0, r); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |