dvFlatDecode function

Object? dvFlatDecode(
  1. Uint8List buffer
)

Decodes a buffer produced by dvFlatEncode.

Throws FormatException for anything that is not exactly one well-formed buffer, including a truncation, an out-of-range length, an unknown tag, and trailing bytes after the root value.

Implementation

Object? dvFlatDecode(Uint8List buffer) {
  if (buffer.length < dvFlatHeaderLength) {
    throw const FormatException('Not a Dartvel flat buffer: too short.');
  }
  for (int i = 0; i < _magic.length; i += 1) {
    if (buffer[i] != _magic[i]) {
      throw const FormatException('Not a Dartvel flat buffer: bad magic.');
    }
  }
  if (buffer[4] != _version) {
    throw FormatException(
      'Unsupported Dartvel flat buffer version ${buffer[4]}.',
    );
  }

  final _Reader reader = _Reader(buffer, dvFlatHeaderLength);
  final Object? value = _readValue(reader);
  if (!reader.atEnd) {
    // Ignoring these would let two different buffers decode alike.
    throw FormatException(
      '${reader.remaining} trailing byte(s) after the value.',
    );
  }
  return value;
}