java - Reading from response body multiple times in Apache HttpClient 4.x -
i'm using apache httpclient 4.2.3 in application. store response of http call so:
httpresponse httpresponse = (defaulthttpclient)httpclient.execute(httprequest);
the response body inputstream in 4.x api:
inputstream responsestream = httpresponse.getentity().getcontent();
my problem need read response body string , byte[] @ various points in application. inputstream used apache eofsensorinputstream, means once reach stream eof, gets closed. there anyway can string , byte[] representations multiple times , not close stream?
i've tried wrapping byte array in new bytearrayinputstream , setting request body, doesn't work since response body can reach few gigs. i've tried this, noticed original response stream still gets closed.
any pointers welcome.
edit: on related note, great if find length of inputstream either without consuming stream or reversing consumption.
1 . think have conflicting requirements:
a)
it doesn't work since response body can reach few gigs
b)
is there anyway can string , byte[] representations multiple times , not close stream
if not have enough memory not possible.
btw, way response bytes entityutils.byte[] tobytearray(final httpentity entity)
.
do need n-gigs string? going it?
2 .
it great if find length of inputstream
httpresponse.getentity().getcontentlength()
3 . since response not fit memory suggest save file (or temp file). set inputstream on file, , read many times need.