getVariableEncInt method

Tuple2<BigInt, int> getVariableEncInt(
  1. int startOffset
)

Lê um inteiro length‑encoded a partir de startOffset.

O formato length‑encoded é definido pelo primeiro byte:

  • Se for < 0xfb, esse próprio byte é o valor.
  • Se for 0xfc, lê 2 bytes (uint16).
  • Se for 0xfd, lê 3 bytes (uint24).
  • Se for 0xfe, lê 8 bytes (uint64).

Retorna uma Tuple2:

  • item1: valor lido como BigInt.
  • item2: número total de bytes consumidos (1 para o header, +2/+3/+8, dependendo do caso).

Implementation

Tuple2<BigInt, int> getVariableEncInt(int startOffset) {
  final firstByte = getUint8(startOffset);

  if (firstByte < 0xfb) {
    // Valor direto (ex.: 0x05).
    return Tuple2(BigInt.from(firstByte), 1);
  }

  if (firstByte == 0xfc) {
    return Tuple2(BigInt.from(getUint16(startOffset + 1, Endian.little)), 3);
  }

  if (firstByte == 0xfd) {
    final value = getUint16(startOffset + 1, Endian.little) |
        (getUint8(startOffset + 3) << 16);
    return Tuple2(BigInt.from(value), 4);
  }

  if (firstByte == 0xfe) {
    final low = getUint32(startOffset + 1, Endian.little);
    final high = getUint32(startOffset + 5, Endian.little);
    return Tuple2((BigInt.from(high) << 32) | BigInt.from(low), 9);
  }

  throw MySQLProtocolException(
    "Wrong first byte, while decoding getVariableEncInt",
  );
}