Skip to content

Commit e263a7c

Browse files
committed
IO-346 Add ByteArrayOutputStream.toInputStream()
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1468938 13f79535-47bb-0310-9956-ffa450edef68
1 parent f9e08f8 commit e263a7c

3 files changed

Lines changed: 84 additions & 4 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
50+
<action issue="IO-346" dev="sebb" type="add">
51+
Add ByteArrayOutputStream.toInputStream()
52+
</action>
5053
<action issue="IO-368" dev="sebb" type="fix">
5154
ClassLoaderObjectInputStream does not handle primitive typed members
5255
</action>

src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public class ByteArrayOutputStream extends OutputStream {
6767
private byte[] currentBuffer;
6868
/** The total count of bytes written. */
6969
private int count;
70+
/** Flag to indicate if the buffers can be reused after reset */
71+
private boolean reuseBuffers = true;
7072

7173
/**
7274
* Creates a new byte array output stream. The buffer capacity is
@@ -230,7 +232,16 @@ public synchronized void reset() {
230232
count = 0;
231233
filledBufferSum = 0;
232234
currentBufferIndex = 0;
233-
currentBuffer = buffers.get(currentBufferIndex);
235+
if (reuseBuffers) {
236+
currentBuffer = buffers.get(currentBufferIndex);
237+
} else {
238+
//Throw away old buffers
239+
currentBuffer = null;
240+
int size = buffers.get(0).length;
241+
buffers.clear();
242+
needNewBuffer(size);
243+
reuseBuffers = true;
244+
}
234245
}
235246

236247
/**
@@ -280,7 +291,7 @@ public static InputStream toBufferedInputStream(final InputStream input)
280291
@SuppressWarnings("resource")
281292
final ByteArrayOutputStream output = new ByteArrayOutputStream();
282293
output.write(input);
283-
return output.toBufferedInputStream();
294+
return output.toInputStream();
284295
}
285296

286297
/**
@@ -291,9 +302,9 @@ public static InputStream toBufferedInputStream(final InputStream input)
291302
* @return the current contents of this output stream.
292303
* @see java.io.ByteArrayOutputStream#toByteArray()
293304
* @see #reset()
294-
* @since 2.0
305+
* @since 2.5
295306
*/
296-
private InputStream toBufferedInputStream() {
307+
public synchronized InputStream toInputStream() {
297308
int remaining = count;
298309
if (remaining == 0) {
299310
return new ClosedInputStream();
@@ -307,6 +318,7 @@ private InputStream toBufferedInputStream() {
307318
break;
308319
}
309320
}
321+
reuseBuffers = false;
310322
return new SequenceInputStream(Collections.enumeration(list));
311323
}
312324

src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
package org.apache.commons.io.output;
1818

1919
import java.io.ByteArrayInputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
23+
import org.apache.commons.io.IOUtils;
2024

2125
import junit.framework.TestCase;
2226

@@ -91,6 +95,67 @@ private void checkStreams(
9195
final byte[] refbuf = expected.toByteArray();
9296
checkByteArrays(buf, refbuf);
9397
}
98+
99+
public void testToInputStream() throws IOException {
100+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
101+
java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
102+
103+
//Write 8224 bytes
104+
writeData(baout, ref, 32);
105+
for(int i=0;i<128;i++) {
106+
writeData(baout, ref, 64);
107+
}
108+
109+
//Get data before more writes
110+
InputStream in = baout.toInputStream();
111+
byte refData[] = ref.toByteArray();
112+
113+
//Write some more data
114+
writeData(baout, ref, new int[] { 2, 4, 8, 16 });
115+
116+
//Check original data
117+
byte baoutData[] = IOUtils.toByteArray(in);
118+
assertEquals(8224, baoutData.length);
119+
checkByteArrays(refData, baoutData);
120+
121+
//Check all data written
122+
baoutData = IOUtils.toByteArray(baout.toInputStream());
123+
refData = ref.toByteArray();
124+
assertEquals(8254, baoutData.length);
125+
checkByteArrays(refData, baoutData);
126+
}
127+
128+
public void testToInputStreamWithReset() throws IOException {
129+
//Make sure reset() do not destroy InputStream returned from toInputStream()
130+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
131+
java.io.ByteArrayOutputStream ref = new java.io.ByteArrayOutputStream();
132+
133+
//Write 8224 bytes
134+
writeData(baout, ref, 32);
135+
for(int i=0;i<128;i++) {
136+
writeData(baout, ref, 64);
137+
}
138+
139+
//Get data before reset
140+
InputStream in = baout.toInputStream();
141+
byte refData[] = ref.toByteArray();
142+
143+
//Reset and write some new data
144+
baout.reset();
145+
ref.reset();
146+
writeData(baout, ref, new int[] { 2, 4, 8, 16 });
147+
148+
//Check original data
149+
byte baoutData[] = IOUtils.toByteArray(in);
150+
assertEquals(8224, baoutData.length);
151+
checkByteArrays(refData, baoutData);
152+
153+
//Check new data written after reset
154+
baoutData = IOUtils.toByteArray(baout.toInputStream());
155+
refData = ref.toByteArray();
156+
assertEquals(30, baoutData.length);
157+
checkByteArrays(refData, baoutData);
158+
}
94159

95160
public void testStream() throws Exception {
96161
int written;

0 commit comments

Comments
 (0)