Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading inputstream is not finding end of file 1

Status
Not open for further replies.

akenty

Programmer
Jan 10, 2003
18
CA
Hi All,
I've tried a number of different ways to read an inputstream and cannot get the -1 for the end of file to trip. Stepped through the code and got to the final carriage return in the stream, but when the next read() is performed, the code goes into neverland. Does not produce an error and seems like it is looping, but I know it isn't (set a breakpoint property and paused it every now and then and the loop counter does not increment). Code is below, any help/suggestions anyone can provide would be greatly appreciated. Thanks

Code:
    private byte[] receiveACK(InputStream in, PrintStream p, int portNumber) throws Exception {

        int nextByte = 0;
        int loopCount;
 
        ByteArrayOutputStream tokenBuffer = new ByteArrayOutputStream();
        
        loopCount = 0;
       
// The following would not find the end of the file.         
//        do {
//            loopCount++;
//            tokenBuffer.write(nextByte);
//            byte[] currentToken = tokenBuffer.toByteArray();
//        } while ((nextByte = in.read()) != -1);

// Alternate code for the above.
        nextByte = in.read();
        while (nextByte != -1){
            log.error("Next byte is: " + nextByte);
            loopCount++;
            tokenBuffer.write(nextByte);
            // Last byte would act like it is looping, but is not (set breakpoint property 
            // and paused and loopCount remains the same), no errors displayed.
            nextByte = in.read();			
        }

        byte[] currentToken = tokenBuffer.toByteArray();
        
        if (currentToken.length > 0) {
            byte[] token = new byte[currentToken.length];
            System.arraycopy(currentToken, 0, token, 0, currentToken.length);
            return token;
        }

        return null;
    }
 
I think the "end of the trip" is null, not -1

_________________
Bob Rashkin
 
from
If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b

Might want to wrap your code in a try catch

Marty
 
To correct the problem, I used the available() method. It seems that the read() could not detect the end of the file, but the available() was able to see that there were no more bytes to be read. Just speculating that is what is happening, I'm a little green to java.

Code:
        while (nextByte != -1){
            log.error("Next byte is: " + nextByte);
            loopCount++;
            tokenBuffer.write(nextByte);
            nextByte = in.read();
[COLOR=red]            if (in.available()== 0)  break;[/color]
            
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top