I’ve this code:
InputStream is = socket.getInputStream();
int b;
while ((b = is.read()) != -1)
{
System.out.println(b);
}
A byte its range is -128 until +127.
But one of the printed bytes is 210.
Is this the result of converting the read byte to an int?
(So that the negatif byte becomes a positif int)
If so, can I do the same (with an OutputStream) by converting an int to a byte?
Thanks,
Martijn
Actually
readreturns an integer..so it’s implictly casted to be unsigned byte by storing it in an int.
As stated in documentation:
Think about the fact that if it’s a signed byte then
-1couldn’t be used as end of stream value.For
OutputStreamyou haveand as stated by documentation implementation will take 8 low order bits of the integer passed..