Suppose I have an io InputStream containing a very very long string and I want to extract the middle of it and put in another InputStream. I don't want to convert the whole thing into a string since it takes too much memory.. Suppose the original stream contains stuff like "<start>VERY VERY LONG CONTENT<end>" and I want to extract "VERY VERY LONG CONTENT" from this InputStream (and by end I want a InputStream not a string). How should I do without utilizing too much memory ?
I was reading this post under the assumption that you wanted to write the chosen data into an OutputStream. But then I read more carefully. Really, you want it to be in another InputStream? I don't understand that because it's already in an InputStream. Maybe you could clarify your requirements a bit and explain what they are for?
Consider using a regular expression to match the text you want to read. Scanners can use such regular expressions with methods like findAll(), findInLine(), and findWithinHorizon()
If this process is all in aid of "not using too much memory", I would recommend doing something ordinary and see if it works. Java allows you to use 4 gigabytes of memory, or maybe more, and chances are that a computer has that much usable memory these days. Let's not go down the premature optimization road.
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Gift giving made easy with the permaculture playing cards