encodeVarint method

Uint8List encodeVarint(
  1. int value
)

Encodes an integer to data using variable-length encoding technique (varint) format.

Implementation

Uint8List encodeVarint(int value) {
  final List<int> bytes = <int>[];
  int cur = value;
  while (cur != 0) {
    bytes.add(cur & 127);
    cur >>= 7;
  }

  int i = bytes.length - 1;
  if (i == 0) {
    return Uint8List.fromList(bytes);
  }

  final List<int> modifiedBytes = <int>[];

  while (i >= 0) {
    int val = bytes[i];
    if (i > 0) {
      val = val | 0x80;
    }
    modifiedBytes.add(val);
    i--;
  }

  return Uint8List.fromList(modifiedBytes);
}