Class PayloadAcc
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 -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Offer a chunk of ablobpayload; returns the payload once the last chunk has arrived,nullwhile more are expected.static voidcheckBlobLength(int total, Bound bound) Refuse an announcedbloblength the receiver's cap does not admit.static voidcheckStringLength(int total, Bound bound) Refuse an announcedstringlength the receiver's cap does not admit — the §6.2.1 comparison on its own, for a caller to make at the length word.Offer a chunk of astringpayload; returns the decoded string once the last chunk has arrived,nullwhile more are expected.
-
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 astringpayload; returns the decoded string once the last chunk has arrived,nullwhile 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, asVisitor.string(int, int, int, byte[], int, int)reports itoffset- byte position of this chunk within the payloaddata- backing array containing the chunkchunkOffset- start of the chunk withindatachunkLength- number of bytes in the chunkbound-Bound.receiver(long)carrying the caller'smax_dyn_string_len(§6.2.1) for a schema-unbounded field, orBound.SCHEMA_BOUNDEDwhere the schema'smaxlengoverns 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 aLIMIT_EXCEEDEDSofabExceptionwhentotalexceeds the receiver cap, anARGUMENTone whenboundis null, or anINVALID_MSGone 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 ablobpayload; returns the payload once the last chunk has arrived,nullwhile 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, asVisitor.blob(int, int, int, byte[], int, int)reports itoffset- byte position of this chunk within the payloaddata- backing array containing the chunkchunkOffset- start of the chunk withindatachunkLength- number of bytes in the chunkbound-Bound.receiver(long)carrying the caller'smax_dyn_blob_len(§6.2.1) for a schema-unbounded field, orBound.SCHEMA_BOUNDEDwhere the schema'smaxlengoverns instead. Abloband astringare separate limits.- Returns:
- the completed payload, or null if it is still incomplete
- Throws:
UncheckedIOException- wrapping aLIMIT_EXCEEDEDSofabExceptionwhentotalexceeds the receiver cap, or anARGUMENTone whenboundis null
-
checkStringLength
Refuse an announcedstringlength 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 fromVisitor.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 nostring(int, int, byte[], int, int, Bound)call, and the decode would reportINCOMPLETEfor 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 thefixlen_wordgives itbound-Bound.receiver(long)carrying this deployment'smax_dyn_string_len, orBound.SCHEMA_BOUNDEDwhere the schemamaxlengoverns and the caller has already enforced it- Throws:
UncheckedIOException- wrapping aLIMIT_EXCEEDEDSofabExceptionwhentotalis above the cap, or anARGUMENTone whenboundis null
-
checkBlobLength
Refuse an announcedbloblength the receiver's cap does not admit. Theblobtwin ofcheckStringLength(int, Bound), down to why it exists; abloband astringare separate limits.- Parameters:
total- the announced payload length, as thefixlen_wordgives itbound-Bound.receiver(long)carrying this deployment'smax_dyn_blob_len, orBound.SCHEMA_BOUNDED- Throws:
UncheckedIOException- wrapping aLIMIT_EXCEEDEDSofabExceptionwhentotalis above the cap, or anARGUMENTone whenboundis null
-