readBits method Null safety

int readBits(
  1. int numBits
)

Read a number of bits from the input stream.

Implementation

int readBits(int numBits) {
  if (numBits == 0) {
    return 0;
  }

  if (_bitPos == 0) {
    _bitPos = 8;
    _bitBuffer = input.readByte();
  }

  var value = 0;

  while (numBits > _bitPos) {
    value = (value << _bitPos) + (_bitBuffer & _bitMask[_bitPos]);
    numBits -= _bitPos;
    _bitPos = 8;
    _bitBuffer = input.readByte();
  }

  if (numBits > 0) {
    if (_bitPos == 0) {
      _bitPos = 8;
      _bitBuffer = input.readByte();
    }

    value = (value << numBits) +
        (_bitBuffer >> (_bitPos - numBits) & _bitMask[numBits]);

    _bitPos -= numBits;
  }

  return value;
}