If you know you are reading a message, but have already read the length of the message OR you're reading fields of the top level data, then this method is the alternative. It's often used by sub-classes So that it can be instationated prior to reading the message.
user defined input function to parse the message fields
The class to mutate given field data
the end position of the message in the buffer
Ex.
export class MapboxVectorLayer {
constructor(pbf: Protobuf, end: number) {
this.#pbf = pbf;
pbf.readFields(this.#readLayer, this, end);
}
#readLayer(tag: number, layer: MapboxVectorLayer, pbf: Protobuf): void {
if (tag === 15) layer.version = pbf.readVarint();
else if (tag === 1) layer.name = pbf.readString();
// ...
}
}
Reads a message from the buffer. Sometimes it's easier to manage sub structures so that the current method can build multiples of an entire structure/class. If you you are at the top level, or parsing the message inside a class, then
user defined input function
an instance of the class you are reading into
readVarint for better performance
Write a packed repeated byte array to the buffer with an associated tag.
the tag to write to associate with the value.
the buffer of bytes to write.
Write a message to the buffer. Allows you to pass in an object with a write function to define how the message should be written. A good tool to abstract away storing classes or sub-classes.
Write a packed repeated boolean array to the buffer.
Supports numbers: 0
is false, everything else is true.
the tag to write to associate with the value.
the array of booleans to write.
Protobuffer Reader and Writer
Description
Create a new PBF instance and either read or write to it. Follows the early Protobuf spec supporting various types of encoding including messages (which are usually representative of class objects).
Usage
Reading:
Writing: