You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/04/17 16:39:33 UTC
svn commit: r1468938 - in /commons/proper/io/trunk/src: changes/changes.xml
main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
Author: sebb
Date: Wed Apr 17 14:39:33 2013
New Revision: 1468938
URL: http://svn.apache.org/r1468938
Log:
IO-346 Add ByteArrayOutputStream.toInputStream()
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1468938&r1=1468937&r2=1468938&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Apr 17 14:39:33 2013
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
<body>
<!-- The release date is the date RC is cut -->
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
+ <action issue="IO-346" dev="sebb" type="add">
+ Add ByteArrayOutputStream.toInputStream()
+ </action>
<action issue="IO-368" dev="sebb" type="fix">
ClassLoaderObjectInputStream does not handle primitive typed members
</action>
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java?rev=1468938&r1=1468937&r2=1468938&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java Wed Apr 17 14:39:33 2013
@@ -67,6 +67,8 @@ public class ByteArrayOutputStream exten
private byte[] currentBuffer;
/** The total count of bytes written. */
private int count;
+ /** Flag to indicate if the buffers can be reused after reset */
+ private boolean reuseBuffers = true;
/**
* Creates a new byte array output stream. The buffer capacity is
@@ -230,7 +232,16 @@ public class ByteArrayOutputStream exten
count = 0;
filledBufferSum = 0;
currentBufferIndex = 0;
- currentBuffer = buffers.get(currentBufferIndex);
+ if (reuseBuffers) {
+ currentBuffer = buffers.get(currentBufferIndex);
+ } else {
+ //Throw away old buffers
+ currentBuffer = null;
+ int size = buffers.get(0).length;
+ buffers.clear();
+ needNewBuffer(size);
+ reuseBuffers = true;
+ }
}
/**
@@ -280,7 +291,7 @@ public class ByteArrayOutputStream exten
@SuppressWarnings("resource")
final ByteArrayOutputStream output = new ByteArrayOutputStream();
output.write(input);
- return output.toBufferedInputStream();
+ return output.toInputStream();
}
/**
@@ -291,9 +302,9 @@ public class ByteArrayOutputStream exten
* @return the current contents of this output stream.
* @see java.io.ByteArrayOutputStream#toByteArray()
* @see #reset()
- * @since 2.0
+ * @since 2.5
*/
- private InputStream toBufferedInputStream() {
+ public synchronized InputStream toInputStream() {
int remaining = count;
if (remaining == 0) {
return new ClosedInputStream();
@@ -307,6 +318,7 @@ public class ByteArrayOutputStream exten
break;
}
}
+ reuseBuffers = false;
return new SequenceInputStream(Collections.enumeration(list));
}
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java?rev=1468938&r1=1468937&r2=1468938&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java Wed Apr 17 14:39:33 2013
@@ -17,6 +17,10 @@
package org.apache.commons.io.output;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.IOUtils;
import junit.framework.TestCase;
@@ -91,6 +95,67 @@ public class ByteArrayOutputStreamTestCa
final byte[] refbuf = expected.toByteArray();
checkByteArrays(buf, refbuf);
}
+
+ public void testToInputStream() throws IOException {
+ ByteArrayOutputStream baout = new ByteArrayOutputStream();
+ java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
+
+ //Write 8224 bytes
+ writeData(baout, ref, 32);
+ for(int i=0;i<128;i++) {
+ writeData(baout, ref, 64);
+ }
+
+ //Get data before more writes
+ InputStream in = baout.toInputStream();
+ byte refData[] = ref.toByteArray();
+
+ //Write some more data
+ writeData(baout, ref, new int[] { 2, 4, 8, 16 });
+
+ //Check original data
+ byte baoutData[] = IOUtils.toByteArray(in);
+ assertEquals(8224, baoutData.length);
+ checkByteArrays(refData, baoutData);
+
+ //Check all data written
+ baoutData = IOUtils.toByteArray(baout.toInputStream());
+ refData = ref.toByteArray();
+ assertEquals(8254, baoutData.length);
+ checkByteArrays(refData, baoutData);
+ }
+
+ public void testToInputStreamWithReset() throws IOException {
+ //Make sure reset() do not destroy InputStream returned from toInputStream()
+ ByteArrayOutputStream baout = new ByteArrayOutputStream();
+ java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
+
+ //Write 8224 bytes
+ writeData(baout, ref, 32);
+ for(int i=0;i<128;i++) {
+ writeData(baout, ref, 64);
+ }
+
+ //Get data before reset
+ InputStream in = baout.toInputStream();
+ byte refData[] = ref.toByteArray();
+
+ //Reset and write some new data
+ baout.reset();
+ ref.reset();
+ writeData(baout, ref, new int[] { 2, 4, 8, 16 });
+
+ //Check original data
+ byte baoutData[] = IOUtils.toByteArray(in);
+ assertEquals(8224, baoutData.length);
+ checkByteArrays(refData, baoutData);
+
+ //Check new data written after reset
+ baoutData = IOUtils.toByteArray(baout.toInputStream());
+ refData = ref.toByteArray();
+ assertEquals(30, baoutData.length);
+ checkByteArrays(refData, baoutData);
+ }
public void testStream() throws Exception {
int written;