I make a http get request to server via HttpURLConnection and I need to read response (InputStream) twice: for logging purposes and for parsing response. The returned InputStraem is instance of org.apache.harmony.luni.internal.net.www.protocol.http.ChunkedInputStream which doesn’t support marking (is.markSupported() return false).
Therefore I can’t mark() and reset() stream and after writing response in log I can’t parse it. Ofcourse I can read response once into String or something else, log them and later parse. But when i’m working with streams i’m avoiding potentially OutOfMemomryError because streams deals with buffering instead of me.
What is the best solution in this case which will keep the benefits of using streams and help to achieve the desired result: the simultaneous recording into the log and parsing response?
EDIT: Solution with writing response into temporary file isn’t appropriate
I’m not sure I fully understand, you’d like to read the
InputStreamonce (not really twice that’s a bit unclean IMO, because what if an error occurs only on the log stream and not the stream you are parsing?) and then simply log and parse the sameInputStream?If the above is the case here pseudo to show the solution:
even better for simultaneous logging and recording would be creating a new
Thread/Runnablefor both methods, starting them and wait for them to return using (thread.join();):