A focused guide on mapping strings and arrays: how offset and length work, element endianness, and common pitfalls.
Strings and arrays are fixed-length in FastProto. You declare the starting byte offset and an explicit length (in bytes for strings, in elements for arrays). During encode/decode, FastProto reads/writes exactly that span.
offset for the field.length (bytes). For arrays, specify length (elements).byteOrder on the array annotation.length is authoritative.length will be truncated to fit.byteOrder applies per element.@StringType(offset = <byteOffset>, length = <byteLength>)offset: first byte indexlength: number of bytes to read/write for the string@BinaryType(offset = <byteOffset>, length = <byteLength>)@UInt8ArrayType(offset = <byteOffset>, length = <count>)
@UInt16ArrayType(offset = <byteOffset>, length = <count>, byteOrder = {ByteOrder.BIG})
@UInt32ArrayType(offset = <byteOffset>, length = <count>, byteOrder = {ByteOrder.LITTLE})
@UInt64ArrayType(offset = <byteOffset>, length = <count>, byteOrder = {ByteOrder.BIG})length: number of elementsbyteOrder: per‑element endiannessimport org.indunet.fastproto.annotation.*;
public class Msg {
@StringType(offset = 0, length = 16)
String name; // fixed 16 bytes, producer should zero‑pad
}public class Payload {
@BinaryType(offset = 16, length = 32)
byte[] body; // 32 bytes of opaque data
}import org.indunet.fastproto.ByteOrder;
public class Data {
@UInt16ArrayType(offset = 0, length = 4, byteOrder = {ByteOrder.BIG})
int[] values; // 4 elements, each 2 bytes in big‑endian
}public class Frame {
@StringType(offset = 0, length = 8)
String topic;
@UInt8ArrayType(offset = 8, length = 12)
int[] flags; // 12 bytes
}encode(object, totalLength). Arrays/strings still need a concrete length at mapping time.FastProto.encode(obj, length)) so trailing regions can be addressed safely.byteOrder explicitly if your spec requires it.