messageLengthFromHeader static method

int messageLengthFromHeader(
  1. Uint8List data
)

Implementation

static int messageLengthFromHeader(Uint8List data) {
  if (data.length < 6) {
    throw const FormatException('Buffer too small for version');
  }
  final byteData = ByteData.sublistView(data);
  final version = byteData.getUint16(4, Endian.little);
  if (version == protocolVersionRowMajor) {
    if (data.length < headerSizeV1) {
      throw const FormatException('Buffer too small for header');
    }
    final payloadSize = byteData.getUint32(12, Endian.little);
    return headerSizeV1 + payloadSize;
  }
  if (version == protocolVersionColumnarV2) {
    if (data.length < headerSizeColumnarV2) {
      throw const FormatException('Buffer too small for columnar v2 header');
    }
    final payloadSize = byteData.getUint32(15, Endian.little);
    return headerSizeColumnarV2 + payloadSize;
  }
  throw FormatException('Unsupported protocol version: $version');
}