decode method

RitoResource decode(
  1. Uint8List bytes
)

Implementation

RitoResource decode(Uint8List bytes) {
  if (bytes.length > ritoMaxWireBytes) {
    throw const FormatException('RITORES1 exceeds the byte limit.');
  }
  final reader = RitoBinaryReader(bytes);
  reader.expectMagic(_magic, 'resource magic');
  final version = reader.uint32('resource wire version');
  if (version != 1) {
    reader.fail('unsupported resource wire version: $version');
  }
  final declaredLength = reader.uint64('resource total length');
  if (declaredLength != bytes.length) {
    reader.fail('resource total length does not match input');
  }
  final artifactId = reader.externalId('resource artifact id');
  final kind = _kind(reader);
  final resource = RitoResource(
    artifactId: artifactId,
    kind: kind,
    href: reader.string('resource href'),
    mediaType: reader.string('resource media type'),
    // The returned view keeps the owned wire buffer alive and avoids one
    // full resource copy between FFI transfer and the platform decoder.
    bytes: reader.blobView('resource bytes', maxBytes: _byteLimit(kind)),
    width: reader.option('resource width', () => reader.uint32('width')),
    height: reader.option('resource height', () => reader.uint32('height')),
  );
  reader.finish('resource wire message');
  return resource;
}