Class IStream

java.lang.Object
org.sofabuffers.sofab.IStream

public final class IStream extends Object
Streaming SofaBuffers decoder.

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
    Constructor
    Description
    Create a fresh decoder ready to accept a new message.
  • Method Summary

    Modifier and Type
    Method
    Description
    feed(byte[] data, int off, int len, Visitor visitor)
    Feed a slice of encoded bytes, pushing decoded fields to visitor, and report where the decode stands.
    feed(byte[] data, Visitor visitor)
    Feed a whole chunk of encoded bytes, pushing decoded fields to visitor, and report where the decode stands.
    void
    Return this decoder to its just-constructed state so it can decode another message.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 acc landing 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 new IStream, 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 generated decode helpers) 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_MSG it 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 call feed(byte[], Visitor) decodes nothing and keeps re-throwing the rejection.

      acc keeps its allocation — it is sized once at construction and never re-made — and only its first accLen bytes are ever read, which is zeroed here. Everything else this class declares is restored, and ResetCoversEveryFieldTest holds that to every field added later.

    • feed

      public DecodeStatus feed(byte[] data, Visitor visitor) throws SofabException
      Feed a whole chunk of encoded bytes, pushing decoded fields to visitor, and report where the decode stands.
      Parameters:
      data - encoded bytes
      visitor - sink for decoded fields
      Returns:
      DecodeStatus.COMPLETE when the bytes consumed so far end at a clean field boundary with no open sequence, else DecodeStatus.INCOMPLETE
      Throws:
      SofabException - with SofabError.INVALID_MSG on malformed input
    • feed

      public DecodeStatus feed(byte[] data, int off, int len, Visitor visitor) throws SofabException
      Feed a slice of encoded bytes, pushing decoded fields to visitor, and report where the decode stands. Decoding can continue across many feed calls; 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.COMPLETE at a clean field boundary with every opened sequence closed, DecodeStatus.INCOMPLETE when 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. INCOMPLETE is 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_MSG outcome is terminal (CORELIB_PLAN §5.2): once any fed bytes have been rejected as malformed, this method decodes nothing further and rethrows INVALID_MSG for every subsequent call until reset() 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 into INVALID_MSG. So is SofabError.ARGUMENT, raised where a Visitor.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 the Visitor. A visitor method cannot throw a checked exception, so that code reports it wrapped in an UncheckedIOException (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 — an UncheckedIOException whose cause is not a SofabException — passes through as thrown.

      Parameters:
      data - backing array
      off - start offset
      len - number of bytes to consume
      visitor - sink for decoded fields
      Returns:
      DecodeStatus.COMPLETE when the bytes consumed so far end at a clean field boundary with no open sequence, else DecodeStatus.INCOMPLETE
      Throws:
      SofabException - with SofabError.INVALID_MSG on malformed input, or on any call after malformed input was already rejected; with SofabError.LIMIT_EXCEEDED where a receiver limit refuses this message, and on every call after that; with SofabError.ARGUMENT where a bulk destination is too short for the count it was offered, and on every call after that