Class IStream
- Namespace
- sofab
- Assembly
- SofaBuffers.dll
Streaming SofaBuffers decoder.
public sealed class IStream
- Inheritance
-
IStream
- Inherited Members
Examples
class Sink : IVisitor {
public long A; public long B;
public void Unsigned(int id, ulong v) { if (id == 1) A = (long)v; }
public void Signed(int id, long v) { if (id == 2) B = v; }
}
var sink = new Sink();
new IStream().Feed(buf, sink);
Remarks
IStream is a byte-at-a-time state machine. Feed it arbitrary
chunks with Feed(byte[], IVisitor); it parses field headers and
pushes decoded fields to your IVisitor. 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.
Each Feed returns a DecodeStatus — the one and only way
to learn where the decode stands: Complete if the
bytes so far end at a field boundary, or
Incomplete if they end
inside a field or with an open sequence (MESSAGE_SPEC §7). Incomplete is
not an error and is not a rejection — the partial field is
held and resumed on the next chunk; the caller owns end-of-input and decides
whether a trailing Incomplete is a truncation. Genuinely malformed input still
throws SofabException (InvalidMessage).
There is no finish / finalize step.
A rejection is terminal. Malformed bytes are malformed regardless of
what follows, so once a Feed has thrown
InvalidMessage the decoder latches that verdict, and
every later Feed throws again, consuming nothing and emitting no
visitor callback — a caller that logs the error and keeps reading its socket
cannot resume a stream this decoder has already rejected. Decode a new
message with a new IStream.
Unlike the C decoder there is no per-field "bind a destination" step and no
explicit skip bookkeeping: an IVisitor 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 ArrayBegin(int, ArrayKind, int) and then delivered through
the scalar / float callbacks. Every fixlen field is likewise announced with
FixlenBegin(int, FixlenType, int) on its length word, before any payload
byte, so a schema maxlen bound is judged at the word that violates it
rather than at whichever payload byte happens to arrive first.
Hot-path convention. Feed(byte[], int, int, IVisitor)
validates its slice once and the decode paths then read it without a per-byte
bounds check; the single-byte varint case is written out at each call site
rather than shared behind a helper, for the reason given on
ReadVarintMulti, which decodes longer varints eight bytes at a time.
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).
Constructors
IStream()
Create a fresh decoder ready to accept a new message.
public IStream()
Methods
Feed(byte[], int, int, IVisitor)
Feed a slice of encoded bytes, pushing decoded fields to
visitor. Decoding can continue across many Feed
calls; the decoder keeps all state internally.
public DecodeStatus Feed(byte[] data, int off, int len, IVisitor visitor)
Parameters
databyte[]backing array
offintstart offset
lenintnumber of bytes to consume
visitorIVisitorsink for decoded fields
Returns
- DecodeStatus
Complete if the bytes consumed so far end at a clean field boundary (a valid message), or Incomplete if they end inside a field — a partial varint, an unfinished fixlen / array payload, or a still-open nested sequence (MESSAGE_SPEC §7). Incomplete is not an error and not a rejection: the decoder keeps its partial state, so feeding the next chunk resumes exactly where this one stopped. This returned value is the only report of the outcome: there is no separate status accessor to consult afterwards, and none to drift out of step with it.
Exceptions
- SofabException
with InvalidMessage on malformed input — including on every later call, once this decoder has rejected a stream (§5.2: the
INVALIDverdict is terminal)- ArgumentNullException
dataisnull- ArgumentOutOfRangeException
[off, off + len)is not a range ofdata
Feed(byte[], IVisitor)
Feed a whole chunk of encoded bytes, pushing decoded fields to
visitor.
public DecodeStatus Feed(byte[] data, IVisitor visitor)
Parameters
Returns
- DecodeStatus
Complete if the bytes consumed so far end at a clean field boundary, or Incomplete if they end inside a field or with an open sequence (MESSAGE_SPEC §7). Incomplete is not an error — feed more bytes to continue.
Exceptions
- SofabException
with InvalidMessage on malformed input — and on every later call once this decoder has rejected a stream, since that verdict is terminal (§5.2)