You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sa...@apache.org on 2001/12/07 22:33:12 UTC
cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util StreamUtils.java
sanders 01/12/07 13:33:12
Modified: util/src/java/org/apache/commons/util StreamUtils.java
Log:
Patch submitted by Michael Bayne <md...@samskivert.com>
Revision Changes Path
1.3 +35 -3 jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StreamUtils.java
Index: StreamUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StreamUtils.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StreamUtils.java 2001/08/15 22:57:03 1.2
+++ StreamUtils.java 2001/12/07 21:33:12 1.3
@@ -66,7 +66,7 @@
*
* @author <a href="mailto:leonardr@collab.net">Leonard Richardson</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
- * @version $Id: StreamUtils.java,v 1.2 2001/08/15 22:57:03 dlr Exp $
+ * @version $Id: StreamUtils.java,v 1.3 2001/12/07 21:33:12 sanders Exp $
*/
public class StreamUtils
{
@@ -104,6 +104,39 @@
String encoding)
throws IOException
{
+ ByteArrayOutputStream contents = readStream(toRead, bufferSize);
+ return (encoding == null ? contents.toString() :
+ contents.toString(encoding));
+ }
+
+ /**
+ * Reads from a stream until EOF, and returns the bytes read.
+ *
+ * @param toRead Stream to use as source.
+ * @param bufferSize Size of buffer to use when reading from source.
+ * @return The contents of <code>toRead</code>.
+ */
+ public static byte[] streamAsBytes(InputStream toRead, int bufferSize)
+ throws IOException
+ {
+ ByteArrayOutputStream contents = readStream(toRead, bufferSize);
+ return contents.toByteArray();
+ }
+
+ /**
+ * Reads from a stream util EOF, placing the resulting data into a
+ * <code>ByteArrayOutputStream</code> which is subsequently returned.
+ *
+ * @param toRead Stream to use as source.
+ * @param bufferSize Size of buffer to use when reading from source.
+ *
+ * @return a <code>ByteArrayOutputStream</code> containing the
+ * contents of <code>toRead</code>.
+ */
+ protected static ByteArrayOutputStream readStream(InputStream toRead,
+ int bufferSize)
+ throws IOException
+ {
ByteArrayOutputStream contents = new ByteArrayOutputStream();
byte[] buffer = new byte[bufferSize];
int bytesRead;
@@ -113,8 +146,7 @@
contents.write(buffer, 0, bytesRead);
}
- return (encoding == null ? contents.toString() :
- contents.toString(encoding));
+ return contents;
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>