parse static method

List<Uint8List> parse(
  1. Uint8List input
)

Implementation

static List<Uint8List> parse(Uint8List input) {
  final List<Uint8List> result = [];
  final uint8Array = input;
  const maxLengthPrefixSize = 5;
  const numBitsToShift = [0, 7, 14, 21, 28];

  for (var offset = 0; offset < input.length;) {
    var numBytes = 0;
    var size = 0;
    var byteRead;
    do {
      byteRead = uint8Array[offset + numBytes];
      size = size | ((byteRead & 0x7f) << (numBitsToShift[numBytes]));
      numBytes++;
    } while (numBytes < min(maxLengthPrefixSize, input.length - offset) &&
        (byteRead & 0x80) != 0);

    if ((byteRead & 0x80) != 0 && numBytes < maxLengthPrefixSize) {
      throw new Exception("Cannot read message size.");
    }

    if (numBytes == maxLengthPrefixSize && byteRead > 7) {
      throw new Exception("Messages bigger than 2GB are not supported.");
    }

    if (uint8Array.length >= (offset + numBytes + size)) {
      result.add(
          uint8Array.sublist(offset + numBytes, offset + numBytes + size));
    } else {
      throw Exception("Incomplete message.");
    }

    offset = offset + numBytes + size;
  }

  return result;
}