Java: InputStream read () returns a byte greater than 127?

I have this code:

InputStream is = socket.getInputStream();
int b;
while ((b = is.read()) != -1)
{
   System.out.println(b);
}

The byte of its range is -128up to +127.
But one of the bytes printed is 210.

Is this the result of converting read byteinto int?
(So ​​negatif bytebecomes positive int)
If yes, can I do the same (c OutputStream), convert intto byte?

Thanks
Martine

+3
source share
1 answer

Actually readreturns an integer.

public abstract int read() throws IOException

therefore it is pushed to be an unsigned byte, storing it in int.

:

. int 0 255. , , -1. ​​

, , -1 .

OutputStream

public abstract void write(int b) throws IOException

, , 8 .

+9

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


All Articles