Cloning/Multiple reading an Input Stream from an HttpResponse

若如初见. 提交于 2021-01-29 07:21:14

问题


I have an HttpResponse containing some data (json/xml, which can contain a large amount of data). I am using a function that reads and uses an input stream with this data (which is closed, and cannot be changed either than maybe accepting a string instead of an inputstream), and a different function that validates the data (general validation, not related to the actual usage which is a part I can't touch).

I would like to do something like this:

HttpResponse response = getTheResponse();
InputStream in = response.getEntity().getContent(); 
boolean isVaid = validateData(in);
if(!isValid){
    throw new Exception("Something is wrong");
} else {
    useData(in);
}

Since validateData needs to read the data from the InputStream (written by me so this can be manipulated), the useData will get an empty stream. What is the best way to implement this? I thought about copying the inputstream but for a large response that could be very wasteful. Converting from InputStream to string can have the same effect

Any Thoughts?

Thanks


回答1:


Basically, you have two options

  1. Keep the result in memory
  2. Perform the HTTP Get request twice

Performing the HTTP request twice will produce some additional network traffic and requires the endpoint to be indempotent. So you move the additional "load" to the network and the remote server. I wouldn't do that.

Keeping the result is probably the better way to go but may require some amount of memory as you pointed out. You could read everything into a byte-array and read from that byte array using ByteArrayInputStream. If you improve your design, you could use a fixed buffer and perform both operations on the content of the buffer - if partial content is acceptable. Some input streams have the capability to rewind, which in the end boils down to an internal buffer that is used. However, when reading from a Network socket - which is required for reading a http response - won't be possible to rewind to the beginning of the stream as the socket buffer might already be flushed.



来源:https://stackoverflow.com/questions/31402711/cloning-multiple-reading-an-input-stream-from-an-httpresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!