readSignedInt method

int readSignedInt()

Reads a variable-length signed integer using modified LEB128 encoding.

Similar to unsigned variant, but the last byte uses 6 data bits and 1 sign bit. The sign bit (bit 6) in the final byte determines if the value is negative.

Returns the decoded signed integer value

Implementation

int readSignedInt() {
  int variableByteDecode = 0;
  int variableByteShift = 0;

  // Optimized signed variable-length integer decoding
  int pos = _bufferPosition;
  final data = _bufferData;

  // check if the continuation bit is set
  while ((data[pos] & 0x80) != 0) {
    variableByteDecode |= (data[pos] & 0x7f) << variableByteShift;
    variableByteShift += 7;
    ++pos;
  }

  variableByteDecode |= (data[pos] & 0x3f) << variableByteShift;

  // read the six data bits from the last byte
  if ((data[pos] & 0x40) != 0) {
    // negative
    _bufferPosition = pos + 1;
    return -variableByteDecode;
  }
  // positive
  _bufferPosition = pos + 1;
  return variableByteDecode;
}