This may help you:
http://ostermiller.org/convert_java_outputstream_inputstream.html
This article mentions 3 possibilities:
- write the full output to the byte array, then read it again
- use pipes
- use the cyclic byte buffer (part of the library located on this page).
Just for reference, doing it the other way around: input to output:
A simple solution with Apache Commons IO would be:
IOUtils.copyLarge(InputStream, OutputStream)
or if you just want to copy the file:
FileUtils.copyFile(inFile,outFile);
If you do not want to use Apache Commons IO, here is what the method does copyLarge:
public static long copyLarge(InputStream input, OutputStream output) throws IOException
{
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}