3

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?

Rob
  • 1,158
  • 1
  • 12
  • 22
  • problem is, an InputStream is a group of bytes, so until you do not read all bytes, you don't have a "translated string". Maybe you shoud try to investigate to read a chuncked inputstream, but I think you will still have same problem. something like this . http://www.java2s.com/Code/Java/File-Input-Output/AnInputStreamthatimplementsHTTP11chunking.htm – duardito Aug 03 '16 at 09:23
  • Sounds like a job for java8 streams. Look for some tutorials like [here](http://www.tutorialspoint.com/java8/java8_streams.htm) or [here](http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/). – ArcticLord Aug 03 '16 at 09:26
  • 3
    At the stream level, this is hard thing (done it once, but it is supprisingly tricky). It gets simpler if you have some kind of delimiter (i.e. if this were chars, buffer each line, and perform search/replace on a line). Otherwise, you have to do it manually, that is, read a bunch of bytes, see if there is a match OR a partial match. If there is a partial match, read again up to the point where you can decide if you have to replace, then give your caller the number of bytes he requested, and buffer what you have over-read... @ArticLord1 : this is highly statefull. Difficult with streams ! – GPI Aug 03 '16 at 09:27
  • 2
    You can also check this link for some guidance [http://stackoverflow.com/questions/7743534/filter-search-and-replace-array-of-bytes-in-an-inputstream](http://stackoverflow.com/questions/7743534/filter-search-and-replace-array-of-bytes-in-an-inputstream) – Rishal Aug 03 '16 at 09:38

1 Answers1

0

I know to few about what kind of filtering do you need. But onw of possible solutions is to extend java.io.FilterInputStream and there override read() and read(byte[] b)