Another of those JavaProblems...
If you write byte values to an OutputStream you pass an int value between -128 and +127. However, if you read byte values from an InputStream you get an int value between 0 and 255.
If you want to perform calculations on the int representation of a byte value read from an InputStream, cast to a byte and then back to an int to perform sign extension.
E.g:
int value_read = in.read(); if( value_read == -1 ) { // handle EOF } int byte_value = (int)(byte)value_read; // Perform calculations on the value of the byte