Author: jukka
Date: Sun Jan 6 13:57:24 2008
New Revision: 609421
URL: http://svn.apache.org/viewvc?rev=609421&view=rev
Log:
IO-152 - Add ByteArrayOutputStream.write(InputStream)
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
commons/proper/io/trunk/src/test/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java?rev=609421&r1=609420&r2=609421&view=diff
==============================================================================
---
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
(original)
+++
commons/proper/io/trunk/src/java/org/apache/commons/io/output/ByteArrayOutputStream.java
Sun Jan 6 13:57:24 2008
@@ -17,6 +17,7 @@
package org.apache.commons.io.output;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
@@ -170,6 +171,34 @@
}
currentBuffer[inBufferPos] = (byte) b;
count++;
+ }
+
+ /**
+ * Writes the entire contents of the specified input stream to this
+ * byte stream. Bytes from the input stream are read directly into the
+ * internal buffers of this streams.
+ *
+ * @param in the input stream to read from
+ * @return total number of bytes read from the input stream
+ * (and written to this stream)
+ * @throws IOException if an I/O error occurs while reading the input
stream
+ * @since Commons IO 1.4
+ */
+ public synchronized int write(InputStream in) throws IOException {
+ int readCount = 0;
+ int inBufferPos = count - filledBufferSum;
+ int n = in.read(currentBuffer, inBufferPos, currentBuffer.length -
inBufferPos);
+ while (n != -1) {
+ readCount += n;
+ inBufferPos += n;
+ count += n;
+ if (inBufferPos == currentBuffer.length) {
+ needNewBuffer(currentBuffer.length);
+ inBufferPos = 0;
+ }
+ n = in.read(currentBuffer, inBufferPos, currentBuffer.length -
inBufferPos);
+ }
+ return readCount;
}
/**
Modified:
commons/proper/io/trunk/src/test/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java?rev=609421&r1=609420&r2=609421&view=diff
==============================================================================
---
commons/proper/io/trunk/src/test/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
(original)
+++
commons/proper/io/trunk/src/test/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
Sun Jan 6 13:57:24 2008
@@ -16,6 +16,7 @@
*/
package org.apache.commons.io.output;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import junit.framework.TestCase;
@@ -120,7 +121,13 @@
written = writeData(baout, ref, new int[] {5, 47, 33, 60, 1, 0, 8});
assertEquals(155, written);
checkStreams(baout, ref);
-
+
+ //Test the readFrom(InputStream) method
+ baout.reset();
+ written = baout.write(new ByteArrayInputStream(ref.toByteArray()));
+ assertEquals(155, written);
+ checkStreams(baout, ref);
+
//Write the commons Byte[]OutputStream to a java.io.Byte[]OutputStream
//and vice-versa to test the writeTo() method.
ByteArrayOutputStream baout1 = new ByteArrayOutputStream(32);