Interface Visitor


public interface Visitor
Receives decoded fields pushed by an 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 Type
    Method
    Description
    default void
    arrayBegin(int id, ArrayKind kind, int count)
    Start of an array field.
    default Object
    arrayBulk(int id, ArrayKind kind, int count)
    Offer to take an integer array's elements in bulk instead of one call at a time.
    default void
    arrayBulkEnd(int id, int n)
    The bulk fill offered at arrayBulk(int, ArrayKind, int) is complete: n elements were written into the destination.
    default void
    blob(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength)
    A chunk of a blob field.
    default void
    fixlenBegin(int id, FixlenType subtype, int total)
    Start of a fixlen field — string, blob, fp32 or fp64.
    default void
    fp32(int id, float value)
    A 32-bit float field, or an fp32 array element.
    default void
    fp64(int id, double value)
    A 64-bit float field, or an fp64 array element.
    default void
    sequenceBegin(int id)
    Start of a nested sequence (a new id scope).
    default void
    End of the current nested sequence.
    default void
    signed(int id, long value)
    A signed-integer field, or a signed array element.
    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).
    default void
    unsigned(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 id
      value - the value (unsigned 64-bit; interpret with Long.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 id
      value - the value
    • fp32

      default void fp32(int id, float value)
      A 32-bit float field, or an fp32 array element.
      Parameters:
      id - field id
      value - the value
    • fp64

      default void fp64(int id, double value)
      A 64-bit float field, or an fp64 array element.
      Parameters:
      id - field id
      value - the value
    • fixlenBegin

      default void fixlenBegin(int id, FixlenType subtype, int total)
      Start of a fixlen field — string, blob, fp32 or fp64. Announced once the fixlen_word has been read and validated as a format matter, and before any payload byte is delivered. Called exactly once per fixlen field, total == 0 included; the payload then follows through string(int, int, int, byte[], int, int) / blob(int, int, int, byte[], int, int) (in chunks) or fp32(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 maxlen violation 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 carry total too, 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.

      subtype is 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 rule arrayBegin(int, ArrayKind, int) follows one field kind over.

      This fires for a fixlen field only. A fixlen array's per-element fixlen_word is the array's header, and is announced by arrayBegin(int, ArrayKind, int) instead; its elements produce no fixlenBegin.

      Parameters:
      id - field id
      subtype - the fixlen subtype read from the wire
      total - 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 == 0 and chunkLength == 0.

      Parameters:
      id - field id
      total - full field length in bytes
      offset - byte position of this chunk within the field
      data - backing array containing the chunk
      chunkOffset - start of the chunk within data
      chunkLength - 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. See string(int, int, int, byte[], int, int) for the chunking model.
      Parameters:
      id - field id
      total - full field length in bytes
      offset - byte position of this chunk within the field
      data - backing array containing the chunk
      chunkOffset - start of the chunk within data
      chunkLength - number of bytes in the chunk
    • arrayBegin

      default void arrayBegin(int id, ArrayKind kind, int count)
      Start of an array field. The count elements follow through the scalar / float callbacks with the same id.

      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 the fixlen_word that follows the count has been read and validated as a format matter, so kind always 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 its count must 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 id
      kind - element category, naming the fixlen subtype for a fixlen array
      count - number of elements
    • arrayBulk

      default Object arrayBulk(int id, ArrayKind kind, int count)
      Offer to take an integer array's elements in bulk instead of one call at a time. Asked once per array, immediately after arrayBegin(int, ArrayKind, int) and before any element, for ArrayKind.UNSIGNED and ArrayKind.SIGNED arrays with at least one element.

      Return a byte[], short[], int[] or long[] of at least count elements and the decoder writes straight into [0, count) — already ZigZag-decoded for a signed array, exactly the values signed(int, long) would have delivered — with no per-element callback at all, then calls arrayBulkEnd(int, int). Return null, the default, or any other type, and the offer is declined: nothing was handed over, so the elements arrive through unsigned(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 count elements and the decode fails with SofabError.ARGUMENT (CORELIB_PLAN §6.6.3: the codec refuses a destination too short rather than growing it). It is neither SofabError.INVALID_MSG — the message is well-formed and decodes for a caller who sizes the destination right — nor SofabError.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 further IStream.feed(byte[], Visitor) rethrows it and IStream.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 a default no-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 with SofabError.INVALID_MSG — zero-extended against the width for an ArrayKind.UNSIGNED array, sign-extended for a ArrayKind.SIGNED one. 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 Object rather 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 count is 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.

      count is untrusted. It is the wire's claim, bounded only by the format ceiling. Return null unless the array's length is already bounded by something the consumer trusts (a schema count), or an oversized claim becomes an up-front allocation.

      Parameters:
      id - field id
      kind - ArrayKind.UNSIGNED or ArrayKind.SIGNED
      count - number of elements the wire announced
      Returns:
      destination of at least count elements, or null for per-element delivery
    • arrayBulkEnd

      default void arrayBulkEnd(int id, int n)
      The bulk fill offered at arrayBulk(int, ArrayKind, int) is complete: n elements 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 id
      n - 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.