writeBits method Null safety

void writeBits(
  1. int numBits,
  2. int value
)

Implementation

void writeBits(int numBits, int value) {
  // TODO optimize
  if (_bitPos == 8 && numBits == 8) {
    output.writeByte(value & 0xff);
    return;
  }

  if (_bitPos == 8 && numBits == 16) {
    output.writeByte((value >> 8) & 0xff);
    output.writeByte(value & 0xff);
    return;
  }

  if (_bitPos == 8 && numBits == 24) {
    output.writeByte((value >> 16) & 0xff);
    output.writeByte((value >> 8) & 0xff);
    output.writeByte(value & 0xff);
    return;
  }

  if (_bitPos == 8 && numBits == 32) {
    output.writeByte((value >> 24) & 0xff);
    output.writeByte((value >> 16) & 0xff);
    output.writeByte((value >> 8) & 0xff);
    output.writeByte(value & 0xff);
    return;
  }

  while (numBits > 0) {
    numBits--;
    final b = (value >> numBits) & 0x1;
    _bitBuffer = (_bitBuffer << 1) | b;
    _bitPos--;
    if (_bitPos == 0) {
      output.writeByte(_bitBuffer);
      _bitPos = 8;
      _bitBuffer = 0;
    }
  }
}