Possible Implementations
As you probably guessed there is no (both easy and efficient) way to get the data written to an OutputStream to fit the InputStream interface or to get the first converted into the latter. However there are some different strategies:
- Write the data the data into a memory buffer (ByteArrayOutputStream) get the byteArray and read it again with a ByteArrayInputStream. This is the best approach if you're sure your data fits into memory.
- Copy your data to a temporary file and read it back.
- Use pipes: this is the best approach both for memory usage and speed (you can take full advantage of the multi-core processors) and also the standard solution offered by Sun.
- Use InputStreamFromOutputStream and OutputStreamToInputStream from this library .
Why so difficult?
Memory buffer