Class BufferReader
java.lang.Object
io.kubfly.sdk.buffer.BufferReader
- All Implemented Interfaces:
AutoCloseable
Reads from an
InputStream the values ArrayBufferWriter wrote, in the same order, with the same
methods: readLong() for writeLong, readString() for writeString. The stream is read
in large chunks into a buffer of its own, so a value usually comes from memory; position() counts the bytes
read, skip(long) passes over a value that is not needed, end() tells the end of the data at a value
boundary. Not thread-safe: one reader, one thread at a time.
of(byte[], int, int) reads a part of an array in place, without a stream and without copying it — a block
already in memory: the same methods, the same strictness; the array is never changed, and the position counts from
the start of the part.
The reader is strict: what ArrayBufferWriter cannot write is a BufferFormatException — a
variable-length integer longer than its type or with more bytes than needed, bytes that are not UTF-8 (overlong, an
encoded surrogate, beyond U+10FFFF), a date, a time, an offset or nanoseconds out of range; the data ending inside a
value is an EOFException. A zone unknown to this JVM (time-zone data older than the writer's) is replaced by
the offset recorded with the value: the instant is kept.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionBufferReader(InputStream input) A reader of a stream with an own buffer of 65536 bytes.BufferReader(InputStream input, int size) A reader of a stream with an own buffer of the given size. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the stream.booleanend()Whether the data has ended: no byte is left.static BufferReaderof(byte[] bytes) A reader of a whole array, in place.static BufferReaderof(byte[] bytes, int offset, int length) A reader of a part of an array, in place: nothing is copied, the array is never changed.longposition()The number of bytes read so far: the position of the next value in the stream.Reads an exact decimal: its scale, then the unscaled value.Reads an integer of any size: its length in bytes, then two's complement, big-endian, of minimal length.booleanReads aboolean: one byte, 0 or 1.bytereadByte()Reads abyte.byte[]Reads bytes: their number, then the bytes.charreadChar()Reads achar: two bytes, little-endian.doubleReads adouble: eight bytes, IEEE 754, little-endian.Reads a duration: the seconds, then the nanoseconds added to them.readEnum()Reads an enum constant: its type, then its name.floatReads afloat: four bytes, IEEE 754, little-endian.Reads an instant: the seconds of the epoch, then the nanoseconds of the second.intreadInt()Reads anint: four bytes, little-endian.Reads a date: the day of the epoch.Reads a date and time: a date, then a time of day.Reads a time of day: the nanoseconds of the day.longreadLong()Reads along: eight bytes, little-endian.Reads a date and time with an offset: a date and time, then the offset in seconds.byte[]readRaw(int length) Reads a given number of bytes, with no number before them.shortReads ashort: two bytes, little-endian.Reads a string: its length in bytes, then strict UTF-8.longReads an unsigned 64-bit integer: the same bits asreadVarLong(), negative aboveLong.MAX_VALUE— read them as unsigned (Long.toUnsignedString(long),Long.compareUnsigned(long, long)).readUuid()Reads a UUID: 16 bytes, big-endian.intReads an unsignedintof variable length (1–5 bytes): values aboveInteger.MAX_VALUEcome back negative, as they were written.longReads an unsignedlongof variable length (1–10 bytes): values aboveLong.MAX_VALUEcome back negative, as they were written.intReads a signedintof variable length:readVarInt(), then zigzag back.longReads a signedlongof variable length:readVarLong(), then zigzag back.Reads a date and time in a zone: a date and time, the offset in seconds, then the zone identifier.voidskip(long count) Skips bytes: a value or a part of the data that is not needed.
-
Field Details
-
SIZE
public static final int SIZEThe size of the own buffer by default, in bytes.- See Also:
-
MIN_SIZE
public static final int MIN_SIZEThe fewest bytes of the own buffer: the longest fixed-width value is 16 bytes.- See Also:
-
-
Constructor Details
-
BufferReader
-
BufferReader
A reader of a stream with an own buffer of the given size.- Parameters:
input- the stream, closed byclose()size- the size of the own buffer, at least 16- Throws:
IllegalArgumentException- if the size is smaller
-
-
Method Details
-
of
A reader of a part of an array, in place: nothing is copied, the array is never changed.- Parameters:
bytes- the arrayoffset- the first bytelength- the number of bytes- Returns:
- the reader;
position()counts fromoffset - Throws:
IndexOutOfBoundsException- if the part is not inside the array
-
of
A reader of a whole array, in place.- Parameters:
bytes- the array- Returns:
- the reader
-
position
public long position()The number of bytes read so far: the position of the next value in the stream.- Returns:
- the position
-
end
Whether the data has ended: no byte is left. Reads ahead if the own buffer is empty.- Returns:
trueat the end of the stream- Throws:
IOException- if the stream fails
-
skip
Skips bytes: a value or a part of the data that is not needed.- Parameters:
count- the number of bytes- Throws:
EOFException- if the data ends soonerIOException- if the stream fails
-
readBoolean
Reads aboolean: one byte, 0 or 1.- Returns:
- the value
- Throws:
BufferFormatException- if the byte is neither 0 nor 1EOFException- if the data has endedIOException- if the stream fails
-
readByte
Reads abyte.- Returns:
- the value
- Throws:
EOFException- if the data has endedIOException- if the stream fails
-
readChar
Reads achar: two bytes, little-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readShort
Reads ashort: two bytes, little-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readInt
Reads anint: four bytes, little-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readLong
Reads along: eight bytes, little-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readULong
Reads an unsigned 64-bit integer: the same bits asreadVarLong(), negative aboveLong.MAX_VALUE— read them as unsigned (Long.toUnsignedString(long),Long.compareUnsigned(long, long)).- Returns:
- the 64 bits
- Throws:
BufferFormatException- if the variable-length long is malformedEOFException- if the data ends inside the valueIOException- if the stream fails
-
readFloat
Reads afloat: four bytes, IEEE 754, little-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readDouble
Reads adouble: eight bytes, IEEE 754, little-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readVarInt
Reads an unsignedintof variable length (1–5 bytes): values aboveInteger.MAX_VALUEcome back negative, as they were written.- Returns:
- the value, unsigned
- Throws:
BufferFormatException- if it is longer than 5 bytes, does not fit 32 bits or has more bytes than neededEOFException- if the data ends inside the valueIOException- if the stream fails
-
readVarLong
Reads an unsignedlongof variable length (1–10 bytes): values aboveLong.MAX_VALUEcome back negative, as they were written.- Returns:
- the value, unsigned
- Throws:
BufferFormatException- if it is longer than 10 bytes, does not fit 64 bits or has more bytes than neededEOFException- if the data ends inside the valueIOException- if the stream fails
-
readZigZagInt
Reads a signedintof variable length:readVarInt(), then zigzag back.- Returns:
- the value
- Throws:
BufferFormatException- if the variable-length int is malformedEOFException- if the data ends inside the valueIOException- if the stream fails
-
readZigZagLong
Reads a signedlongof variable length:readVarLong(), then zigzag back.- Returns:
- the value
- Throws:
BufferFormatException- if the variable-length long is malformedEOFException- if the data ends inside the valueIOException- if the stream fails
-
readEnum
Reads an enum constant: its type, then its name.- Returns:
- the constant
- Throws:
BufferFormatException- if a name is not UTF-8EOFException- if the data ends inside the valueIOException- if the stream fails
-
readUuid
Reads a UUID: 16 bytes, big-endian.- Returns:
- the value
- Throws:
EOFException- if the data ends inside the valueIOException- if the stream fails
-
readBigInteger
Reads an integer of any size: its length in bytes, then two's complement, big-endian, of minimal length.- Returns:
- the value
- Throws:
BufferFormatException- if the length is 0 or negative, or the bytes are longer than needed (a leading00orFFthat only repeats the sign)EOFException- if the data ends inside the valueIOException- if the stream fails
-
readBigDecimal
Reads an exact decimal: its scale, then the unscaled value.- Returns:
- the value, with its scale
- Throws:
BufferFormatException- if the unscaled value is malformedEOFException- if the data ends inside the valueIOException- if the stream fails
-
readInstant
Reads an instant: the seconds of the epoch, then the nanoseconds of the second.- Returns:
- the value
- Throws:
BufferFormatException- if it is out of the range ofInstantor the nanoseconds are not 0–999 999 999EOFException- if the data ends inside the valueIOException- if the stream fails
-
readLocalDate
Reads a date: the day of the epoch.- Returns:
- the value
- Throws:
BufferFormatException- if it is out of the range ofLocalDateEOFException- if the data ends inside the valueIOException- if the stream fails
-
readLocalTime
Reads a time of day: the nanoseconds of the day.- Returns:
- the value
- Throws:
BufferFormatException- if it is not within a dayEOFException- if the data ends inside the valueIOException- if the stream fails
-
readLocalDateTime
Reads a date and time: a date, then a time of day.- Returns:
- the value
- Throws:
BufferFormatException- if a part is out of rangeEOFException- if the data ends inside the valueIOException- if the stream fails
-
readOffsetDateTime
Reads a date and time with an offset: a date and time, then the offset in seconds.- Returns:
- the value
- Throws:
BufferFormatException- if a part is out of range (an offset beyond ±18 hours)EOFException- if the data ends inside the valueIOException- if the stream fails
-
readZonedDateTime
Reads a date and time in a zone: a date and time, the offset in seconds, then the zone identifier. The instant of the date, time and offset is kept: a zone unknown to this JVM is replaced by the offset.- Returns:
- the value
- Throws:
BufferFormatException- if a part is out of range or the zone identifier is malformedEOFException- if the data ends inside the valueIOException- if the stream fails
-
readDuration
Reads a duration: the seconds, then the nanoseconds added to them.- Returns:
- the value
- Throws:
BufferFormatException- if the nanoseconds are not 0–999 999 999EOFException- if the data ends inside the valueIOException- if the stream fails
-
readString
Reads a string: its length in bytes, then strict UTF-8.- Returns:
- the value
- Throws:
BufferFormatException- if the length is negative or the bytes are not UTF-8EOFException- if the data ends inside the valueIOException- if the stream fails
-
readBytes
Reads bytes: their number, then the bytes.- Returns:
- the bytes
- Throws:
BufferFormatException- if the number is negativeEOFException- if the data ends inside the valueIOException- if the stream fails
-
readRaw
Reads a given number of bytes, with no number before them.- Parameters:
length- the number of the bytes- Returns:
- the bytes
- Throws:
BufferFormatException- if the number is negativeEOFException- if the data ends inside themIOException- if the stream fails
-
close
Closes the stream.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- if the stream fails to close
-