Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes slowness in HttpURLConnection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "slowness in HttpURLConnection?" Watch "slowness in HttpURLConnection?" New topic
Author

slowness in HttpURLConnection?

Prateek Sharma
Greenhorn

Joined: Jul 20, 2008
Posts: 23
Hello All,

I am using a small code for downloading a web page content. But its working very slow. When I open same web page using my mozilla browser it hardly takes 2-3 seconds but when I use my Java code it takes 55-60 seconds for the same page (Just for HTML, no images). What could be the reason.





Thanks in advance
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8934
    
    9



This is some fairly inefficient code. While you do use a BufferedInputStream, which is good, you read it one. byte. at. a. time. Which is not good.
Furthermore, you have a Reader, which does character conversion, but you since you are reading it by the byte, you have to do a second character conversion (the cast of 'c' to char).


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Prateek Sharma
Greenhorn

Joined: Jul 20, 2008
Posts: 23
Hi Joe,

Could you please suggest me what is the better way to do it ....

Regards
-
P
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
You should read more than a single character at a time. See http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes for an example of that.

Also note that String.valueOf uses the platform default encoding for the conversion - which may not be the right one. You need to check the Content-Type header of the response to find out which encoding it's in, and then use that for your byte-to-String conversion.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8934
    
    9

Hey, I wrote that FAQ
I'd recommend following the link at the bottom of the FAQ to the Java Platform Performance book. There' s more than one way to buffer IO and some methods make more sense in some situations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: slowness in HttpURLConnection?