Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple question on urlConnection.getInputStream();  RSS feed

kevind duff
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22480
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!