Class ArrayBufferWriter

java.lang.Object
io.kubfly.sdk.buffer.ArrayBufferWriter

public final class ArrayBufferWriter extends Object
Writes values into a byte[] at an offset and returns the offset after them — or, with a null buffer, only counts: returns the same offset without writing anything. One method thus both measures a message and writes it, so the size reserved in a shared buffer always matches the bytes written:
static int event(byte[] buffer, int offset, long id, String message) {
    offset = ArrayBufferWriter.writeZigZagLong(buffer, offset, id);
    return ArrayBufferWriter.writeString(buffer, offset, message);
}

int size = event(null, 0, id, message);                 // count
int at = position.getAndAdd(size);                     // reserve
event(lane, at, id, message);                          // write
The methods are on the hot path of the collectors: no allocation (except writeBigDecimal(byte[], int, BigDecimal) and a BigInteger beyond 63 bits), no locks, no checks beyond the array bounds the JVM checks anyway; the caller reserves the room. Fixed-width values are little-endian, except a UUID (big-endian, as RFC 9562 prints it) and the two's complement of a BigInteger (big-endian, as Java writes it); integers of variable length are LEB128 (7 bits a byte, the high bit — more bytes follow), signed ones zigzag first (0, -1, 1, -2 … become 0, 1, 2, 3 …), so small values take one byte whatever their sign. NaN is written canonical; strings are UTF-8, a lone surrogate written as U+FFFD. Reference values must not be null: how a format marks absence is up to the format. BufferReader reads all of these back. The encodings are the same in the SDKs of all languages: sdk/spec/buffer.md, checked by sdk/spec/buffer/vectors.tsv.

