Top.Mail.Ru
package buffer — KubFly SDK for Go

KubFly SDK for Go / buffer

package buffer

import "kubfly.io/sdk/buffer"

Package buffer holds the primitives of the binary formats of KubFly: how one value is written and read back. The encodings are the same in the SDKs of all languages (sdk/spec/buffer.md), checked by the vectors of sdk/spec/buffer/vectors.tsv; the Java SDK is the reference.

The writer is a set of functions that write a value into a byte slice at an offset and return the offset after it — or, with a nil buffer, only count: return the same offset as if they had written. One function thus both measures a message and writes it, so the room reserved in a shared buffer always matches the bytes written:

func event(into []byte, offset int, id int64, message string) int {
	offset = buffer.WriteZigZagLong(into, offset, id)
	return buffer.WriteString(into, offset, message)
}

size := event(nil, 0, id, message)        // count
at := int(position.Add(int64(size))) - size // reserve
event(lane, at, id, message)               // write

The functions are on the hot path of the collectors: no allocation (except WriteBigInteger beyond 63 bits), no locks, no checks beyond the bounds checks of Go; the caller reserves the room. They are the ArrayBufferWriter of the Java SDK, whose static methods they are. Fixed-width values are little-endian, except a UUID (big-endian, as RFC 9562 prints it) and the two's complement of a big integer (big-endian); integers of variable length are LEB128, signed ones zigzag first, so small values take one byte whatever their sign. NaN is written canonical; strings are strict UTF-8.

Reader reads them all back (the BufferReader of the Java SDK). The values Go has no exact type for — a decimal, a date beyond the years of time.Time, a duration beyond 292 years — are types of this package that keep the fields of their encoding and print as Java prints them.

Constants

const MinSize = 16

MinSize is the smallest buffer of a Reader: the widest fixed value, a UUID, fits it.

const Size = 64 * 1024

Size is the size of the buffer of a Reader by default.

Functions

func WriteBigDecimal

func WriteBigDecimal(buffer []byte, offset int, value BigDecimal) int

WriteBigDecimal writes a decimal: its scale (WriteZigZagInt), then its unscaled value (WriteBigInteger); 12.50 is the scale 2 and 1250, and the scale is kept: 1.0 and 1.00 are different values.

func WriteBigInteger

func WriteBigInteger(buffer []byte, offset int, value *big.Int) int

WriteBigInteger writes an integer of any size: the count of its bytes (WriteVarInt), then its two's complement, big-endian, of the least length — as BigInteger.toByteArray() of Java: 0 is 00, 128 is 00 80, -128 is 80.

func WriteBoolean

func WriteBoolean(buffer []byte, offset int, value bool) int

WriteBoolean writes a boolean: one byte, 0 or 1.

func WriteByte

func WriteByte(buffer []byte, offset int, value byte) int

WriteByte writes a byte as it is.

func WriteBytes

func WriteBytes(buffer []byte, offset int, value []byte) int

WriteBytes writes bytes: their count (WriteVarInt), then the bytes.

func WriteChar

func WriteChar(buffer []byte, offset int, value uint16) int

WriteChar writes a UTF-16 code unit, a surrogate too: two bytes, little-endian.

func WriteDouble

func WriteDouble(buffer []byte, offset int, value float64) int

WriteDouble writes a 64-bit float: IEEE 754, little-endian; NaN as the canonical 0x7FF8000000000000, -0.0 kept.

func WriteDuration

func WriteDuration(buffer []byte, offset int, value Duration) int

WriteDuration writes a duration: its seconds (WriteZigZagLong), then the nanoseconds added to them (WriteVarInt, 0–999 999 999): -0.5 s is -1 s and 500 000 000 ns.

func WriteEnum

func WriteEnum(buffer []byte, offset int, typ string, name string) int

WriteEnum writes a constant of an enumeration: the name of its type (empty when the language has none), then the name of the constant; never its ordinal, which changes when the constants are reordered.

func WriteFloat

