writeVariableEncInt method

void writeVariableEncInt(
  1. int value
)

Escreve um inteiro com codificação length‑encoded.

  • Se value for menor que 251, escreve-o como 1 byte.
  • Se value estiver entre 251 e 65535, escreve o marcador 0xfc seguido de 2 bytes.
  • Se value estiver entre 65536 e 16777215, escreve o marcador 0xfd seguido de 3 bytes.
  • Se value for maior ou igual a 16777216, escreve o marcador 0xfe seguido de 8 bytes.

Implementation

void writeVariableEncInt(int value) {
  if (value < 251) {
    // Valor de 1 byte.
    writeUint8(value);
  } else if (value >= 251 && value < 65536) {
    // 0xfc + 2 bytes.
    writeUint8(0xfc);
    writeInt16(value);
  } else if (value >= 65536 && value < 16777216) {
    // 0xfd + 3 bytes.
    writeUint8(0xfd);
    final bd = ByteData(4);
    bd.setInt32(0, value, Endian.little);
    write(bd.buffer.asUint8List().sublist(0, 3));
  } else if (value >= 16777216) {
    // 0xfe + 8 bytes.
    writeUint8(0xfe);
    writeInt64(value);
  }
}