toPacket method

Uint8List toPacket(
  1. Uint8List payload,
  2. Random random,
  3. int blockSize
)

Implementation

Uint8List toPacket(Uint8List payload, Random random, int blockSize) {
  Uint8List buffer = Uint8List(nextMultipleOfN(
      4 + BinaryPacket.headerSize + payload.length, max(8, blockSize)));
  SerializableOutput output = SerializableOutput(buffer);
  int padding = buffer.length - BinaryPacket.headerSize - payload.length;
  output.addUint32(buffer.length - 4);
  output.addUint8(padding);
  output.addBytes(payload);
  output.addBytes(randBytes(random, padding));
  if (!output.done) {
    throw FormatException('${output.offset}/${output.buffer.length}');
  }
  return buffer;
}