readBits method
Implementation
int readBits(int numBits) {
if (numBits < 1 || numBits > 32 || numBits > available) throw const UCodeDecodeException("Bit source overrun");
int result = 0;
int remaining = numBits;
if (_bitOffset > 0) {
final int bitsLeft = 8 - _bitOffset;
final int toRead = remaining < bitsLeft ? remaining : bitsLeft;
final int bitsToNotRead = bitsLeft - toRead;
final int mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
result = (bytes[_byteOffset] & mask) >> bitsToNotRead;
remaining -= toRead;
_bitOffset += toRead;
if (_bitOffset == 8) {
_bitOffset = 0;
_byteOffset++;
}
}
if (remaining > 0) {
while (remaining >= 8) {
result = (result << 8) | (bytes[_byteOffset] & 0xFF);
_byteOffset++;
remaining -= 8;
}
if (remaining > 0) {
final int bitsToNotRead = 8 - remaining;
final int mask = (0xFF >> bitsToNotRead) << bitsToNotRead;
result = (result << remaining) | ((bytes[_byteOffset] & mask) >> bitsToNotRead);
_bitOffset += remaining;
}
}
return result;
}