encodeBound static method

Uint8List encodeBound(
  1. int timestamp,
  2. Uint8List idPrefix, {
  3. int lastTimestamp = 0,
})

Encodes a bound. Timestamps are delta encoded against lastTimestamp, which is the timestamp of the previously encoded bound in the same message (0 at the start of every message).

Implementation

static Uint8List encodeBound(
  int timestamp,
  Uint8List idPrefix, {
  int lastTimestamp = 0,
}) {
  if (idPrefix.length > idSize) {
    throw ArgumentError('Bound ID prefix must be at most $idSize bytes');
  }

  final output = BytesBuilder();

  if (timestamp >= infiniteTimestamp) {
    output.addByte(0);
  } else {
    if (timestamp < lastTimestamp) {
      throw ArgumentError('Bound timestamps must be non-decreasing');
    }
    output.add(encodeVarint(timestamp - lastTimestamp + 1));
  }

  output.add(encodeVarint(idPrefix.length));
  output.add(idPrefix);

  return output.toBytes();
}