decode static method

Frame decode(
  1. Uint8List bytes, [
  2. int offset = 0
])

Decodes a single frame from bytes starting at offset. Throws FormatException if bytes is too short for header or payload.

Implementation

static Frame decode(Uint8List bytes, [int offset = 0]) {
  final available = bytes.length - offset;
  if (available < 3) {
    throw FormatException(
        'Insufficient bytes for frame header: $available < 3');
  }
  final bdata = ByteData.sublistView(bytes, offset);
  final type = bdata.getUint8(0);
  final length = bdata.getUint16(1, Endian.big);
  if (available < 3 + length) {
    throw FormatException(
        'Insufficient bytes for frame payload: $available < ${3 + length}');
  }
  final payload =
      Uint8List.sublistView(bytes, offset + 3, offset + 3 + length);
  return Frame(type: type, payload: payload);
}