I have done today a test and my results are strange. I used code below and test some of web pages.
long start = System.nanoTime();
URL url = new URL("http://wp.pl");
System.out.println("" + (System.nanoTime() - start) / 1000000);
start = System.nanoTime();
InputStream is = url.openStream();
System.out.println("" + (System.nanoTime() - start) / 1000000);
start = System.nanoTime();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
System.out.println("" + (System.nanoTime() - start) / 1000000);
start = System.nanoTime();
BufferedReader in = new BufferedReader(isr);
System.out.println("" + (System.nanoTime() - start) / 1000000);
//After this point reading whole site goes nice and fast
Only second time was diffrent (the rest was arround 0). Here are the times for different sites of (url.openStream()):
adress | time | lines of code
yahoo.com/ 1000 943
bbc.com 230 1500
news.google.pl/ 138 384
free-ebooks.net 776 668
wp.pl 3066 2811
The point is the time is different and its look like it depends on length of site code. My question:
Do url.openStream() download whole site code? It should not just open stream and wait for user to read it?
openStreamwaits at least until the server starts sending the response. Otherwise it couldn’t know if it can actually return anInputStreamor should throw an exception instead.On many webservers the actual sending of data is the shortest time: by the time the web server has decided which headers to send (and thus starts sending the response) it already knows the full content.