Class PayloadAcc

java.lang.Object
org.sofabuffers.sofab.PayloadAcc

public final class PayloadAcc extends Object
Reassembles a string or blob payload that arrives in more than one piece — the support layer, not the codec.

Visitor.string(int, int, int, byte[], int, int) and Visitor.blob(int, int, int, byte[], int, int) deliver a payload in one or more chunks, split wherever the input happened to be split, and the data array they hand over is only valid for the duration of the call. A consumer that wants the whole value therefore has to buffer the pieces. That is all this is, and its code has the same shape for every schema, so it lives here rather than being emitted into every generated package (generator#345).

Hold one per visitor and pass the callback's arguments straight through, followed by the bound that governs the destination they are headed for; the value comes back on the chunk that completes it, and null before that:

private static final Bound STRING_CAP = Bound.receiver(MAX_DYN_STRING_LEN);

public void string(int id, int total, int offset, byte[] data, int co, int cl) {
    String s = acc.string(total, offset, data, co, cl, STRING_CAP);
    if (s == null) {
        return;                  // more chunks to come
    }
    ...                          // route s to its field
}

A payload that arrives whole never touches the buffer. The common case — one chunk carrying the entire field — is answered straight out of the caller's input array, so an accumulator that is never needed never allocates one byte.

total is not an allocation. The announced length is the wire's claim, so the buffer grows by doubling against bytes that have actually arrived: even a caller whose field the schema bounds, and who therefore hands over no receiver cap, cannot be made to allocate more than the peer actually sent.

The receiver cap is compared here (CORELIB_PLAN §6.2.1). A breach is Sofab.limitExceeded(String): the bytes are well-formed and this receiver declines to hold that much, so it is a policy rejection and never SofabError.INVALID_MSG.

...and it is offered at the LENGTH WORD, not only at the first chunk. §6.2.1 puts the enforcement point "at the count/length header — before the allocation it is meant to prevent", and string(int, int, byte[], int, int, Bound) / blob(int, int, byte[], int, int, Bound) cannot be that point on their own: they fire only once a payload byte exists, so a message that ends immediately after the length word reaches neither. The announced length is already on the wire and the verdict is already decided, yet the decode would answer INCOMPLETE — losing the category (§6.3 makes the refusal terminal, and no continuation can lift it) and inviting a caller to keep feeding a stream this receiver has already refused. Three bytes claiming a hundred, or five claiming a megabyte, would hold a connection open: precisely the amplification the caps exist to close.

So the comparison is also reachable on its own, as checkStringLength(int, Bound) / checkBlobLength(int, Bound), for a caller to make from Visitor.fixlenBegin(int, FixlenType, int) — which the decoder raises at the length word, for exactly this reason. The payload methods keep calling the same two, so the rule has one implementation applied at two points (§6.2.1, "one implementation, wherever it runs"); an accumulator driven by hand, without the header call, still refuses an over-cap payload.

The two answers are not one number. Bound.receiver(long) states the deployment's cap and Bound.SCHEMA_BOUNDED states that the schema's maxlen governs instead — a distinct value carrying no number, so a caller that forgot to configure a cap cannot arrive here spelling the same thing a schema-bounded field spells. A missing bound is Sofab.argument(String), never silently uncapped. A caller may instead hand the maxlen itself over as Bound.schema(long); it is then compared here like a cap, but a breach is INVALID_MSG (MESSAGE_SPEC §7.1), never LIMIT_EXCEEDED.

The number is the caller's. §6.2.1 fixes the provenance of a receiver limit — it comes from generated code, which knows the schema and the target — but leaves the site of the comparison open: "A corelib MAY take a limit as an argument and perform the check itself, and a port that does is conformant." This class does. Nothing here holds a limit, defaults one, keeps one past the call it was given for, or clamps to one, and a caller that passes the cap here does not also guard in front of the call — one implementation, wherever it runs. Where the schema bounds the field there is no receiver cap to pass: see Bound.SCHEMA_BOUNDED.

A skipped field is never capped. The check sits behind the decoder's wire-type dispatch and behind the caller's own destination switch, so a field whose wire type contradicts the declared one (MESSAGE_SPEC §7.3), or one this message does not read at all, never reaches an accumulator and never meets a cap.

No re-arming step. Every payload's first chunk is reported at offset 0, and that is where the buffer is emptied — so an accumulator still holding the remains of a payload that never completed (a stream that ended mid-field) is correct again the moment the next one starts, whether or not the visitor around it was reused.

The split must not be observable. CORELIB_PLAN §6.4 forbids an outcome that depends on where a chunk boundary fell — for the same bytes, the value and the UTF-8 verdict are the same whether they arrive in one piece or one byte at a time.

  • Constructor Summary

    Constructors
    Constructor
    Description
    A fresh accumulator, holding no payload and no buffer.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    blob(int total, int offset, byte[] data, int chunkOffset, int chunkLength, Bound bound)
    Offer a chunk of a blob payload; returns the payload once the last chunk has arrived, null while more are expected.
    static void
    checkBlobLength(int total, Bound bound)
    Refuse an announced blob length the receiver's cap does not admit.
    static void
    checkStringLength(int total, Bound bound)
    Refuse an announced string length the receiver's cap does not admit — the §6.2.1 comparison on its own, for a caller to make at the length word.
    string(int total, int offset, byte[] data, int chunkOffset, int chunkLength, Bound bound)
    Offer a chunk of a string payload; returns the decoded string once the last chunk has arrived, null while more are expected.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PayloadAcc

      public PayloadAcc()
      A fresh accumulator, holding no payload and no buffer.
  • Method Details

    • string

      public String string(int total, int offset, byte[] data, int chunkOffset, int chunkLength, Bound bound)
      Offer a chunk of a string payload; returns the decoded string once the last chunk has arrived, null while more are expected.

      Validation happens on the reassembled payload, once, at the point it is complete — never per chunk, which would reject a multi-byte character split across a boundary.

      Parameters:
      total - full payload length in bytes, as Visitor.string(int, int, int, byte[], int, int) reports it
      offset - byte position of this chunk within the payload
      data - backing array containing the chunk
      chunkOffset - start of the chunk within data
      chunkLength - number of bytes in the chunk
      bound - Bound.receiver(long) carrying the caller's max_dyn_string_len (§6.2.1) for a schema-unbounded field, or Bound.SCHEMA_BOUNDED where the schema's maxlen governs instead — which is neither "unlimited" nor a cap left unstated
      Returns:
      the completed string, or null if the payload is still incomplete
      Throws:
      UncheckedIOException - wrapping a LIMIT_EXCEEDED SofabException when total exceeds the receiver cap, an ARGUMENT one when bound is null, or an INVALID_MSG one when the completed payload is not valid UTF-8
    • blob

      public byte[] blob(int total, int offset, byte[] data, int chunkOffset, int chunkLength, Bound bound)
      Offer a chunk of a blob payload; returns the payload once the last chunk has arrived, null while more are expected.

      The returned array is the caller's to keep: it is a copy, never a view into the decoder's input buffer or into this accumulator.

      Parameters:
      total - full payload length in bytes, as Visitor.blob(int, int, int, byte[], int, int) reports it
      offset - byte position of this chunk within the payload
      data - backing array containing the chunk
      chunkOffset - start of the chunk within data
      chunkLength - number of bytes in the chunk
      bound - Bound.receiver(long) carrying the caller's max_dyn_blob_len (§6.2.1) for a schema-unbounded field, or Bound.SCHEMA_BOUNDED where the schema's maxlen governs instead. A blob and a string are separate limits.
      Returns:
      the completed payload, or null if it is still incomplete
      Throws:
      UncheckedIOException - wrapping a LIMIT_EXCEEDED SofabException when total exceeds the receiver cap, or an ARGUMENT one when bound is null
    • checkStringLength

      public static void checkStringLength(int total, Bound bound)
      Refuse an announced string length the receiver's cap does not admit — the §6.2.1 comparison on its own, for a caller to make at the length word.

      string(int, int, byte[], int, int, Bound) calls this first, so a caller that only routes payload chunks is already covered. Call it in addition from Visitor.fixlenBegin(int, FixlenType, int) to close the case no payload callback can see: a message whose length word declares more than the cap and then ends. There is no chunk, so there is no string(int, int, byte[], int, int, Bound) call, and the decode would report INCOMPLETE for bytes this receiver has already refused — a verdict §6.3 makes terminal and no continuation can lift.

      Calling it at both points is not two implementations of the rule: it is this one, applied where §6.2.1 requires it and again where a hand-driven accumulator would otherwise slip past it.

      Only for a field this message actually reads. §6.2.1: "a skipped field is never capped" — a limit bounds an allocation, and a field walked over allocates nothing. A caller must resolve the destination, and the MESSAGE_SPEC §7.3 subtype test, before it gets here.

      Parameters:
      total - the announced payload length, as the fixlen_word gives it
      bound - Bound.receiver(long) carrying this deployment's max_dyn_string_len, or Bound.SCHEMA_BOUNDED where the schema maxlen governs and the caller has already enforced it
      Throws:
      UncheckedIOException - wrapping a LIMIT_EXCEEDED SofabException when total is above the cap, or an ARGUMENT one when bound is null
    • checkBlobLength

      public static void checkBlobLength(int total, Bound bound)
      Refuse an announced blob length the receiver's cap does not admit. The blob twin of checkStringLength(int, Bound), down to why it exists; a blob and a string are separate limits.
      Parameters:
      total - the announced payload length, as the fixlen_word gives it
      bound - Bound.receiver(long) carrying this deployment's max_dyn_blob_len, or Bound.SCHEMA_BOUNDED
      Throws:
      UncheckedIOException - wrapping a LIMIT_EXCEEDED SofabException when total is above the cap, or an ARGUMENT one when bound is null