func WriteFloat(buffer []byte, offset int, value float32) int

WriteFloat writes a 32-bit float: IEEE 754, little-endian; NaN as the canonical 0x7FC00000.

func WriteInstant

func WriteInstant(buffer []byte, offset int, value Instant) int

WriteInstant writes a moment: the seconds of the epoch (WriteZigZagLong), then the nanoseconds of the second (WriteVarInt).

func WriteInt

func WriteInt(buffer []byte, offset int, value int32) int

WriteInt writes a 32-bit integer: four bytes, little-endian.

func WriteLocalDate

func WriteLocalDate(buffer []byte, offset int, value LocalDate) int

WriteLocalDate writes a date: its day of the epoch, 1970-01-01 is 0 (WriteZigZagLong).

func WriteLocalDateTime

func WriteLocalDateTime(buffer []byte, offset int, value LocalDateTime) int

WriteLocalDateTime writes a date and a time of day: WriteLocalDate, then WriteLocalTime.

func WriteLocalTime

func WriteLocalTime(buffer []byte, offset int, value LocalTime) int

WriteLocalTime writes a time of day: its nanoseconds of the day (WriteVarLong).

func WriteLong

func WriteLong(buffer []byte, offset int, value int64) int

WriteLong writes a 64-bit integer: eight bytes, little-endian.

func WriteOffsetDateTime

func WriteOffsetDateTime(buffer []byte, offset int, value OffsetDateTime) int

WriteOffsetDateTime writes a date and a time with an offset: WriteLocalDateTime, then the offset in seconds (WriteZigZagInt).

func WriteShort

func WriteShort(buffer []byte, offset int, value int16) int

WriteShort writes a 16-bit integer: two bytes, little-endian.

func WriteString

func WriteString(buffer []byte, offset int, value string) int

WriteString writes a string: its length in bytes of UTF-8 (WriteVarInt), then strict UTF-8. Bytes of a Go string that are not UTF-8 are written as U+FFFD: a surrogate encoded in three bytes as one, any other byte as one each.

func WriteStringMax

func WriteStringMax(buffer []byte, offset int, value string, maxBytes int) int

WriteStringMax writes a string cut to at most maxBytes bytes of UTF-8 at the boundary of a character, then as WriteString.

func WriteULong

func WriteULong(buffer []byte, offset int, value uint64) int

WriteULong writes an unsigned 64-bit integer: the bytes of WriteVarLong, read as unsigned.

func WriteUuid

func WriteUuid(buffer []byte, offset int, value uuid.UUID) int

WriteUuid writes a UUID: its 16 bytes as RFC 9562 prints them, big-endian.

func WriteVarInt

func WriteVarInt(buffer []byte, offset int, value int32) int

WriteVarInt writes a 32-bit integer as unsigned LEB128: 1–5 bytes; a negative one takes 5.

func WriteVarLong

func WriteVarLong(buffer []byte, offset int, value int64) int

WriteVarLong writes a 64-bit integer as unsigned LEB128: 1–10 bytes; a negative one takes 10.

func WriteZigZagInt

func WriteZigZagInt(buffer []byte, offset int, value int32) int

WriteZigZagInt writes a signed 32-bit integer zigzag, then as WriteVarInt: -64…63 take one byte.

func WriteZigZagLong

func WriteZigZagLong(buffer []byte, offset int, value int64) int

WriteZigZagLong writes a signed 64-bit integer zigzag, then as WriteVarLong.

func WriteZonedDateTime

func WriteZonedDateTime(buffer []byte, offset int, value ZonedDateTime) int

WriteZonedDateTime writes a date and a time in a zone: WriteLocalDateTime, the offset (WriteZigZagInt), then the identifier of the zone (WriteString). The offset tells the two equal hours apart when the clocks go back.

Types

type BigDecimal

type BigDecimal struct {
	// Unscaled is the value without the scale.
	Unscaled *big.Int
	// Scale is the count of digits after the point.
	Scale int32
}

