Home arrow static arrow Java Programming [Archive] - what does this do:
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - what does this do:
This topic has 2 replies on 1 page.

Posts:58
Registered: 7/6/04
what does this do:  
Jul 13, 2004 6:21 PM



 
im tryng to understand this peice of code.

a file is being read in which is 1073844 in size. now how much of that is written to
ab? at the end when i print i1 it equals 1073844. so is 1073844 read into ab or only
1024000 of the 1073844?

ab = new byte [1024000];

int i1 = 0;
int j1 = 0;

for (bufferedInputStream = new BufferedInputStream(uRLConnection.getInputStream());
(j1 = bufferedInputStream.read(ab, 0, 1024000)) != -1; )
i1 += j1;

// i1 is the size of the test file. [1073844] when printed at the end.

 

Posts:1,183
Registered: 1/23/02
Re: what does this do:  
Jul 13, 2004 6:37 PM (reply 1 of 2)



 
Yeeeah. If that were written by a coworker of mine I'd attempt to break his wrists.

See the [url http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[], int, int)]API documentation[/url] for a full explanation of what the read() method's parameters mean.

The effect of this code is not completely predictable. It reads between 1 and 1024000 on each iteration of the loop. On each iteration it writes whatever data it has read at the start of array ab. So in the end 1073844 bytes have been read from the URL connection, but these bytes are overlayed on top of each other in an unpredicatble way in ab.
 

Posts:1,183
Registered: 1/23/02
Re: what does this do:  
Jul 13, 2004 6:42 PM (reply 2 of 2)



 
It reads between 1 and 1024000

It reads between 1 and 1024000 bytes from the URLConnection, that is. The loop ends when there are no bytes left to be read (e.g. end of file).
 
This topic has 2 replies on 1 page.