org.openlr.binary.reader.IntegerReader Maven / Gradle / Ivy
The newest version!
package org.openlr.binary.reader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
class IntegerReader {
int readSignedInteger(int numberOfBytes, ByteArrayInputStream inputStream) throws IOException {
byte[] bytes = inputStream.readNBytes(numberOfBytes);
int value = 0;
for (int i = 0; i < numberOfBytes; i++) {
value = ((bytes[i] & 0xff) << ((numberOfBytes - i - 1) * 8)) | value;
}
if (((bytes[0] & 0xff) >> 7) == 1) {
value -= (1 << (numberOfBytes * 8));
}
return value;
}
int readUnsignedInteger(int numberOfBytes, ByteArrayInputStream inputStream) throws IOException {
byte[] bytes = inputStream.readNBytes(numberOfBytes);
int value = 0;
for (int i = 0; i < numberOfBytes; i++) {
value = ((bytes[i] & 0xff) << ((numberOfBytes - i - 1) * 8)) | value;
}
return value;
}
}