BigDecimal is a decimal number: an integer without a scale and the scale, the count of digits after the point; a negative scale multiplies by a power of ten. 12.50 is 1250 and 2, 1E+3 is 1 and -3.

func (BigDecimal) String

func (d BigDecimal) String() string

String prints the decimal as BigDecimal.toString() of Java: plain while the exponent is -6 or more and the scale is not negative, scientific otherwise (1E+3).

type Duration

type Duration struct {
	// Seconds are the whole seconds, rounded down.
	Seconds int64
	// Nanos are the nanoseconds added to the seconds.
	Nanos int32
}

Duration is an amount of time: seconds and the nanoseconds added to them, 0–999 999 999; -0.5 s is -1 s and 500 000 000 ns. It holds what time.Duration does not: more than 292 years.

func DurationOf

func DurationOf(d time.Duration) Duration

DurationOf is the amount of a time.Duration.

func (Duration) Duration

func (d Duration) Duration() (time.Duration, bool)

Duration is the amount as a time.Duration, and whether it fits one.

func (Duration) String

func (d Duration) String() string

String prints the amount as Duration.toString() of Java: PT0S, PT1.5S, PT-0.5S, PT2H30M.

type EnumConstant

type EnumConstant struct {
	// Type is the name of the type, empty when the language has none.
	Type string
	// Name is the name of the constant.
	Name string
}

EnumConstant is a constant of an enumeration as the format has it: the name of its type and its own name.

func (EnumConstant) String

func (c EnumConstant) String() string

String prints the type and the name with a dot between, or the name alone.

type FormatError

type FormatError struct {
	// Position is where the value begins, in bytes from the start of the data.
	Position int64
	// Message tells what is wrong.
	Message string
	// Err is the cause, or nil.
	Err error
}

FormatError tells that the data are not of the format: a value that no writer writes. It is the BufferFormatException of the Java SDK.

func (*FormatError) Error

func (e *FormatError) Error() string

Error is the message with the position.

func (*FormatError) Unwrap

func (e *FormatError) Unwrap() error

Unwrap is the cause.

type Instant

type Instant struct {
	// Seconds are the seconds since 1970-01-01T00:00:00Z.
	Seconds int64
	// Nanos are the nanoseconds of the second.
	Nanos int32
}

Instant is a moment: seconds of the epoch and nanoseconds of the second, 0–999 999 999.

func InstantOf

func InstantOf(t time.Time) Instant

InstantOf is the moment of a time of Go.

func (Instant) String

func (i Instant) String() string

String prints the moment as Instant.toString() of Java: 2026-11-01T12:34:56.789Z.

func (Instant) Time

func (i Instant) Time() time.Time

Time is the moment as a time of Go, in UTC.

type LocalDate

type LocalDate struct {
	// EpochDay is the count of days since 1970-01-01.
	EpochDay int64
}

LocalDate is a date without a zone: its day of the epoch, 1970-01-01 is 0.

func LocalDateOf

func LocalDateOf(t time.Time) LocalDate

LocalDateOf is the date of a time of Go, in its own zone.

func (LocalDate) Date

func (d LocalDate) Date() (year int64, month int, day int)

Date is the year, the month (1–12) and the day of the month.

func (LocalDate) String

func (d LocalDate) String() string

String prints the date as LocalDate.toString() of Java: 2026-11-01, -0001-01-01, +10000-01-01.

type LocalDateTime

type LocalDateTime struct {
	// Date is the date.
	Date LocalDate
	// Time is the time of day.
	Time LocalTime
}

LocalDateTime is a date and a time of day without a zone.

func LocalDateTimeOf

func LocalDateTimeOf(t time.Time) LocalDateTime

LocalDateTimeOf is the date and the time of day of a time of Go, in its own zone.

func (LocalDateTime) String

func (t LocalDateTime) String() string

String prints the date and the time as LocalDateTime.toString() of Java: 2026-11-01T12:34:56.

type LocalTime

type LocalTime struct {
	// NanoOfDay are the nanoseconds since midnight, less than 86 400 × 10⁹.
	NanoOfDay int64
}

