I was looking over an old program that somone had coded and im not sure of this loop. I would ask the person who coded this but i havent seen him in a while. I understand the logic but im not sure of the use of the loop.
If an array is constructed with 10 elements ie... int[9] then on the first iteration 11 bytes will be read since the offset is 0 and data length is 10. So an ArrayOutOfBounds Exception should be thrown. Right? I see that on the next line offset will be greater than data.length because 11 bytes have been read.
Also the condition is being tested before bytesRead is stored in offset. So the first time around offset will still have the value 0. Shouldn't offset += bytesread; come first?
code:--------------------------------------------------------------------------------
int[] data = new int[9]
int offset = 0;
int bytesRead = 0;
while(true){
bytesRead = in.read(data, offset, data.length-offset);
if(bytesRead == -1 || offset > data.length) break;
offset += bytesread;
}
Thanks for the help!
If an array is constructed with 10 elements ie... int[9] then on the first iteration 11 bytes will be read since the offset is 0 and data length is 10. So an ArrayOutOfBounds Exception should be thrown. Right? I see that on the next line offset will be greater than data.length because 11 bytes have been read.
Also the condition is being tested before bytesRead is stored in offset. So the first time around offset will still have the value 0. Shouldn't offset += bytesread; come first?
code:--------------------------------------------------------------------------------
int[] data = new int[9]
int offset = 0;
int bytesRead = 0;
while(true){
bytesRead = in.read(data, offset, data.length-offset);
if(bytesRead == -1 || offset > data.length) break;
offset += bytesread;
}
Thanks for the help!