The problem is that a byte to in cast in java is done with two complements values (e.g. 0xFF becomes 0xFFFFFFFF), therefore every value greater as 0x7F will be seen as negative value because the highest bit is set. This violates the contract for read(), which states that read() will return a value in the range from 0 to 254.
The read() method returns a to int casted byte.
The problem is that a byte to in cast in java is done with two complements values (e.g. 0xFF becomes 0xFFFFFFFF), therefore every value greater as 0x7F will be seen as negative value because the highest bit is set. This violates the contract for read(), which states that read() will return a value in the range from 0 to 254.