decodeVectorBlob function

Vector decodeVectorBlob(
  1. List<int> bytes
)

Decode a vector BLOB previously produced by encodeVectorBlob. Throws FormatException if bytes does not have the expected size or dim header.

Implementation

Vector decodeVectorBlob(List<int> bytes) {
  if (bytes.length < 4) {
    throw const FormatException('vector blob too short (need 4-byte header)');
  }
  final u8 = bytes is Uint8List ? bytes : Uint8List.fromList(bytes);
  final bd = ByteData.sublistView(u8);
  final dim = bd.getUint32(0, Endian.little);
  final expected = 4 + 4 * dim;
  if (u8.length != expected) {
    throw FormatException(
      'vector blob length ${u8.length} does not match dim=$dim '
      '(expected $expected bytes)',
    );
  }
  final out = Float32List(dim);
  for (var i = 0; i < dim; i++) {
    out[i] = bd.getFloat32(4 + 4 * i, Endian.little);
  }
  return Vector(out);
}