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

I am using java InputStreamReader read (). When I get to the end of the input stream, I have to enter -1 into my int variable, but instead it goes into a block. Why am I not getting -1 at the end of the input stream? (I debugged this letter by letter, making sure that this is really the end of the input and that the connection connector is alive).

Uses the ready () function - a good solution, making:

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

Thanks in advance, Guy.

+4
source share
1 answer

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

Using ready-made and available in my experience is quite unpredictable. I would just read (byte []) until the end is reached, and expect the end to be closed when finished.

+4
source

Source: https://habr.com/ru/post/1392215/


All Articles