java - Cloning/Multiple reading an Input Stream from an HttpResponse -
i have httpresponse containing data (json/xml, can contain large amount of data). using function reads , uses input stream data (which closed, , cannot changed either maybe accepting string instead of inputstream), , different function validates data (general validation, not related actual usage part can't touch).
i this:
httpresponse response = gettheresponse(); inputstream in = response.getentity().getcontent(); boolean isvaid = validatedata(in); if(!isvalid){ throw new exception("something wrong"); } else { usedata(in); }
since validatedata needs read data inputstream (written me can manipulated), usedata empty stream. best way implement this? thought copying inputstream large response wasteful. converting inputstream string can have same effect
any thoughts?
thanks
basically, have 2 options
- keep result in memory
- perform http request twice
performing http request twice produce additional network traffic , requires endpoint indempotent. move additional "load" network , remote server. wouldn't that.
keeping result better way go may require amount of memory pointed out. read byte-array , read byte array using bytearrayinputstream
. if improve design, use fixed buffer , perform both operations on content of buffer - if partial content acceptable. input streams have capability rewind, in end boils down internal buffer used. however, when reading network socket - required reading http response - won't possible rewind beginning of stream socket buffer might flushed.
Comments
Post a Comment