From: <bra...@us...> - 2009-10-28 03:38:40
|
Revision: 2858 http://archive-access.svn.sourceforge.net/archive-access/?rev=2858&view=rev Author: bradtofel Date: 2009-10-28 03:38:26 +0000 (Wed, 28 Oct 2009) Log Message: ----------- INITIAL REV: simple common ops on byte arrays Added Paths: ----------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java Added: 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 (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java 2009-10-28 03:38:26 UTC (rev 2858) @@ -0,0 +1,44 @@ +/* ByteOp + * + * $Id$ + * + * Created on 3:56:12 PM Dec 16, 2008. + * + * Copyright (C) 2008 Internet Archive. + * + * This file is part of Wayback. + * + * SocksProxyCore is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * any later version. + * + * SocksProxyCore 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU Lesser Public License + * along with SocksProxyCore; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package org.archive.wayback.util; + +public class ByteOp { + public static byte[] copy(byte[] src, int offset, int length) { + byte[] copy = new byte[length]; + System.arraycopy(src, offset, copy, 0, length); + return copy; + } + public static boolean cmp(byte[] input, byte[] want) { + if(input.length != want.length) { + return false; + } + for(int i = 0; i < input.length; i++) { + if(input[i] != want[i]) { + return false; + } + } + return true; + } +} Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java ___________________________________________________________________ Added: svn:executable + * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <bra...@us...> - 2010-04-02 03:14:19
|
Revision: 3015 http://archive-access.svn.sourceforge.net/archive-access/?rev=3015&view=rev Author: bradtofel Date: 2010-04-02 03:14:13 +0000 (Fri, 02 Apr 2010) Log Message: ----------- FEATURE: added discardStream() which reads a stream till it's empty, throwing away the data read 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-04-02 03:12:04 UTC (rev 3014) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java 2010-04-02 03:14:13 UTC (rev 3015) @@ -47,6 +47,16 @@ } return true; } + + public static void discardStream(InputStream is) throws IOException { + discardStream(is,BUFFER_SIZE); + } + public static void discardStream(InputStream is,int size) throws IOException { + byte[] buffer = new byte[size]; + while(is.read(buffer, 0, size) != -1) { + } + } + /** * Write all bytes from is to os. Does not close either stream. * @param is to copy bytes from This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bra...@us...> - 2010-05-14 22:52:56
|
Revision: 3091 http://archive-access.svn.sourceforge.net/archive-access/?rev=3091&view=rev Author: bradtofel Date: 2010-05-14 22:52:50 +0000 (Fri, 14 May 2010) Log Message: ----------- FEATURE: added discardStream() method JAVADOC 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-05-13 18:34:37 UTC (rev 3090) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/ByteOp.java 2010-05-14 22:52:50 UTC (rev 3091) @@ -28,34 +28,100 @@ import java.io.InputStream; import java.io.OutputStream; +/** + * Byte oriented static methods. Likely a lot of overlap with apache- commons + * stuff - eventually should be reconciled. + * + * @author brad + * + */ public class ByteOp { + /** Default buffer size for IO ops */ public final static int BUFFER_SIZE = 4096; + /** + * Create a new byte array with contents initialized to values from the + * argument byte array. + * @param src source byte array of initial values + * @param offset start offset to copy bytes + * @param length number of bytes to copy + * @return a new byte array of size length, containing values from src + * starting from offset in the src array. + */ public static byte[] copy(byte[] src, int offset, int length) { byte[] copy = new byte[length]; System.arraycopy(src, offset, copy, 0, length); return copy; } - public static boolean cmp(byte[] input, byte[] want) { - if(input.length != want.length) { + + /** + * Compare two byte arrays + * @param a byte array to compare + * @param b byte array to compare + * @return true if a and b have same length, and all the same values, false + * otherwise + */ + public static boolean cmp(byte[] a, byte[] b) { + if(a.length != b.length) { return false; } - for(int i = 0; i < input.length; i++) { - if(input[i] != want[i]) { + for(int i = 0; i < a.length; i++) { + if(a[i] != b[i]) { return false; } } return true; } + /** + * throw away all bytes from stream argument + * @param is InputStream to read and discard + * @throws IOException when is throws one + */ public static void discardStream(InputStream is) throws IOException { discardStream(is,BUFFER_SIZE); } + + /** + * throw away all bytes from stream argument + * @param is InputStream to read and discard + * @param size number of bytes to read at once from the stream + * @throws IOException when is throws one + */ public static void discardStream(InputStream is,int size) throws IOException { byte[] buffer = new byte[size]; while(is.read(buffer, 0, size) != -1) { } } + + /** + * throw away all bytes from stream argument, and count how many bytes were + * discarded before reaching the end of the stream. + * @param is InputStream to read and discard + * @return the number of bytes discarded + * @throws IOException when is throws one + */ + public static long discardStreamCount(InputStream is) throws IOException { + return discardStreamCount(is, BUFFER_SIZE); + } + + /** + * throw away all bytes from stream argument, and count how many bytes were + * discarded before reaching the end of the stream. + * @param is InputStream to read and discard + * @param size number of bytes to read at once from the stream + * @return the number of bytes discarded + * @throws IOException when is throws one + */ + public static long discardStreamCount(InputStream is,int size) throws IOException { + long count = 0; + byte[] buffer = new byte[size]; + int amt = 0; + while((amt = is.read(buffer, 0, size)) != -1) { + count += amt; + } + return count; + } /** * Write all bytes from is to os. Does not close either stream. @@ -67,6 +133,7 @@ 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 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |