Home arrow static arrow Java Programming [Archive] - question about read(byte[] b)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - question about read(byte[] b)
This topic has 3 replies on 1 page.

Posts:445
Registered: 4/17/03
question about read(byte[] b)  
Aug 4, 2004 8:06 AM



 
just a pretty simple question really, i just really need to get this straight. I just want to confirm this hunch.
So, using the read function from FileInputStream or ZipInputStream, this function will not return -1 unless
ALL bytes have been read from the stream, right? So, say i have this
byte b[] = byte[10];int result = 0; while((result = FIS.read(b)) != -1){    FIS.read(b);    System.out.println("Result: " + result);}


now, suppose FIS is an input stream with 35 bytes. My output should be:
Result: 10
Result: 10
Result: 10
Result: 5

and after it reads the last 5 it will output a -1 on the next read, right? I know this sounds intuitive, but i want
to make sure that FIS would never output -1 BEFORE all bytes are read. Hence, the file pointer is always
scooted right up to the very end on the last read, and on any subsequent reads a -1 will be returned, is this
right? thanks
 

Posts:14,142
Registered: 99-04-02
Re: question about read(byte[] b)  
Aug 4, 2004 8:25 AM (reply 1 of 3)



 
it'll return -1 when no more bytes can be read... I guess that could mean the same thing... so yeah..

Yes.

But the output could easily be:

Result: 5
Result: 5
Result: 5
Result: 5
Result: 5
Result: 5
Result: 5

or

Result: 15
Result: 15
Result: 5

or

Result: 6
Result: 13
Result: 8
Result: 8

But yes, once you get a -1 as the number of bytes read, it'll have already have put the last actual bytes into the byte array the call before.
 

Posts:14,142
Registered: 99-04-02
Re: question about read(byte[] b)  
Aug 4, 2004 8:26 AM (reply 2 of 3)



 
well, if the byte array is 10 bytes long, of course, it can't be more then 10.. duh. But it could be less, which is why you need "result"
 

Posts:445
Registered: 4/17/03
Re: question about read(byte[] b)  
Aug 4, 2004 8:33 AM (reply 3 of 3)



 
bsampieri,
thanks, that is what i suspected. I am glad to see it won't read 15 bytes into a 10 byte array! =) i really
appreciate the help!
 
This topic has 3 replies on 1 page.