encode method
The bytes, in the layout decode reads.
Little-endian throughout. Strings are a uint32 byte length and UTF-8;
a stage is a string and one byte, 1 for fragment; a section is a string
id, a uint32 length and the bytes.
Implementation
ByteData encode() {
final out = BytesBuilder(copy: false);
void u8(int value) => out.addByte(value);
void u32(int value) {
out.add(
Uint8List(4)..buffer.asByteData().setUint32(0, value, Endian.little),
);
}
void string(String value) {
final bytes = utf8.encode(value);
u32(bytes.length);
out.add(bytes);
}
out.add(ascii.encode(magic));
u32(formatVersion);
string(name);
string(sdk);
u32(stages.length);
for (final stage in stages) {
string(stage.name);
u8(stage.fragment ? 1 : 0);
}
u32(sections.length);
for (final entry in sections.entries) {
string(entry.key);
final bytes = entry.value;
u32(bytes.lengthInBytes);
out.add(
bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes),
);
}
return out.takeBytes().buffer.asByteData();
}