Class Seq

java.lang.Object
org.sofabuffers.sofab.Seq

public final class Seq extends Object
Element placement and array growth for generated decode destinations — the support layer, not the codec.

Nothing here touches the wire. These are the operations a generated message class performs around a Visitor callback: put an element at the index its id names, grow a primitive array as elements actually arrive, re-arm a reused destination. Their code has the same shape for every schema — a count, a maxlen or a capacity arrives as an argument and an element type as a type parameter — which is why they live in the corelib rather than being emitted, rationale and all, into every generated package (generator#345).

This is the one place in this library that names List. The codec itself stays array- and primitive-based; a generated message is what holds a list, because a wrapper array of strings, blobs or sub-messages has no primitive form.

Three reservations, one shape. Every wrapper array a schema can declare reaches this class through one of three calls, which differ only in what a slot holds: placeElem(List, int, T, T, Bound) puts a decoded string or blob at the index its id names; reserveElem(List, int, Supplier, Bound) makes the slot a struct, union or nested-array element will be routed into; reserveRow(List, int, Bound) and its six primitive overloads reserve a matrix row. The schema dependence of all three is exactly a bound, an element type and an element default — an argument, a type parameter and an argument — which is the whole of why they fit here at all.

Two rules run through all of it. Ids are positions (MESSAGE_SPEC §5.1): an array element's id is its index, an interior element equal to the element default may be omitted, and the highest id present is what gives the decoded array its length — so a missing id fills a gap rather than shifting every later element down by one, and a repeated id replaces rather than appends. A count is untrusted: it is the wire's claim about how many elements follow, bounded by nothing until a schema count or a receiver limit bounds it, so no method here allocates from a count alone.

The index bound (MESSAGE_SPEC §7.1, CORELIB_PLAN §6.2.1). Every reservation here — placeElem(List, int, T, T, Bound), reserveElem(List, int, Supplier, Bound), reserveRow(List, int, Bound) and the six primitive reserveRow* overloads — takes a Bound on the array's element index and compares it here, before anything is created and before the list is grown to hold it. A wrapper array announces no count, so the index is what bounds it: its length is highest present id + 1 (MESSAGE_SPEC §5.1), and two elements at id 0 and id 65535 are a 65536-slot list. The bound is the schema count where the schema declares one (Bound.schema(long), a capacity; a breach is INVALID) and the receiver's max_dyn_array_count where it does not (Bound.receiver(long); a breach is LIMIT_EXCEEDED) — exactly one of the two, never both. §6.2.1 permits this placement — "a corelib MAY take a limit as an argument and perform the check itself, and a port that does is conformant" — and each rule then has one implementation: a caller that passes the bound does not also guard in front of the call.

checkIndex(int, Bound) is that comparison on its own, published for the sites that must judge the index before the reservation runs: a generated fixlenBegin arm bounds a string or blob element's index at the length word, so that a message ending right there is still refused rather than reported INCOMPLETE (MESSAGE_SPEC §5.2), and a native matrix row's header judges the row's index before the row's own element count. The reservation that follows re-runs it, which costs one comparison against a folded constant and spares the two sites from having to agree by inspection.

Nothing here holds a limit. The number is the caller's, used for that one comparison and not retained; there is no default, no fallback and no clamping, and Sofab.ARRAY_MAX is a format ceiling rather than a receiver cap. The schema count is the schema's number, handed in the same way.

The two answers are separate values, and an unstated one is refused. A Bound reaches the comparison with a number only by naming its rule — Bound.schema(long) or Bound.receiver(long) — and each carries its own verdict, so a caller who never configured a cap cannot arrive here spelling what a schema-bounded array spells. A null, and Bound.SCHEMA_BOUNDED (which carries no count), are Sofab.argument(String) rather than an uncompared index. §6.2.1: "no unset state and no unlimited mode", and a codec "MUST NOT read an omitted argument as unlimited".

What the caps here do not cover. A string or blob element's own maxlen is not one of these arguments: the payload arrives through the visitor's own callback, and its length must be decided at the length word so that a message truncated right after that word is refused rather than INCOMPLETE (MESSAGE_SPEC §5.2). There is no reservation here for it to ride — PayloadAcc.checkStringLength(int, Bound) and PayloadAcc.string(int, int, byte[], int, int, Bound) own that half.

The cap on a row's own element count, and on the count of a top-level native array, is not one of these arguments: n below is a length the caller has already bounded, and the two numbers can be governed by different rules — an inner array the schema bounds inside an outer one it does not. A native array count also has no call into this class at all; generated code writes new int[count] straight into its field. Those checks therefore stay in generated code, deliberately, and this class must not grow a helper to hold them: a call invented to carry a check costs more than the guard it replaces.

  • Field Details

    • ARRAY_INIT_CAP

      public static final int ARRAY_INIT_CAP
      Initial element capacity for an array whose length is not bounded by the schema. The announced count decides the ceiling, never the first allocation: a decoder that sized the destination from an untrusted count would let a three-byte header ask for gigabytes, so growth starts here and ensureCap(int[], int, int) doubles it against elements that have actually arrived.
      See Also:
    • EMPTY_BYTES

      public static final byte[] EMPTY_BYTES
      Shared zero-length arrays. A generated field initializer references one of these instead of allocating a fresh empty array per instance — decode replaces the value anyway, and a zero-length array has no state to share.
    • EMPTY_SHORTS

      public static final short[] EMPTY_SHORTS
      The shared empty short[]; see EMPTY_BYTES.
    • EMPTY_INTS

      public static final int[] EMPTY_INTS
      The shared empty int[]; see EMPTY_BYTES.
    • EMPTY_LONGS

      public static final long[] EMPTY_LONGS
      The shared empty long[]; see EMPTY_BYTES.
    • EMPTY_FLOATS

      public static final float[] EMPTY_FLOATS
      The shared empty float[]; see EMPTY_BYTES.
    • EMPTY_DOUBLES

      public static final double[] EMPTY_DOUBLES
      The shared empty double[]; see EMPTY_BYTES.
  • Method Details

    • placeElem

      public static <T> void placeElem(List<T> out, int id, T def, T value, Bound bound)
      Place a leaf element — a wrapper array's string or blob — at the index its wire id names, growing the list and filling the gaps that omitted interior elements left (MESSAGE_SPEC §5.1, §2).

      Three rules of §5.1 sit in this one loop, and not one of them is visible in the bytes: two implementations can disagree about every one and still emit an identical message, which is why they are written here once instead of being re-emitted per schema (CORELIB_PLAN §7.2 item 8 asks for them separately for the same reason).

      • A missing id fills a gap with def rather than shifting every later element down by one — an interior element equal to the element default is omitted by a conformant encoder (§2).
      • The array's length is highest present id + 1, so growing to id + 1 per element is exactly right and no trailing fill is ever needed: the last element is never elided.
      • A repeated id replaces rather than appends (§7.4), which set does by construction and an add could not.

      The index is bounded before the list grows (§7.2 item 8), so a refused id leaves the list exactly as it was and a lower id delivered afterwards still lands at its own index. Where the schema declares a count, bound carries it and a breach is INVALID (MESSAGE_SPEC §7.1); where it declares none, bound carries the receiver cap and a breach is LIMIT_EXCEEDED. Never both (§6.2.1); see checkIndex(int, Bound).

      def is shared, not copied. The gap value of an array of strings or blobs is "" or EMPTY_BYTES — immutable, or zero-length and so with no state to share — so a gap costs no allocation. An element whose default is a mutable object belongs in reserveElem(List, int, Supplier, Bound) instead, which makes one per slot.

      Type Parameters:
      T - element type
      Parameters:
      out - the destination list, which this grows
      id - the element's wire id, which is its index
      def - the element default, filling any gap below id
      value - the decoded element
      bound - Bound.schema(long) carrying the schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count where the schema declares none (§6.2.1) — see checkIndex(int, Bound)
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveElem

      public static <T> void reserveElem(List<T> out, int id, Supplier<T> make, Bound bound)
      Reserve a framed element — the slot a wrapper array's struct, union or nested-array element is routed into: bound the index, then grow the list to id + 1, giving each new slot its own element from make (MESSAGE_SPEC §5.1, §2).

      The same three rules as placeElem(List, int, T, T, Bound) and the same ordering — the index is decided before the list grows (§7.2 item 8) — with one deliberate difference: a slot already present is left alone. A framed element's fields arrive one at a time and each is routed into the object this call reserved, so a re-opened element id must merge into what its earlier fields built rather than start the element again (§7.4); replacing the slot would discard them. That is also why nothing is returned: generated code parks id in its own element-index register and reaches the element through out.get(...) on the field arms that follow.

      A factory, not a shared default. A struct, union or nested row is mutable and reachable by the caller, and an arriving element decodes into the object placed here, so one shared instance would alias every element of the array onto it — the single reason this is a second method rather than placeElem(List, int, T, T, Bound) with one more argument. make is a generated method reference (SomeElem::new): it names a generated type, which is the schema dependence a type parameter is allowed to carry, and javac lowers a non-capturing one to a constant, so the argument allocates nothing per call.

      What this does not own is the routing — binding the element index, switching into the element's scope, emptying a re-opened nested row. That has a different shape per schema and stays generated. This owns growth and the bound, and stops at the slot.

      Type Parameters:
      T - element type
      Parameters:
      out - the destination list, which this grows
      id - the element's wire id, which is its index
      make - the element factory, called once per slot this creates
      bound - Bound.schema(long) carrying the schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count where the schema declares none (§6.2.1) — see checkIndex(int, Bound)
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRow

      public static <T> void reserveRow(List<List<T>> rows, int id, Bound bound)
      Reserve the row at index id of a matrix — an array whose elements are themselves arrays — as an empty row, growing the outer list with empty rows so that a gap in the ids decodes as an empty row instead of shifting every later row down by one.

      Gaps are ordinary: an interior row equal to the element default (the empty row) is omitted by a conformant encoder (MESSAGE_SPEC §2), and only the last row is guaranteed present — which is what makes the decoded length, highest present id + 1, exact. The row is replaced rather than merged into, because an array wrapper is the array's value (§7.4).

      "Replaced" is a statement about the value, not the object: an already present row is emptied in place — the same rule reset(List) follows for a reused decode destination — instead of being swapped for a fresh list. Decoding N rows then allocates N lists rather than 2N, one to grow into the slot and one to overwrite it. A caller holding a reference to a row across a decode into the same destination sees it emptied; a decode destination is not shared.

      id is the wire's, and this grows the list to hold it, so it is bounded here. Which bound depends on the outer array: where the schema declares a capacity, bound carries it and a breach is INVALID (MESSAGE_SPEC §7.1); where the schema declares none, bound carries the receiver cap and a breach is LIMIT_EXCEEDED (§6.2.1).

      Type Parameters:
      T - row element type
      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count where the schema declares none (§6.2.1) — see checkIndex(int, Bound)
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRowBytes

      public static byte[] reserveRowBytes(List<byte[]> rows, int id, int n, Bound bound)
      reserveRow(List, int, Bound) for a primitive row: the same id-keyed placement and the same gap fill with the empty row, but the new row is handed back so the caller can fill it by index instead of reading it out of the list once per element.

      n is the caller's capped reservation, never the wire count — an untrusted count must not be able to force an up-front allocation — and ensureCap(byte[], int, int) grows the row as elements arrive.

      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      n - initial length of the new row
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count, bounding the row index — see checkIndex(int, Bound). It does not bound n, which the caller has already bounded.
      Returns:
      the new row, now at index id
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRowShorts

      public static short[] reserveRowShorts(List<short[]> rows, int id, int n, Bound bound)
      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      n - initial length of the new row
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count, bounding the row index — see checkIndex(int, Bound). It does not bound n, which the caller has already bounded.
      Returns:
      the new row, now at index id
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRowInts

      public static int[] reserveRowInts(List<int[]> rows, int id, int n, Bound bound)
      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      n - initial length of the new row
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count, bounding the row index — see checkIndex(int, Bound). It does not bound n, which the caller has already bounded.
      Returns:
      the new row, now at index id
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRowLongs

      public static long[] reserveRowLongs(List<long[]> rows, int id, int n, Bound bound)
      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      n - initial length of the new row
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count, bounding the row index — see checkIndex(int, Bound). It does not bound n, which the caller has already bounded.
      Returns:
      the new row, now at index id
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRowFloats

      public static float[] reserveRowFloats(List<float[]> rows, int id, int n, Bound bound)
      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      n - initial length of the new row
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count, bounding the row index — see checkIndex(int, Bound). It does not bound n, which the caller has already bounded.
      Returns:
      the new row, now at index id
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • reserveRowDoubles

      public static double[] reserveRowDoubles(List<double[]> rows, int id, int n, Bound bound)
      Parameters:
      rows - the outer list, one entry per row
      id - index of the row to reserve
      n - initial length of the new row
      bound - Bound.schema(long) carrying the outer array's schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count, bounding the row index — see checkIndex(int, Bound). It does not bound n, which the caller has already bounded.
      Returns:
      the new row, now at index id
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED; the list is left untouched in all three
    • checkIndex

      public static void checkIndex(int id, Bound bound)
      The element-index rule, written once for all nine reservations — and published for the one site that has none to ride. A bound must have been stated, and the index is refused before anything is created and before the list is grown to hold it (CORELIB_PLAN §7.2 item 8), so a lower id delivered after a refused one still lands at its own index.

      Which verdict a breach earns is the bound's, and exactly one of the two is ever in force (§6.2.1, "never both"):

      The site with no reservation is the length word of a string or blob element: generated code bounds the element's index there, so that a message ending right after that word is refused rather than reported INCOMPLETE (MESSAGE_SPEC §5.2), and the placement that follows — placeElem(List, int, T, T, Bound) — runs the same comparison again against the same folded constant.

      The index is compared with >= rather than > because a wrapper array's length is highest present id + 1 (MESSAGE_SPEC §5.1): an element at index n makes a list of n + 1. Rejected, never clamped — placing the element at n - 1 instead would be data corruption.

      A missing bound, or Bound.SCHEMA_BOUNDED — which carries no count to compare — is a defect in the call and answers SofabError.ARGUMENT, not a verdict on the bytes and not silence, which would grow the list uncompared (§6.3).

      The fast path is one comparison against a folded constant; the verdict is decided out of line, on the path that throws.

      Parameters:
      id - the element's wire id, which is its index
      bound - Bound.schema(long) carrying the schema count, or Bound.receiver(long) carrying the caller's max_dyn_array_count (§6.2.1)
      Throws:
      UncheckedIOException - wrapping an INVALID_MSG SofabException when id is at or past the schema count, a LIMIT_EXCEEDED one when it is at or past the receiver cap, or an ARGUMENT one when bound is null or Bound.SCHEMA_BOUNDED
    • reset

      public static <T> List<T> reset(List<T> list)
      Empty a list in place, keeping its capacity, and materialize one only when the field is null.

      A generated reset() calls this, so re-arming a reused decode destination costs no allocation — which is the whole point of taking a destination from the caller.

      Type Parameters:
      T - element type
      Parameters:
      list - the list to re-arm, possibly null
      Returns:
      list, emptied, or a fresh list when it was null
    • orEmpty

      public static <T> List<T> orEmpty(List<T> list)
      The null-absorbing identity a wrapper array is read through on the encode side — by the serialize loop and by the all-default predicate alike.

      No narrowing happens here and none may: the wire count is a compact array's length and the highest wrapper id is its last index (MESSAGE_SPEC §3 / §5.1), so dropping a trailing default element would not re-shape the bytes, it would shorten the value. What the interior may drop — a leaf equal to the element default, an all-default sequence element — is decided per element inside the loop, never here.

      Type Parameters:
      T - element type
      Parameters:
      list - the field's value, possibly null
      Returns:
      list, or an immutable empty list when it was null
    • boolsToLongs

      public static long[] boolsToLongs(List<Boolean> values)
      Convert a boolean wrapper array to the long[] the encoder writes.

      This is the one boxed-to-primitive conversion the encode side still needs. Every other native array — top level or a matrix row — is already a primitive array of its declared width and reaches OStream.writeArrayUnsigned(int, long[]) and friends unconverted; a boolean array is the one with no primitive overload of its own, bool being carried on the wire as an unsigned 0 or 1.

      Parameters:
      values - the field's value; a null element counts as false
      Returns:
      one long per element, 1 for true and 0 for false
    • ensureCap

      public static byte[] ensureCap(byte[] a, int i, int cap)
      Enlarge a so index i can be written, doubling its length but never exceeding cap.

      This is the growth policy for an array being filled element by element, and its whole point is that it tracks elements that have actually arrived. The alternative — sizing the destination from the announced count — hands an attacker a multi-gigabyte allocation for a three-byte header, since a count is bounded only by Sofab.ARRAY_MAX until a schema count or a receiver limit bounds it. Doubling keeps the fill amortized O(n), and the cap clamp means a valid array of the announced length still ends up exactly right-sized rather than at the next power of two.

      cap is a ceiling on the result, not a bound the caller is relieved of checking: it is the announced count for an unbounded field and the schema capacity for a bounded one, both already validated by the caller, and a fill that stays below its own count therefore never sees it clamp. Returns a untouched whenever i already fits, so the call sits on the hot path unguarded.

      Parameters:
      a - the array so far
      i - index about to be written
      cap - growth ceiling: the announced or declared element count
      Returns:
      a, or a longer copy of it
    • ensureCap

      public static short[] ensureCap(short[] a, int i, int cap)
      Parameters:
      a - the array so far
      i - index about to be written
      cap - growth ceiling: the announced or declared element count
      Returns:
      a, or a longer copy of it
    • ensureCap

      public static int[] ensureCap(int[] a, int i, int cap)
      Parameters:
      a - the array so far
      i - index about to be written
      cap - growth ceiling: the announced or declared element count
      Returns:
      a, or a longer copy of it
    • ensureCap

      public static long[] ensureCap(long[] a, int i, int cap)
      Parameters:
      a - the array so far
      i - index about to be written
      cap - growth ceiling: the announced or declared element count
      Returns:
      a, or a longer copy of it
    • ensureCap

      public static float[] ensureCap(float[] a, int i, int cap)
      Parameters:
      a - the array so far
      i - index about to be written
      cap - growth ceiling: the announced or declared element count
      Returns:
      a, or a longer copy of it
    • ensureCap

      public static double[] ensureCap(double[] a, int i, int cap)
      Parameters:
      a - the array so far
      i - index about to be written
      cap - growth ceiling: the announced or declared element count
      Returns:
      a, or a longer copy of it