Class IStream
Feed IStream arbitrary chunks with feed(byte[], Visitor); it parses field
headers and pushes decoded fields to your Visitor. Because all parse
state lives inside the decoder, a message may be split across any number of
feed calls at any byte boundary — true streaming on the input side.
Two decode paths. When a clean field boundary and a contiguous run of
bytes are both in hand, the decoder advances a pointer straight over the buffer,
reading whole field headers, scalars and array elements with no per-byte state
dispatch (the "advance a pointer over a contiguous buffer" technique). The
moment a field — or array element — would run past the end of the supplied
bytes, a resumable byte-at-a-time state machine takes over, suspends, and
resumes on the next feed. Only that one construct pays for the split:
the moment it completes, the rest of the chunk goes back to the fast path —
within an array as much as between fields, so a boundary inside a long array
costs one element and not its remainder. The two paths are byte-for-byte
equivalent; the fast path simply removes the per-byte overhead from the common
case where a message (or a large chunk of one) arrives in a single feed.
Unlike the C decoder there is no per-field "bind a destination" step and no
explicit skip bookkeeping: a Visitor simply ignores fields it does not
care about. Scalars and floats are delivered whole; string / blob payloads are
delivered in chunks (so they may exceed RAM); array elements are announced
with Visitor.arrayBegin(int, ArrayKind, int) and then delivered through the scalar / float
callbacks. Every fixlen field is announced with Visitor.fixlenBegin(int, FixlenType, int) at
its length word, before any payload byte, so a length bound can be enforced
there rather than after the payload assembles.
One question, one answer: the outcome is what feed returns
(CORELIB_PLAN §5.2.4, "the status feed/decode returns is
the answer"). Every feed reports on the bytes consumed so far:
DecodeStatus.COMPLETE when they end exactly at a field boundary with no
open sequence, DecodeStatus.INCOMPLETE when they end inside a field — a
partial varint, a fixlen/array payload shorter than declared, an array with
elements still pending — or with an open (unclosed) nested sequence. There is no
second way to ask and no finish/finalize step; the caller owns end-of-input and
decides whether a trailing INCOMPLETE is a truncation it cares about.
INCOMPLETE is not an error. Running out of bytes mid-field
suspends the decode and returns normally; the next feed resumes it at
the byte it stopped on.
A refusal travels on the error channel, carrying its code. Malformed
bytes — the INVALID outcome of §5.2, InvalidMessage in §6.3's
table — throw SofabException with SofabError.INVALID_MSG; a
receiver-cap rejection of well-formed bytes (§6.2.1) throws
SofabError.LIMIT_EXCEEDED, the code §6.3 keeps distinct from it. Neither
is ever returned: a rejected decode leaves through the throw,
which is the one place the caller learns of it.
A third code joins them on that channel: the call was wrong. The one
place a caller hands this decoder storage is Visitor.arrayBulk(int, ArrayKind, int), and a
destination the decoder can fill but that is shorter than the announced count is
refused with SofabError.ARGUMENT rather than grown or quietly declined
(CORELIB_PLAN §6.6.3). It is §6.3's third way a value can be refused, and the
only one that says nothing about the message: the bytes are well-formed and the
same message decodes for a caller who sizes the destination right, so it is
neither INVALID_MSG nor LIMIT_EXCEEDED, which would name a
receiver cap nobody configured.
All three rejections are terminal. Malformed bytes are malformed
regardless of what follows (§5.2), a limit rejection is "a terminal,
receiver-local policy rejection" (§6.3), and a refused destination leaves the
parse position meaningless — the offer site is behind us and the array's payload
was never consumed — so the verdict sticks: every further feed
re-throws the same code without decoding a byte. A caller that catches the
exception and keeps feeding therefore cannot resume mid-stream on a message the
decoder has already refused, nor read a COMPLETE out of it.
reset() — resynchronising onto the next message — is what clears it.
This class is not thread-safe; decode one message from one thread. Reuse an
instance for a new message only after the previous one is fully consumed (or
by constructing a fresh IStream).
Example
class Sink implements Visitor {
long a; long b;
public void unsigned(int id, long v) { if (id == 1) a = v; }
public void signed(int id, long v) { if (id == 2) b = v; }
}
Sink sink = new Sink();
IStream is = new IStream();
if (is.feed(buf, sink) == DecodeStatus.INCOMPLETE) {
// buf ended mid-message; wait for more bytes (or treat as truncation).
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFeed a slice of encoded bytes, pushing decoded fields tovisitor, and report where the decode stands.Feed a whole chunk of encoded bytes, pushing decoded fields tovisitor, and report where the decode stands.voidreset()Return this decoder to its just-constructed state so it can decode another message.
-
Constructor Details
-
IStream
public IStream()Create a fresh decoder ready to accept a new message.This is the one moment a decoder allocates (CORELIB_PLAN §6.6): the object itself and the eight-byte
acclanding zone.feed(byte[], Visitor)and everything it reaches allocate nothing.
-
-
Method Details
-
reset
public void reset()Return this decoder to its just-constructed state so it can decode another message. Equivalent to allocating a newIStream, without the allocation: a decoder is one small object plus its scalar accumulator, so a caller decoding many messages in a row (a server loop, the generateddecodehelpers) can hold one instance and reset it per message instead of letting each decode allocate and immediately discard one.Discards any partially decoded field and any open sequence nesting, so it must not be called mid-message unless that is the intent — after an
SofabError.INVALID_MSGit is exactly how a stream decoder resynchronises onto the next message, and the only way: that outcome is terminal (CORELIB_PLAN §5.2), so until this callfeed(byte[], Visitor)decodes nothing and keeps re-throwing the rejection.acckeeps its allocation — it is sized once at construction and never re-made — and only its firstaccLenbytes are ever read, which is zeroed here. Everything else this class declares is restored, andResetCoversEveryFieldTestholds that to every field added later. -
feed
Feed a whole chunk of encoded bytes, pushing decoded fields tovisitor, and report where the decode stands.- Parameters:
data- encoded bytesvisitor- sink for decoded fields- Returns:
DecodeStatus.COMPLETEwhen the bytes consumed so far end at a clean field boundary with no open sequence, elseDecodeStatus.INCOMPLETE- Throws:
SofabException- withSofabError.INVALID_MSGon malformed input
-
feed
Feed a slice of encoded bytes, pushing decoded fields tovisitor, and report where the decode stands. Decoding can continue across manyfeedcalls; the decoder keeps all state internally.The returned status is the answer (CORELIB_PLAN §5.2.4). It describes the bytes consumed so far, not just this slice:
DecodeStatus.COMPLETEat a clean field boundary with every opened sequence closed,DecodeStatus.INCOMPLETEwhen the bytes ended inside a field — a partial varint (field header or value), a fixlen/array payload shorter than declared, an array with elements still pending — or with an open nested sequence.INCOMPLETEis not an error and needs no finish step to resolve: the decode suspends, the next call resumes it, and the caller owns end-of-input.A rejection leaves by the exception, never by the return. The
SofabError.INVALID_MSGoutcome is terminal (CORELIB_PLAN §5.2): once any fed bytes have been rejected as malformed, this method decodes nothing further and rethrowsINVALID_MSGfor every subsequent call untilreset()begins a new message.SofabError.LIMIT_EXCEEDED— a receiver-cap refusal of well-formed bytes (§6.2.1) — is terminal too (§6.3) and behaves the same way, rethrown under its own code, never folded intoINVALID_MSG. So isSofabError.ARGUMENT, raised where aVisitor.arrayBulk(int, ArrayKind, int)destination is too short for the count it was offered (§6.6.3). Running out of bytes mid-field is not any of that: it suspends and resumes on the next call, as before.Every rejection arrives as the checked
SofabException, including one raised by generated code inside theVisitor. A visitor method cannot throw a checked exception, so that code reports it wrapped in anUncheckedIOException(Sofab.invalid(String),Sofab.limitExceeded(String),Sofab.argument(String)); this method is the public boundary and unwraps it. Only a visitor's own I/O failure — anUncheckedIOExceptionwhose cause is not aSofabException— passes through as thrown.- Parameters:
data- backing arrayoff- start offsetlen- number of bytes to consumevisitor- sink for decoded fields- Returns:
DecodeStatus.COMPLETEwhen the bytes consumed so far end at a clean field boundary with no open sequence, elseDecodeStatus.INCOMPLETE- Throws:
SofabException- withSofabError.INVALID_MSGon malformed input, or on any call after malformed input was already rejected; withSofabError.LIMIT_EXCEEDEDwhere a receiver limit refuses this message, and on every call after that; withSofabError.ARGUMENTwhere a bulk destination is too short for the count it was offered, and on every call after that
-