VarInt.parse constructor

VarInt.parse(
  1. Uint8List data, {
  2. int start = 0,
})

Given an Array of Bytes, parse it and create a VarInt instance from the specified starting index.

If not provided the start index is the beginning of the array.

Implementation

factory VarInt.parse(Uint8List data, {int start = 0})  {
  assert(data.isNotEmpty && data.length > start);
  final segment = data.sublist(start);
  final int firstByte = segment.buffer.asByteData().getUint8(0);
  switch(firstByte) {
    case 0xFD: return VarInt._(value: segment.buffer.asByteData().getInt16(1), flag: firstByte);
    case 0xFE: return VarInt._(value: segment.buffer.asByteData().getInt32(1), flag: firstByte);
    case 0xFF: return VarInt._(value: segment.buffer.asByteData().getInt64(1), flag: firstByte);
    default: return VarInt._(value: firstByte, flag: firstByte);
  }
}