Interface Visitor
IStream.
The decoder follows the visitor pattern: rather than binding a
destination buffer per field (as the C API does), it calls back into a
Visitor as each field is decoded. Every method has a default no-op
implementation, so an implementor overrides only the field kinds it cares
about; unhandled fields are simply dropped (the equivalent of "not
interested" / skip in the C API). This keeps generated message classes small:
a generated Visitor is typically one switch on the field id.
Streaming contract. Scalars and floats are delivered whole. String
and blob payloads are delivered in one or more chunks so they can exceed the
input chunk size (and even RAM); each chunk reports the field total
length and the byte offset of the chunk within the field. Array
elements are announced once via arrayBegin(int, ArrayKind, int) and then delivered through
the scalar / float callbacks with the same id. A fixlen field —
string, blob, fp32 or fp64 — is announced once via fixlenBegin(int, FixlenType, int) at its
length word, before any payload byte.
Buffer ownership. The data array handed to string(int, int, int, byte[], int, int)
and blob(int, int, int, byte[], int, int) is the caller's input buffer; it is only valid for the
duration of the call. A visitor that needs to retain bytes must copy the
[chunkOffset, chunkOffset + chunkLength) range.
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidarrayBegin(int id, ArrayKind kind, int count) Start of an array field.default ObjectOffer to take an integer array's elements in bulk instead of one call at a time.default voidarrayBulkEnd(int id, int n) The bulk fill offered atarrayBulk(int, ArrayKind, int)is complete:nelements were written into the destination.default voidblob(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength) A chunk of a blob field.default voidfixlenBegin(int id, FixlenType subtype, int total) Start of a fixlen field — string, blob, fp32 or fp64.default voidfp32(int id, float value) A 32-bit float field, or anfp32array element.default voidfp64(int id, double value) A 64-bit float field, or anfp64array element.default voidsequenceBegin(int id) Start of a nested sequence (a new id scope).default voidEnd of the current nested sequence.default voidsigned(int id, long value) A signed-integer field, or a signed array element.default voidstring(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength) A chunk of a string field (raw UTF-8 bytes, no NUL terminator).default voidunsigned(int id, long value) An unsigned-integer field, or an unsigned array element.
-
Method Details
-
unsigned
default void unsigned(int id, long value) An unsigned-integer field, or an unsigned array element.- Parameters:
id- field idvalue- the value (unsigned 64-bit; interpret withLong.toUnsignedString(long, int)/Long.compareUnsigned(long, long))
-
signed
default void signed(int id, long value) A signed-integer field, or a signed array element.- Parameters:
id- field idvalue- the value
-
fp32
default void fp32(int id, float value) A 32-bit float field, or anfp32array element.- Parameters:
id- field idvalue- the value
-
fp64
default void fp64(int id, double value) A 64-bit float field, or anfp64array element.- Parameters:
id- field idvalue- the value
-
fixlenBegin
Start of a fixlen field — string, blob, fp32 or fp64. Announced once thefixlen_wordhas been read and validated as a format matter, and before any payload byte is delivered. Called exactly once per fixlen field,total == 0included; the payload then follows throughstring(int, int, int, byte[], int, int)/blob(int, int, int, byte[], int, int)(in chunks) orfp32(int, float)/fp64(int, double)(whole).Why the decoder announces it here. CORELIB_PLAN §5.2 makes INVALID dominate INCOMPLETE: once the bytes seen so far are already malformed, running out of input cannot downgrade the verdict. A
maxlenviolation is fully established by the length word — the number that exceeds the bound is already on the wire, and no later byte can make it legal. The payload callbacks carrytotaltoo, but they only fire once payload bytes exist, so a message truncated immediately after the length word would produce no event at all and degrade to INCOMPLETE, while the same bytes arriving in one chunk are INVALID. That is a chunk-boundary-dependent outcome, which §6.4 and §7.2 item 4 forbid outright. Announcing at the length word is what lets generated code latch the violation there; raising from this callback is what turns the field INVALID.subtypeis the subtype that arrived, which the corelib knows, not the one the schema declared, which it does not: a subtype contradicting the declared field type must be ignored as a MESSAGE_SPEC §7.3 skip rather than measured against this field's bound — the same rulearrayBegin(int, ArrayKind, int)follows one field kind over.This fires for a fixlen field only. A fixlen array's per-element
fixlen_wordis the array's header, and is announced byarrayBegin(int, ArrayKind, int)instead; its elements produce nofixlenBegin.- Parameters:
id- field idsubtype- the fixlen subtype read from the wiretotal- declared payload length in bytes (4 for fp32, 8 for fp64)
-
string
default void string(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength) A chunk of a string field (raw UTF-8 bytes, no NUL terminator).For an empty string this is called once with
total == 0andchunkLength == 0.- Parameters:
id- field idtotal- full field length in bytesoffset- byte position of this chunk within the fielddata- backing array containing the chunkchunkOffset- start of the chunk withindatachunkLength- number of bytes in the chunk
-
blob
default void blob(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength) A chunk of a blob field. Seestring(int, int, int, byte[], int, int)for the chunking model.- Parameters:
id- field idtotal- full field length in bytesoffset- byte position of this chunk within the fielddata- backing array containing the chunkchunkOffset- start of the chunk withindatachunkLength- number of bytes in the chunk
-
arrayBegin
Start of an array field. Thecountelements follow through the scalar / float callbacks with the sameid.When it fires. For an integer array (wire types
ARRAY_UNSIGNED/ARRAY_SIGNED) this is called immediately after the element-count varint — that word is the whole header. For a fixlen array (ARRAY_FIXLEN) it is called only after thefixlen_wordthat follows the count has been read and validated as a format matter, sokindalways names the concrete element subtype (ArrayKind.FP32/ArrayKind.FP64). CORELIB_PLAN §4.8 requires that ordering: a fixlen array whose subtype contradicts the declared element type is skipped under MESSAGE_SPEC §7.3 and itscountmust not be judged against a schema bound, so the subtype has to be known before the visitor is asked about the field. Either way the call happens exactly once per array field, before any element callback; a message that ends between the two words produces no call at all (it is INCOMPLETE, not INVALID).- Parameters:
id- field idkind- element category, naming the fixlen subtype for a fixlen arraycount- number of elements
-
arrayBulk
Offer to take an integer array's elements in bulk instead of one call at a time. Asked once per array, immediately afterarrayBegin(int, ArrayKind, int)and before any element, forArrayKind.UNSIGNEDandArrayKind.SIGNEDarrays with at least one element.Return a
byte[],short[],int[]orlong[]of at leastcountelements and the decoder writes straight into[0, count)— already ZigZag-decoded for a signed array, exactly the valuessigned(int, long)would have delivered — with no per-element callback at all, then callsarrayBulkEnd(int, int). Returnnull, the default, or any other type, and the offer is declined: nothing was handed over, so the elements arrive throughunsigned(int, long)/signed(int, long)as before.A destination too short is refused, not declined. Hand back one of the four array types with fewer than
countelements and the decode fails withSofabError.ARGUMENT(CORELIB_PLAN §6.6.3: the codec refuses a destination too short rather than growing it). It is neitherSofabError.INVALID_MSG— the message is well-formed and decodes for a caller who sizes the destination right — norSofabError.LIMIT_EXCEEDED, which would name a configured limit that does not exist. The mistake is in the call. Like a malformed message the refusal is terminal: every furtherIStream.feed(byte[], Visitor)rethrows it andIStream.reset()is the way on. Falling back to per-element delivery instead would be silent data loss for a visitor that overrides only this method, since every other one is adefaultno-op.The array's width is a bound. Handing back a narrower array than
long[]says the elements are declared that wide, so a value that does not fit it is malformed input and the decode fails withSofabError.INVALID_MSG— zero-extended against the width for anArrayKind.UNSIGNEDarray, sign-extended for aArrayKind.SIGNEDone. The decoder never truncates silently. This is the reason to hand back a narrow array at all: the check and the narrowing happen in the same pass that decodes, instead of a second one afterwards.The return type is
Objectrather than four overloads because the decoder must resolve the destination in ONE virtual call per array — asking four times, three of them for null, costs more than the pass it saves.This exists because
countis the one thing the consumer is told before the elements arrive: a destination sized from it needs no per-element routing, bounds growth or dispatch, and those are the whole cost of a short array. It is a fast path, not a contract change — a visitor that never overrides it decodes identically.countis untrusted. It is the wire's claim, bounded only by the format ceiling. Returnnullunless the array's length is already bounded by something the consumer trusts (a schemacount), or an oversized claim becomes an up-front allocation.- Parameters:
id- field idkind-ArrayKind.UNSIGNEDorArrayKind.SIGNEDcount- number of elements the wire announced- Returns:
- destination of at least
countelements, or null for per-element delivery
-
arrayBulkEnd
default void arrayBulkEnd(int id, int n) The bulk fill offered atarrayBulk(int, ArrayKind, int)is complete:nelements were written into the destination. Called exactly once per accepted offer, after the last element and before any following field. A truncated message never reaches it — the array did not end.- Parameters:
id- field idn- number of elements written (the announced count)
-
sequenceBegin
default void sequenceBegin(int id) Start of a nested sequence (a new id scope).- Parameters:
id- field id of the sequence
-
sequenceEnd
default void sequenceEnd()End of the current nested sequence.
-