I agree that reading and writing bytes is what you want here. To get around the "sign extension" problem requires an extra operation...
To recap, most integral types in Java are signed:
byte - signed 8-bit (-128..127)
short - sign 16-bit (-32768..32767)
char - unsigned 16-bit (0..65535)
(mostly for unicode character set, but you can use them as a numeric type)
int - signed 32-bit (-2147483648..2147483647)
long - signed 64-bit (0x8000000000000000L..0x7fffffffffffffffL) (the decimal values are getting less useful here!)
Compared to C, Java is nice in that the limits for each type are the same on every platform. BUT - it's much more of a pain to deal with unsigned values in Java. Also note that Java's "char" is nothing like a C "char".
To deal with unsigned values in Java you need to promote the type to a "larger" type, then mask the value back to an 8 (or 16, or 32) bit range.
BYTE:
int unsignedVal = signedValue & 0xFF;
SHORT:
int unsignedVal = signedValue & 0xFFFF;
INT:
long unsignedVal = signedValue & 0x0FFFFFFFFL;
To extend buchanan's RawReader example:
Code:
InputStream in = s.getInputStream();
for (int i = in.read(); i != -1; i = in.read()) { // read signed byte
i = i & 0xFF; // convert to unsigned byte value
System.out.print(i + " ");
}
System.out.println();