I am streaming very large amounts of data from InputStreams to OutputStreams. The method I’m using looks like this:
public static void forwardStream(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
Now I need to perform search/replace operations on the stream contents before its written. Loading the whole stream into a String and then perform a String.replace() is not an option due to the large size of the streamed data.
My idea would be to wrap the InputStream in some kind of helper class inheriting InputStream and perform the replace operations while its contents are read. Are there any proven solutions out there?