Offsets are int: a buffer of this writer is a Java array, at most 2 GiB. A writer of larger buffers would be a second path with the same encodings (a MemorySegmentBufferWriter with long offsets, say); this one stays the fast path, where the JVM removes the bounds checks of an array.

  • Method Details

    • writeBoolean

      public static int writeBoolean(byte[] buffer, int offset, boolean value)
      Writes a boolean: one byte, 0 or 1.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeByte

      public static int writeByte(byte[] buffer, int offset, byte value)
      Writes a byte: one byte.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeChar

      public static int writeChar(byte[] buffer, int offset, char value)
      Writes a char: two bytes, little-endian — the UTF-16 code unit as it is, a surrogate too.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeShort

      public static int writeShort(byte[] buffer, int offset, short value)
      Writes a short: two bytes, little-endian.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeInt

      public static int writeInt(byte[] buffer, int offset, int value)
      Writes an int: four bytes, little-endian.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeLong

      public static int writeLong(byte[] buffer, int offset, long value)
      Writes a long: eight bytes, little-endian.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeULong

      public static int writeULong(byte[] buffer, int offset, long value)
      Writes an unsigned 64-bit integer: its bits as writeVarLong(byte[], int, long) (1–10 bytes) — the same bytes, the meaning says unsigned: -1 is 2^64 − 1.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the 64 bits, read as unsigned
      Returns:
      the offset after the value
    • writeFloat

      public static int writeFloat(byte[] buffer, int offset, float value)
      Writes a float: four bytes, IEEE 754, little-endian; NaN as the canonical 0x7FC00000.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeDouble

      public static int writeDouble(byte[] buffer, int offset, double value)
      Writes a double: eight bytes, IEEE 754, little-endian; NaN as the canonical 0x7FF8000000000000, -0.0 as it is.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeVarInt

      public static int writeVarInt(byte[] buffer, int offset, int value)
      Writes an unsigned int of variable length: 1–5 bytes, LEB128; a negative value is taken as unsigned (−1 takes 5 bytes) — for signed values writeZigZagInt(byte[], int, int).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value, unsigned
      Returns:
      the offset after the value
    • writeVarLong

      public static int writeVarLong(byte[] buffer, int offset, long value)
      Writes an unsigned long of variable length: 1–10 bytes, LEB128; a negative value is taken as unsigned (−1 takes 10 bytes) — for signed values writeZigZagLong(byte[], int, long).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value, unsigned
      Returns:
      the offset after the value
    • writeZigZagInt

      public static int writeZigZagInt(byte[] buffer, int offset, int value)
      Writes a signed int of variable length: zigzag, then writeVarInt(byte[], int, int) — 1–5 bytes, small values of either sign in one byte (−64 … 63).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeZigZagLong

      public static int writeZigZagLong(byte[] buffer, int offset, long value)
      Writes a signed long of variable length: zigzag, then writeVarLong(byte[], int, long) — 1–10 bytes, small values of either sign in one byte (−64 … 63).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeEnum

      public static int writeEnum(byte[] buffer, int offset, Enum<?> value)
      Writes an enum constant: the name of its declaring class (writeString(byte[], int, String)), then its name — never its ordinal. Allocates nothing: both names are kept by the JVM.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the constant
      Returns:
      the offset after the value
    • writeEnum

      public static int writeEnum(byte[] buffer, int offset, String type, String name)
      Writes an enum constant given by its names: the type (writeString(byte[], int, String); empty if there is none), then the name of the constant.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      type - the type
      name - the name of the constant
      Returns:
      the offset after the value
    • writeUuid

      public static int writeUuid(byte[] buffer, int offset, UUID value)
      Writes a UUID: 16 bytes, big-endian — the most significant half first, the bytes in the order RFC 9562 prints them.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeBigInteger

      public static int writeBigInteger(byte[] buffer, int offset, BigInteger value)
      Writes an integer of any size: its length in bytes (writeVarInt(byte[], int, int)), then two's complement, big-endian, of minimal length — the bytes of BigInteger.toByteArray() (zero is one byte 00). A value of up to 63 bits allocates nothing; a larger one is copied through toByteArray().
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeBigDecimal

      public static int writeBigDecimal(byte[] buffer, int offset, BigDecimal value)
      Writes an exact decimal: its scale (writeZigZagInt(byte[], int, int)), then the unscaled value (writeBigInteger(byte[], int, BigInteger)); the scale is kept — 1.0 and 1.00 are different values, as in BigDecimal.equals(Object). Allocates a small object each call, counting too: the unscaled value is public only as a new BigInteger (BigDecimal.unscaledValue()), except for −16…16.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeInstant

      public static int writeInstant(byte[] buffer, int offset, Instant value)
      Writes an instant: the seconds of the epoch (writeZigZagLong(byte[], int, long)), then the nanoseconds of the second (writeVarInt(byte[], int, int), 0–999 999 999).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeLocalDate

      public static int writeLocalDate(byte[] buffer, int offset, LocalDate value)
      Writes a date: the day of the epoch, writeZigZagLong(byte[], int, long) (1970-01-01 is 0).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeLocalTime

      public static int writeLocalTime(byte[] buffer, int offset, LocalTime value)
      Writes a time of day: the nanoseconds of the day, writeVarLong(byte[], int, long).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeLocalDateTime

      public static int writeLocalDateTime(byte[] buffer, int offset, LocalDateTime value)
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeOffsetDateTime

      public static int writeOffsetDateTime(byte[] buffer, int offset, OffsetDateTime value)
      Writes a date and time with an offset: writeLocalDateTime(byte[], int, LocalDateTime), then the offset in seconds, writeZigZagInt(byte[], int, int).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeZonedDateTime

      public static int writeZonedDateTime(byte[] buffer, int offset, ZonedDateTime value)
      Writes a date and time in a zone: writeLocalDateTime(byte[], int, LocalDateTime), the offset in seconds (writeZigZagInt(byte[], int, int) — it tells the two occurrences of an hour repeated at a clock change apart), then the zone identifier (writeString(byte[], int, String)).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeDuration

      public static int writeDuration(byte[] buffer, int offset, Duration value)
      Writes a duration: the seconds (writeZigZagLong(byte[], int, long)), then the nanoseconds added to them (writeVarInt(byte[], int, int), 0–999 999 999; −0.5 s is −1 s and 500 000 000 ns).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeString

      public static int writeString(byte[] buffer, int offset, String value)
      Writes a string: its length in UTF-8 bytes (writeVarInt(byte[], int, int)), then UTF-8; a lone surrogate becomes U+FFFD (3 bytes). Counting goes through the characters once; writing, usually, once too — the bytes go right after the room for the length, and the length is written last.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      Returns:
      the offset after the value
    • writeString

      public static int writeString(byte[] buffer, int offset, String value, int maxBytes)
      Writes a string cut to at most maxBytes UTF-8 bytes, at a character boundary (a surrogate pair is never split): its length in bytes (writeVarInt(byte[], int, int)), then UTF-8; a lone surrogate becomes U+FFFD (3 bytes). Whether it was cut: compare with the count of writeString(byte[], int, String).
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the value
      maxBytes - the most bytes of UTF-8 written, not counting the length
      Returns:
      the offset after the value
    • utf8Length

      public static int utf8Length(String value)
      The UTF-8 bytes of a string, as writeUtf8(byte[], int, String) writes it: a lone surrogate takes 3, as U+FFFD.
      Parameters:
      value - the string
      Returns:
      the bytes
    • writeUtf8

      public static int writeUtf8(byte[] buffer, int offset, String value)
      Writes a string as UTF-8 without its length (utf8Length(String) bytes): a lone surrogate becomes U+FFFD.
      Parameters:
      buffer - the buffer, with room
      offset - where to write
      value - the value
      Returns:
      the offset after it
    • writeBytes

      public static int writeBytes(byte[] buffer, int offset, byte[] value)
      Writes bytes: their number (writeVarInt(byte[], int, int)), then the bytes.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the bytes
      Returns:
      the offset after the value
    • writeBytes

      public static int writeBytes(byte[] buffer, int offset, byte[] value, int from, int length)
      Writes a range of bytes: their number (writeVarInt(byte[], int, int)), then the bytes.
      Parameters:
      buffer - the buffer or null to count only
      offset - where to write
      value - the array holding the bytes
      from - the first byte in value
      length - the number of bytes
      Returns:
      the offset after the value