Class ArrayBufferWriter
java.lang.Object
io.kubfly.sdk.buffer.ArrayBufferWriter
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 Summary
Modifier and TypeMethodDescriptionstatic intutf8Length(String value) The UTF-8 bytes of a string, aswriteUtf8(byte[], int, String)writes it: a lone surrogate takes 3, as U+FFFD.static intwriteBigDecimal(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.0and1.00are different values, as inBigDecimal.equals(Object).static intwriteBigInteger(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 ofBigInteger.toByteArray()(zero is one byte00).static intwriteBoolean(byte[] buffer, int offset, boolean value) Writes aboolean: one byte, 0 or 1.static intwriteByte(byte[] buffer, int offset, byte value) Writes abyte: one byte.static intwriteBytes(byte[] buffer, int offset, byte[] value) Writes bytes: their number (writeVarInt(byte[], int, int)), then the bytes.static intwriteBytes(byte[] buffer, int offset, byte[] value, int from, int length) Writes a range of bytes: their number (writeVarInt(byte[], int, int)), then the bytes.static intwriteChar(byte[] buffer, int offset, char value) Writes achar: two bytes, little-endian — the UTF-16 code unit as it is, a surrogate too.static intwriteDouble(byte[] buffer, int offset, double value) Writes adouble: eight bytes, IEEE 754, little-endian;NaNas the canonical0x7FF8000000000000,-0.0as it is.static intwriteDuration(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).static intWrites an enum constant: the name of its declaring class (writeString(byte[], int, String)), then its name — never its ordinal.static intWrites an enum constant given by its names: the type (writeString(byte[], int, String); empty if there is none), then the name of the constant.static intwriteFloat(byte[] buffer, int offset, float value) Writes afloat: four bytes, IEEE 754, little-endian;NaNas the canonical0x7FC00000.static intwriteInstant(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).static intwriteInt(byte[] buffer, int offset, int value) Writes anint: four bytes, little-endian.static intwriteLocalDate(byte[] buffer, int offset, LocalDate value) Writes a date: the day of the epoch,writeZigZagLong(byte[], int, long)(1970-01-01 is 0).static intwriteLocalDateTime(byte[] buffer, int offset, LocalDateTime value) Writes a date and time:writeLocalDate(byte[], int, LocalDate), thenwriteLocalTime(byte[], int, LocalTime).static intwriteLocalTime(byte[] buffer, int offset, LocalTime value) Writes a time of day: the nanoseconds of the day,writeVarLong(byte[], int, long).static intwriteLong(byte[] buffer, int offset, long value) Writes along: eight bytes, little-endian.static intwriteOffsetDateTime(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).static intwriteShort(byte[] buffer, int offset, short value) Writes ashort: two bytes, little-endian.static intwriteString(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).static intwriteString(byte[] buffer, int offset, String value, int maxBytes) Writes a string cut to at mostmaxBytesUTF-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).static intwriteULong(byte[] buffer, int offset, long value) Writes an unsigned 64-bit integer: its bits aswriteVarLong(byte[], int, long)(1–10 bytes) — the same bytes, the meaning says unsigned:-1is 2^64 − 1.static intWrites a string as UTF-8 without its length (utf8Length(String)bytes): a lone surrogate becomes U+FFFD.static intWrites a UUID: 16 bytes, big-endian — the most significant half first, the bytes in the order RFC 9562 prints them.static intwriteVarInt(byte[] buffer, int offset, int value) Writes an unsignedintof variable length: 1–5 bytes, LEB128; a negative value is taken as unsigned (−1 takes 5 bytes) — for signed valueswriteZigZagInt(byte[], int, int).static intwriteVarLong(byte[] buffer, int offset, long value) Writes an unsignedlongof variable length: 1–10 bytes, LEB128; a negative value is taken as unsigned (−1 takes 10 bytes) — for signed valueswriteZigZagLong(byte[], int, long).static intwriteZigZagInt(byte[] buffer, int offset, int value) Writes a signedintof variable length: zigzag, thenwriteVarInt(byte[], int, int)— 1–5 bytes, small values of either sign in one byte (−64 … 63).static intwriteZigZagLong(byte[] buffer, int offset, long value) Writes a signedlongof variable length: zigzag, thenwriteVarLong(byte[], int, long)— 1–10 bytes, small values of either sign in one byte (−64 … 63).static intwriteZonedDateTime(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)).
-
Method Details
-
writeBoolean
public static int writeBoolean(byte[] buffer, int offset, boolean value) Writes aboolean: one byte, 0 or 1.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeByte
public static int writeByte(byte[] buffer, int offset, byte value) Writes abyte: one byte.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeChar
public static int writeChar(byte[] buffer, int offset, char value) Writes achar: two bytes, little-endian — the UTF-16 code unit as it is, a surrogate too.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeShort
public static int writeShort(byte[] buffer, int offset, short value) Writes ashort: two bytes, little-endian.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeInt
public static int writeInt(byte[] buffer, int offset, int value) Writes anint: four bytes, little-endian.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeLong
public static int writeLong(byte[] buffer, int offset, long value) Writes along: eight bytes, little-endian.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- 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 aswriteVarLong(byte[], int, long)(1–10 bytes) — the same bytes, the meaning says unsigned:-1is 2^64 − 1.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the 64 bits, read as unsigned- Returns:
- the offset after the value
-
writeFloat
public static int writeFloat(byte[] buffer, int offset, float value) Writes afloat: four bytes, IEEE 754, little-endian;NaNas the canonical0x7FC00000.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeDouble
public static int writeDouble(byte[] buffer, int offset, double value) Writes adouble: eight bytes, IEEE 754, little-endian;NaNas the canonical0x7FF8000000000000,-0.0as it is.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeVarInt
public static int writeVarInt(byte[] buffer, int offset, int value) Writes an unsignedintof variable length: 1–5 bytes, LEB128; a negative value is taken as unsigned (−1 takes 5 bytes) — for signed valueswriteZigZagInt(byte[], int, int).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value, unsigned- Returns:
- the offset after the value
-
writeVarLong
public static int writeVarLong(byte[] buffer, int offset, long value) Writes an unsignedlongof variable length: 1–10 bytes, LEB128; a negative value is taken as unsigned (−1 takes 10 bytes) — for signed valueswriteZigZagLong(byte[], int, long).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value, unsigned- Returns:
- the offset after the value
-
writeZigZagInt
public static int writeZigZagInt(byte[] buffer, int offset, int value) Writes a signedintof variable length: zigzag, thenwriteVarInt(byte[], int, int)— 1–5 bytes, small values of either sign in one byte (−64 … 63).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeZigZagLong
public static int writeZigZagLong(byte[] buffer, int offset, long value) Writes a signedlongof variable length: zigzag, thenwriteVarLong(byte[], int, long)— 1–10 bytes, small values of either sign in one byte (−64 … 63).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeEnum
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 ornullto count onlyoffset- where to writevalue- the constant- Returns:
- the offset after the value
-
writeEnum
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 ornullto count onlyoffset- where to writetype- the typename- the name of the constant- Returns:
- the offset after the value
-
writeUuid
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 ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeBigInteger
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 ofBigInteger.toByteArray()(zero is one byte00). A value of up to 63 bits allocates nothing; a larger one is copied throughtoByteArray().- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeBigDecimal
Writes an exact decimal: its scale (writeZigZagInt(byte[], int, int)), then the unscaled value (writeBigInteger(byte[], int, BigInteger)); the scale is kept —1.0and1.00are different values, as inBigDecimal.equals(Object). Allocates a small object each call, counting too: the unscaled value is public only as a newBigInteger(BigDecimal.unscaledValue()), except for −16…16.- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeInstant
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 ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeLocalDate
Writes a date: the day of the epoch,writeZigZagLong(byte[], int, long)(1970-01-01 is 0).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeLocalTime
Writes a time of day: the nanoseconds of the day,writeVarLong(byte[], int, long).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeLocalDateTime
Writes a date and time:writeLocalDate(byte[], int, LocalDate), thenwriteLocalTime(byte[], int, LocalTime).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeOffsetDateTime
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 ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeZonedDateTime
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 ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeDuration
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 ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeString
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 ornullto count onlyoffset- where to writevalue- the value- Returns:
- the offset after the value
-
writeString
Writes a string cut to at mostmaxBytesUTF-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 ofwriteString(byte[], int, String).- Parameters:
buffer- the buffer ornullto count onlyoffset- where to writevalue- the valuemaxBytes- the most bytes of UTF-8 written, not counting the length- Returns:
- the offset after the value
-
utf8Length
The UTF-8 bytes of a string, aswriteUtf8(byte[], int, String)writes it: a lone surrogate takes 3, as U+FFFD.- Parameters:
value- the string- Returns:
- the bytes
-
writeUtf8
Writes a string as UTF-8 without its length (utf8Length(String)bytes): a lone surrogate becomes U+FFFD.- Parameters:
buffer- the buffer, with roomoffset- where to writevalue- 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 ornullto count onlyoffset- where to writevalue- 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 ornullto count onlyoffset- where to writevalue- the array holding the bytesfrom- the first byte invaluelength- the number of bytes- Returns:
- the offset after the value
-