File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes simple question on  urlConnection.getInputStream(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "simple question on  urlConnection.getInputStream();" Watch "simple question on  urlConnection.getInputStream();" New topic

simple question on urlConnection.getInputStream();

kevind duff

Joined: Jul 07, 2011
Posts: 23
Hello All at the Ranch:

I have a simple question regarding urlConnection.getInputStream();

Referring to the bit of code below, my question is when does the data
come down from the remote server. When I call getInputStream?
Do the full results get sent at that time?
Or somewhere in the while loop?

If I check result.available() immediately after getInputStream() I get 0 zero.

Thought I would need to know this to put some error handling around this

thanks for your kind assistance.


URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout( 30 * 1000 ); // wait 30 seconds to connect
urlConnection.setReadTimeout( 120 * 1000 ); // wait 120 seconds for data to return

urlConnection.setRequestProperty("Proxy-Authorization", encodedPword);

InputStream result = urlConnection.getInputStream();
BufferedInputStream bis = new BufferedInputStream( result, 4096);

StringBuffer strBuffer = new StringBuffer();

/* read the InputStream into a StringBuffer. Change code here to handle result set as desired*/
while (true) {

byte buffer[] = new byte [4096];
int endOfResultSet = (buffer, 0, 4096);
if (endOfResultSet == -1)
for (int i=0; i<buffer.length; i++) {
strBuffer.append ((char) buffer[i]);
System.out.print( (char) buffer[i]);
System.out.println("\n** ** strBuffer len: " + strBuffer.length() );

} //while
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The simple answer is, the data comes down as soon as it can. But read our FAQ entry AvailableDoesntDoWhatYouThinkItDoes to see why you don't really need to know the exact answer to the question.
I agree. Here's the link:
subject: simple question on urlConnection.getInputStream();
jQuery in Action, 3rd edition