Results 1 to 1 of 1

Thread: Unsure of this loop [Resolved]

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question Unsure of this loop [Resolved]

    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; 
       }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width