简体   繁体   中英

Why does InputStreamReader read() block instead of returning -1?

I'm using java's InputStreamReader read() function. When I reach the end of the input stream i'm supposed to get in to my int variable the value of -1, but instead it goes to block. Why don't I get a -1 at the end of the input stream? (i've debugged it letter by letter making sure it is actualy the end of the input and that the connection socket is alive).

Is using the ready() function a good solution by doing:

if (isr.ready())
    currCharVal = isr.read();

Thanks in advance, Guy.

This would happen if the other end is not closing the connection. When the socket is closed, read() will return a -1.

Using ready and available is rather unpredictable in my experience. I would just read(byte[]) until the end is reached and expect the other end to close when finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM