aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "simple question on  urlConnection.getInputStream();" Watch "simple question on  urlConnection.getInputStream();" New topic
Author

simple question on urlConnection.getInputStream();

kevind duff
Greenhorn

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
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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: http://aspose.com/file-tools
 
subject: simple question on urlConnection.getInputStream();