LocalTime is a time of day without a zone: its nanoseconds of the day.

func LocalTimeOf

func LocalTimeOf(t time.Time) LocalTime

LocalTimeOf is the time of day of a time of Go, in its own zone.

func (LocalTime) String

func (t LocalTime) String() string

String prints the time as LocalTime.toString() of Java: 12:00, 12:34:56, 12:34:56.789.

type OffsetDateTime

type OffsetDateTime struct {
	// DateTime is the date and the time at the offset.
	DateTime LocalDateTime
	// Offset is the offset from UTC in seconds, at most ±18 hours.
	Offset int32
}

OffsetDateTime is a date and a time of day with their offset from UTC.

func OffsetDateTimeOf

func OffsetDateTimeOf(t time.Time) OffsetDateTime

OffsetDateTimeOf is the date, the time and the offset of a time of Go.

func (OffsetDateTime) String

func (t OffsetDateTime) String() string

String prints it as OffsetDateTime.toString() of Java: 2026-11-01T12:34:56+03:00, 2026-11-01T00:00Z.

func (OffsetDateTime) Time

func (t OffsetDateTime) Time() time.Time

Time is the moment as a time of Go, at a fixed zone of the offset.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads the values the writer functions write, from a stream or from bytes, through a buffer of its own. The end of the data inside a value is io.ErrUnexpectedEOF; a value no writer writes is a *FormatError; a corrupt length never makes it take more memory than the data have. It is the BufferReader of the Java SDK; one goroutine reads it.

func NewReader

func NewReader(input io.Reader) *Reader

NewReader is a reader of a stream with a buffer of Size bytes.

func NewReaderSize

func NewReaderSize(input io.Reader, size int) *Reader

NewReaderSize is a reader of a stream with a buffer of its own size, at least MinSize.

func ReaderOf

func ReaderOf(bytes []byte) *Reader

ReaderOf is a reader of bytes, without a copy: the bytes must not change while it reads.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the stream, if it is an io.Closer.

func (*Reader) End

func (r *Reader) End() (bool, error)

End tells whether the data have ended, at the boundary of a value.

func (*Reader) Position

func (r *Reader) Position() int64

Position is the count of the bytes read so far.

func (*Reader) ReadBigDecimal

func (r *Reader) ReadBigDecimal() (BigDecimal, error)

ReadBigDecimal reads a decimal: its scale, then its unscaled value.

func (*Reader) ReadBigInteger

func (r *Reader) ReadBigInteger() (*big.Int, error)

ReadBigInteger reads an integer of any size; a length of 0 or a leading byte that only repeats the sign is a FormatError.

func (*Reader) ReadBoolean

func (r *Reader) ReadBoolean() (bool, error)

ReadBoolean reads a boolean: 0 or 1, anything else is a FormatError.

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByte reads a byte.

func (*Reader) ReadBytes

func (r *Reader) ReadBytes() ([]byte, error)

ReadBytes reads bytes.

func (*Reader) ReadChar

func (r *Reader) ReadChar() (uint16, error)

ReadChar reads a UTF-16 code unit.

func (*Reader) ReadDouble

func (r *Reader) ReadDouble() (float64, error)

ReadDouble reads a 64-bit float.

func (*Reader) ReadDuration

func (r *Reader) ReadDuration() (Duration, error)

ReadDuration reads an amount of time.

func (*Reader) ReadEnum

func (r *Reader) ReadEnum() (EnumConstant, error)

ReadEnum reads a constant of an enumeration: the name of its type and its own name.

func (*Reader) ReadFloat

func (r *Reader) ReadFloat() (float32, error)

ReadFloat reads a 32-bit float.

func (*Reader) ReadInstant

func (r *Reader) ReadInstant() (Instant, error)

ReadInstant reads a moment; one beyond the range of Java (years ±1 000 000 000) is a FormatError.

func (*Reader) ReadInt

func (r *Reader) ReadInt() (int32, error)

