SofaBuffers TypeScript - v0.9.0
    Preparing search index...

    Class OStream

    Encoder for the SofaBuffers wire format. Each write* method appends one field and maps one-to-one onto a wire type. Construct it in-memory (an auto-growing buffer, read back with OStream.bytes) or in streaming mode over a caller-provided buffer that drains to a FlushSink as it fills, so the message can outgrow the buffer. Invalid arguments and a full buffer with no sink throw SofabError.

    Index
    • get bytesUsed(): number

      Bytes currently held in the buffer (since construction or the last flush).

      Returns number

    • The encoded message so far, as a view into the working buffer. Meaningful for the in-memory mode; in streaming mode it is only the not-yet-flushed tail. The view is valid until the next write.

      Returns Uint8Array

    • Rewind the encoder to empty, reusing the existing buffer. Lets a caller pool one OStream across many messages instead of allocating a fresh buffer per encode. Any view previously returned by bytes is invalidated.

      Returns void

    • Install a fresh output buffer to write into, mid-stream. Intended for the streaming (flush-sink) mode: call it from inside your flush callback to hand the encoder a new buffer for the next batch of bytes, so encoding continues without interruption. offset reserves space at the front of the new buffer. Any not-yet-flushed bytes in the old buffer are dropped, so flush first (the flush callback fires before you swap).

      Parameters

      • buffer: Uint8Array
      • offset: number = 0

      Returns void

    • Write a blob (arbitrary bytes) field.

      Parameters

      • id: number
      • data: Uint8Array

      Returns void

    • Write a boolean field (encoded as the unsigned value 0 or 1).

      Parameters

      • id: number
      • value: boolean

      Returns void

    • Write an IEEE-754 32-bit float field.

      Parameters

      • id: number
      • value: number

      Returns void

    • Write an array of IEEE-754 32-bit floats.

      Parameters

      • id: number
      • values: ArrayLike<number>

      Returns void

    • Write an fp32 array from its raw little-endian element payload. The bytes are emitted verbatim — no per-element setFloat32 — so a signaling NaN survives bit-for-bit (§4.6), which writeFp32Array cannot guarantee because it re-quantizes each JS number. payload.length must be a multiple of 4; the element count is payload.length / 4.

      Parameters

      • id: number
      • payload: Uint8Array

      Returns void

    • Write an IEEE-754 64-bit double field.

      Parameters

      • id: number
      • value: number

      Returns void

    • Write an array of IEEE-754 64-bit doubles.

      Parameters

      • id: number
      • values: ArrayLike<number>

      Returns void

    • Open a nested sequence (a fresh id scope) whose header is held back until the sequence turns out to have content.

      MESSAGE_SPEC §2 omits a sequence-typed field whose value equals its declared default, and "not one child was written" is exactly that condition — evaluated per child field, recursively, for free, because the message layer already omits every child equal to its default. A sequence closed with nothing in it therefore emits nothing instead of a two-byte empty frame, and an all-default message becomes the empty byte string. No byte image is ever compared, so in-memory layout never enters the decision.

      This is the only way to open a sequence. How it closes decides whether a contentless one survives: OStream.writeSequenceEnd drops it, OStream.writeSequenceEndKeep forces the frame out.

      Parameters

      • id: number

      Returns void

    • Close the current sequence, letting it vanish if it received no content.

      Use it wherever absence encodes the same value as an empty frame: a struct/union field, and an array field whose declared default is the empty collection (MESSAGE_SPEC §2). Where the frame must be visible, close with OStream.writeSequenceEndKeep instead.

      An end with no matching begin is not rejected: the encoder writes what it is told, and the resulting bytes are then malformed, which is the decoder's verdict to make. No other port refuses it. The depth counter stops at zero so the MAX_DEPTH check on begin cannot be fooled by an underflow.

      Returns void

    • Close the current sequence, keeping its frame even when it received no content.

      Behaves like a write: it first emits any held-back headers — this frame's and every enclosing one's — and then the end marker, so an empty sequence reaches the wire as begin + end.

      Required wherever the frame carries information beyond its contents:

      • a wrapper-array element (struct/union/nested row): element presence is what carries a dynamic array's length — highest present id + 1 (MESSAGE_SPEC §5.1) — so dropping an all-default element would change the decoded length, not just the bytes;
      • an array field already known to differ from a non-empty declared default: absence would reconstruct that default, so the empty frame is the only encoding of "explicitly empty" (§2, §3).

      The two failure directions are not symmetric, which is why this is the safe choice when in doubt: using it where OStream.writeSequenceEnd would do costs one non-canonical empty frame that a decoder normalizes away, while the reverse silently changes an array's length.

      Returns void

    • Write a signed integer field (zig-zag encoded).

      Parameters

      • id: number
      • value: number | bigint

      Returns void

    • Write an array of signed integers (each zig-zag + varint).

      Parameters

      • id: number
      • values: ArrayLike<number | bigint>

      Returns void

    • Write a signed 64-bit array (zig-zag) from Long[] — the bigint-free path. Zig-zag (n << 1) ^ (n >> 63) is computed on the lo/hi pair.

      Parameters

      • id: number
      • values: readonly Long[]

      Returns void

    • Write a UTF-8 string field.

      Parameters

      • id: number
      • text: string

      Returns void

    • Write an unsigned integer field.

      Parameters

      • id: number
      • value: number | bigint

      Returns void

    • Write an array of unsigned integers (each a varint).

      Parameters

      • id: number
      • values: ArrayLike<number | bigint>

      Returns void

    • Write an unsigned 64-bit array from Long[] — the bigint-free path. Produces the identical wire to writeUnsignedArray; reads each Long's 32-bit halves directly, so no bigint is created per element.

      Parameters

      • id: number
      • values: readonly Long[]

      Returns void