Class Sofab

java.lang.Object
org.sofabuffers.sofab.Sofab

public final class Sofab extends Object
Library-level constants for the SofaBuffers (sofab) core.

These mirror the normative limits in the SofaBuffers architecture guide (§6.2). API_VERSION lets callers and the schema-driven code generator verify compatibility at build or run time.

invalid(String) is here too, and is not a constant: it is the one way a Visitor — that is, generated code — reports malformed input, and belongs beside the contract it is part of.

  • Field Details

    • API_VERSION

      public static final int API_VERSION
      SofaBuffers core API version. Callers and the generator check this for compatibility; the current contract is version 1.

      This tracks the wire contract — normative and identical in every port (CORELIB_PLAN §6.2) — so it moves only when the bytes on the wire change meaning. It is not this library's source-compatibility version: a release that renames or removes a Java method bumps the artifact version in pom.xml and leaves API_VERSION alone.

      See Also:
    • ID_MAX

      public static final int ID_MAX
      Largest valid field id, 2^31 - 1 (INT32_MAX).
      See Also:
    • ARRAY_MAX

      public static final long ARRAY_MAX
      Largest array element count / fixed-length byte count, 2^31 - 1 (INT32_MAX).
      See Also:
    • MIN_OUTPUT_BUFFER

      public static final int MIN_OUTPUT_BUFFER
      Smallest output buffer this port accepts for streaming (CORELIB_PLAN §5.1). It is 1: the encoder splits every atomic unit, so no write has to land contiguously and a single usable byte is enough.

      It binds a buffer installed with a FlushSink — at construction and at every mid-stream OStream.bufferSet(byte[], int), both of which reject buffer.length - offset < MIN_OUTPUT_BUFFER with IllegalArgumentException where the buffer is handed over, never partway through a message. A buffer installed without a sink is subject to no minimum: no flush can occur there, so a caller sizing from the generated MAX_SIZE keeps an exact fit.

      Any size at or above this produces output byte-identical to the one-shot path, so sizing a streaming buffer from this constant trades nothing but flush frequency.

      See Also:
    • MAX_DEPTH

      public static final int MAX_DEPTH
      Maximum nested-sequence depth (§4.9 / §6.2). An encoder must not open more than MAX_DEPTH nested sequences, and a decoder rejects a message that nests deeper with SofabError.INVALID_MSG, bounding recursion / stack growth.
      See Also:
  • Method Details

    • invalid

      public static UncheckedIOException invalid(String detail)
      The carrier a Visitor rejects malformed input through: an SofabError.INVALID_MSG SofabException, wrapped so it can leave a callback that declares no checked exception.

      Every Visitor method is declared throws-free, because a visitor is called from the middle of the decoder's state machine and most implementations have nothing to report. Generated code does: MESSAGE_SPEC §7 puts the schema bounds — a length above a maxlen, a count above a declared capacity, an index above one, a value outside a declared width, a string that is not UTF-8 — on the side that knows the schema, which is the visitor and not this library. So the rejection travels as an unchecked wrapper, and IStream.feed(byte[], Visitor) recognizes it on the way out: an INVALID_MSG raised by a visitor latches the decode exactly as one raised by the decoder itself does, so it is terminal: every later IStream.feed(byte[], Visitor) re-throws it and none returns a status (CORELIB_PLAN §5.2). The wrapper itself reaches the caller unchanged; the SofabException is its cause.

      That makes it a two-sided contract, and this is the side that names it. Throw the result rather than calling it for effect — throw Sofab.invalid(...) ends the method as far as the compiler is concerned:

      if (total > 8) {
          throw Sofab.invalid("name: string length above schema maxlen 8");
      }
      

      SofabError.LIMIT_EXCEEDED — a receiver-side policy rejection of well-formed bytes (§6.2.1) — is deliberately not this: it travels through limitExceeded(String) instead, and is latched under its own code.

      The wrapper only carries the rejection across the Visitor interface: IStream.feed(byte[], Visitor) unwraps it, so the decode's caller catches the SofabException feed declares, as it does for the decoder's own rejections. The same holds for limitExceeded(String) and argument(String).

      Parameters:
      detail - human-readable context, naming the field and the bound it broke
      Returns:
      the exception to throw
    • limitExceeded

      public static UncheckedIOException limitExceeded(String detail)
      The carrier a receiver-side limit (§6.2.1) is refused through: a SofabError.LIMIT_EXCEEDED SofabException, wrapped like invalid(String) so it can leave a callback that declares no checked exception.

      It is the twin of invalid(String) and deliberately not the same thing. INVALID_MSG says these bytes are broken; LIMIT_EXCEEDED says these bytes are fine and this receiver declines to hold that much, so the same message decodes under a looser limit. Both are terminal (§6.3: "a terminal, receiver-local policy rejection") and IStream.feed(byte[], Visitor) latches both, so the decode ends there and every later call repeats the rejection until IStream.reset(); what keeps them apart is the code they keep — a limit rejection is never reported as INVALID_MSG (§6.3), and is never clamped or truncated into a shortened value.

      Raised from two places, and only where a cap was actually supplied: PayloadAcc.string(int, int, byte[], int, int, Bound) / PayloadAcc.blob(int, int, byte[], int, int, Bound) at a payload's announced length, and the Seq row reservations at a wrapper array's element index. Both take the number as an argument, inside the Bound that says which of §6.2.1's two rules governs the field.

      Parameters:
      detail - human-readable context, naming the value and the limit it broke
      Returns:
      the exception to throw
    • argument

      public static UncheckedIOException argument(String detail)
      The carrier a defect in the call is refused through: a SofabError.ARGUMENT SofabException, wrapped like invalid(String) so it can leave a callback that declares no checked exception.

      It is what §6.3 calls InvalidArgument, and it is deliberately neither of the other two. INVALID_MSG would mark a well-formed message malformed; LIMIT_EXCEEDED would "promise a limit to raise that was never configured" (§6.3). The mistake is in the call, not in the bytes and not in the deployment.

      Raised where a Bound the caller had to state is missing — a null where a bound belonged, or the numberless Bound.SCHEMA_BOUNDED handed to a call that bounds an array index. §6.2.1 forbids reading an omitted argument as unlimited, so an omission is reported rather than obeyed.

      Parameters:
      detail - human-readable context, naming what the call failed to state
      Returns:
      the exception to throw