ReadInt reads a 32-bit integer.

func (*Reader) ReadLocalDate

func (r *Reader) ReadLocalDate() (LocalDate, error)

ReadLocalDate reads a date; one beyond the years ±999 999 999 is a FormatError.

func (*Reader) ReadLocalDateTime

func (r *Reader) ReadLocalDateTime() (LocalDateTime, error)

ReadLocalDateTime reads a date and a time of day.

func (*Reader) ReadLocalTime

func (r *Reader) ReadLocalTime() (LocalTime, error)

ReadLocalTime reads a time of day; one of a day or more is a FormatError.

func (*Reader) ReadLong

func (r *Reader) ReadLong() (int64, error)

ReadLong reads a 64-bit integer.

func (*Reader) ReadOffsetDateTime

func (r *Reader) ReadOffsetDateTime() (OffsetDateTime, error)

ReadOffsetDateTime reads a date and a time with their offset.

func (*Reader) ReadRaw

func (r *Reader) ReadRaw(length int) ([]byte, error)

ReadRaw reads a given number of bytes, with no number before them.

func (*Reader) ReadShort

func (r *Reader) ReadShort() (int16, error)

ReadShort reads a 16-bit integer.

func (*Reader) ReadString

func (r *Reader) ReadString() (string, error)

ReadString reads a string; bytes that are not strict UTF-8 are a FormatError.

func (*Reader) ReadULong

func (r *Reader) ReadULong() (uint64, error)

ReadULong reads an unsigned 64-bit integer.

func (*Reader) ReadUuid

func (r *Reader) ReadUuid() (uuid.UUID, error)

ReadUuid reads a UUID: 16 bytes as RFC 9562 prints them.

func (*Reader) ReadVarInt

func (r *Reader) ReadVarInt() (int32, error)

ReadVarInt reads an unsigned LEB128 of 32 bits, as an int32: at most 5 bytes, never longer than needed.

func (*Reader) ReadVarLong

func (r *Reader) ReadVarLong() (int64, error)

ReadVarLong reads an unsigned LEB128 of 64 bits, as an int64: at most 10 bytes, never longer than needed.

func (*Reader) ReadZigZagInt

func (r *Reader) ReadZigZagInt() (int32, error)

ReadZigZagInt reads a signed 32-bit integer written zigzag.

func (*Reader) ReadZigZagLong

func (r *Reader) ReadZigZagLong() (int64, error)

ReadZigZagLong reads a signed 64-bit integer written zigzag.

func (*Reader) ReadZonedDateTime

func (r *Reader) ReadZonedDateTime() (ZonedDateTime, error)

ReadZonedDateTime reads a date and a time in a zone. The zone is kept as written, known to Go or not; an identifier that no zone can have is a FormatError.

func (*Reader) Skip

func (r *Reader) Skip(count int64) error

Skip passes over a count of bytes.

type ZonedDateTime

type ZonedDateTime struct {
	// DateTime is the date and the time in the zone.
	DateTime LocalDateTime
	// Offset is the offset from UTC in seconds at that moment.
	Offset int32
	// Zone is the identifier of the zone: Europe/Moscow, UTC, +03:00.
	Zone string
}

ZonedDateTime is a date and a time of day in a zone, with the offset in effect: it tells the two equal hours apart when the clocks go back. The zone is kept as written: a zone unknown to the reader loses nothing.

func ZonedDateTimeOf

func ZonedDateTimeOf(t time.Time) ZonedDateTime

ZonedDateTimeOf is the date, the time, the offset and the zone of a time of Go. A zone without a name of the database of zones — time.Local, a fixed zone — is written as its offset.

func (ZonedDateTime) String

func (t ZonedDateTime) String() string

String prints it as ZonedDateTime.toString() of Java: 2026-11-01T01:30-04:00[America/New_York].

func (ZonedDateTime) Time

func (t ZonedDateTime) Time() time.Time

Time is the moment as a time of Go: in the zone when Go knows it, at a fixed zone of the offset otherwise.