I am querying an api over http that returns a huge response. This response is obtained in the form of an InputStream
. It turns out merely reading the characters iteratively is quite slow, for example approximately 16,5s for 750k characters.
On the other hand, if I create a 750k characters long string, and get a stream from new ByteArrayInputStream(string.getBytes())
, and iteratively call read()
until it returns -1, it will do so in approximately 0,02s.
I'm currently calling IOUtils.toString
but my attempts of doing it manually (with the use of a BufferedReader
of InputStreamReader
) yield the same result.
String Builder output Buffer = new StringBuilder(750000);
for (int i = 0; i < 750000; i++) {
outputBuffer.append(i%100==0?"\n":"a");
}
String myString = outputBuffer.toString();
InputStream theStreamIBuilt = new ByteArrayInputStream(myString.getBytes());
long start = nanoTime();
String body = IOUtils.toString(theStreamIBuilt, encoding);
System.out.println("Time : "+((double)(nanoTime() - start)) /1000000000);
System.out.println("Number of chars :" +body.length ());
This yields 0,02s.
URLConnection con = (new URL(url)).openConnection();
InputStream theStreamIReceive = con.getInputStream();
long start = nanoTime();
String body = IOUtils.toString(theStreamIReceive, encoding);
System.out.println("Time : " + ((double) (nanoTime()-start) /1000000000);
System.out.println("Number of chars :" +body.length());
This yields 16,74s. The same size was used.
There's something I'm fundamentally missing here most likely regarding the nature of the InputStream http response. What is it? Where does the speed difference come from?