serialize method

ByteBuffer serialize()

Serializes the ComplexStruct to a ByteBuffer.

Returns a ByteBuffer containing the serialized data.

Implementation

ByteBuffer serialize() {
  final buffer = ByteData(sizeOf<ComplexStruct>());
  var offset = 0;

  // Write id
  buffer.setInt32(offset, id, Endian.host);
  offset += sizeOf<Int32>();

  // Write start point
  final startBuffer = start.serialize();
  final startData = ByteData.view(startBuffer);
  for (var i = 0; i < sizeOf<Point>(); i++) {
    buffer.setUint8(offset + i, startData.getUint8(i));
  }
  offset += sizeOf<Point>();

  // Write end point
  final endBuffer = end.serialize();
  final endData = ByteData.view(endBuffer);
  for (var i = 0; i < sizeOf<Point>(); i++) {
    buffer.setUint8(offset + i, endData.getUint8(i));
  }
  offset += sizeOf<Point>();

  // Write data array
  for (var i = 0; i < 10; i++) {
    buffer.setInt32(offset, data[i], Endian.host);
    offset += sizeOf<Int32>();
  }

  return buffer.buffer;
}