readVarLong method

Pair<int, int> readVarLong({
  1. bool signed = false,
})
inherited

Reads a 8 byte variable length long starting at readPosition. The format of this long will be as to LEB128, or better what Minecraft uses. Returns a Pair, in which the first integer is the read value and the second the length of the var-integer in bytes.

Implementation

Pair<int, int> readVarLong({bool signed = false}) {
  var index = readPosition;
  var value = 0;
  for (var i = 0; i < 64; i += 7) {
    final byte = readByteData!.getUint8(index++);
    value |= (0x7F & byte) << i;
    if (0x80 & byte == 0) break;
  }
  var length = index - readPosition;
  readPosition = index;

  if (signed) {
    final rem = value % 2;
    return Pair(rem == 0 ? value ~/ 2 : (value ~/ -2) - 1, length);
  } else {
    return Pair(value, length);
  }
}