Posted By:
Stephen_Ostermiller
Posted On:
Wednesday, July 14, 2004 06:18 AM
If you need to read data from a stream more than once, you need to buffer the entire data. Depending on the size of the dataset you can either do this in memory or to the file system.
I'd recommend reading the entire stream to byte array and then create a ByteArrayInputStream on it to pass to your consumer. If you then have another consumer, you can create a new ByteArrayInputStream on that byte array.
To create a byte array, I'd recommend a ByteArrayOutputStream that you write to.
// Capture the data into memomory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read;
while((read=in.read(buffer))!=-1){
baos.write(buffer,0,read);
}
// Send the bytes to two different consumers
InputStream in1 = new ByteArrayInputStream(baos.toByteArray());
InputStream in2 = new ByteArrayInputStream(baos.toByteArray());