toBytes method

Uint8List toBytes()

Serializes the Bloom filter to bytes for transmission.

Wire format:

[0xBF, 0x00] [bitCount: 4 bytes LE] [hashCount: 1 byte] [bit array...]

Total overhead: 7 bytes. The bit array is the dominant size.

Implementation

Uint8List toBytes() {
  final header = ByteData(7);
  // Magic
  header.setUint8(0, kBloomFilterMagic[0]);
  header.setUint8(1, kBloomFilterMagic[1]);
  // Bit count (little-endian 32-bit)
  header.setUint32(2, _bitCount, Endian.little);
  // Hash count
  header.setUint8(6, _hashCount);

  final result = Uint8List(7 + _bits.length);
  result.setAll(0, header.buffer.asUint8List());
  result.setAll(7, _bits);
  return result;
}