• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple question on urlConnection.getInputStream();

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
code.


thanks for your kind assistance.

KD



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 = bis.read (buffer, 0, 4096);
if (endOfResultSet == -1)
break;
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() );
//Thread.sleep(500);

} //while
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic