Class BufferReader

java.lang.Object
io.kubfly.sdk.buffer.BufferReader
All Implemented Interfaces:
AutoCloseable

public final class BufferReader extends Object implements 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 Details

    • SIZE

      public static final int SIZE
      The size of the own buffer by default, in bytes.
      See Also:
    • MIN_SIZE

      public static final int MIN_SIZE
      The fewest bytes of the own buffer: the longest fixed-width value is 16 bytes.
      See Also:
  • Constructor Details

    • BufferReader

      public BufferReader(InputStream input)
      A reader of a stream with an own buffer of 65536 bytes.
      Parameters:
      input - the stream, closed by close()
    • BufferReader

      public BufferReader(InputStream input, int size)
      A reader of a stream with an own buffer of the given size.
      Parameters:
      input - the stream, closed by close()
      size - the size of the own buffer, at least 16
      Throws:
      IllegalArgumentException - if the size is smaller
  • Method Details

    • of

      public static BufferReader of(byte[] bytes, int offset, int length)
      A reader of a part of an array, in place: nothing is copied, the array is never changed.
      Parameters:
      bytes - the array
      offset - the first byte
      length - the number of bytes
      Returns:
      the reader; position() counts from offset
      Throws:
      IndexOutOfBoundsException - if the part is not inside the array
    • of

      public static BufferReader of(byte[] bytes)
      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

      public boolean end() throws IOException
      Whether the data has ended: no byte is left. Reads ahead if the own buffer is empty.
      Returns:
      true at the end of the stream
      Throws:
      IOException - if the stream fails
    • skip

      public void skip(long count) throws IOException
      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 sooner
      IOException - if the stream fails
    • readBoolean

      public boolean readBoolean() throws IOException
      Reads a boolean: one byte, 0 or 1.
      Returns:
      the value
      Throws:
      BufferFormatException - if the byte is neither 0 nor 1
      EOFException - if the data has ended
      IOException - if the stream fails
    • readByte

      public byte readByte() throws IOException
      Reads a byte.
      Returns:
      the value
      Throws:
      EOFException - if the data has ended
      IOException - if the stream fails
    • readChar

      public char readChar() throws IOException
      Reads a char: two bytes, little-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readShort

      public short readShort() throws IOException
      Reads a short: two bytes, little-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readInt

      public int readInt() throws IOException
      Reads an int: four bytes, little-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readLong

      public long readLong() throws IOException
      Reads a long: eight bytes, little-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readULong

      public long readULong() throws IOException
      Reads an unsigned 64-bit integer: the same bits as readVarLong(), negative above Long.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 malformed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readFloat

      public float readFloat() throws IOException
      Reads a float: four bytes, IEEE 754, little-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readDouble

      public double readDouble() throws IOException
      Reads a double: eight bytes, IEEE 754, little-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readVarInt

      public int readVarInt() throws IOException
      Reads an unsigned int of variable length (1–5 bytes): values above Integer.MAX_VALUE come 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 needed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readVarLong

      public long readVarLong() throws IOException
      Reads an unsigned long of variable length (1–10 bytes): values above Long.MAX_VALUE come 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 needed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readZigZagInt

      public int readZigZagInt() throws IOException
      Reads a signed int of variable length: readVarInt(), then zigzag back.
      Returns:
      the value
      Throws:
      BufferFormatException - if the variable-length int is malformed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readZigZagLong

      public long readZigZagLong() throws IOException
      Reads a signed long of variable length: readVarLong(), then zigzag back.
      Returns:
      the value
      Throws:
      BufferFormatException - if the variable-length long is malformed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readEnum

      public EnumConstant readEnum() throws IOException
      Reads an enum constant: its type, then its name.
      Returns:
      the constant
      Throws:
      BufferFormatException - if a name is not UTF-8
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readUuid

      public UUID readUuid() throws IOException
      Reads a UUID: 16 bytes, big-endian.
      Returns:
      the value
      Throws:
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readBigInteger

      public BigInteger readBigInteger() throws IOException
      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 leading 00 or FF that only repeats the sign)
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readBigDecimal

      public BigDecimal readBigDecimal() throws IOException
      Reads an exact decimal: its scale, then the unscaled value.
      Returns:
      the value, with its scale
      Throws:
      BufferFormatException - if the unscaled value is malformed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readInstant

      public Instant readInstant() throws IOException
      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 of Instant or the nanoseconds are not 0–999 999 999
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readLocalDate

      public LocalDate readLocalDate() throws IOException
      Reads a date: the day of the epoch.
      Returns:
      the value
      Throws:
      BufferFormatException - if it is out of the range of LocalDate
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readLocalTime

      public LocalTime readLocalTime() throws IOException
      Reads a time of day: the nanoseconds of the day.
      Returns:
      the value
      Throws:
      BufferFormatException - if it is not within a day
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readLocalDateTime

      public LocalDateTime readLocalDateTime() throws IOException
      Reads a date and time: a date, then a time of day.
      Returns:
      the value
      Throws:
      BufferFormatException - if a part is out of range
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readOffsetDateTime

      public OffsetDateTime readOffsetDateTime() throws IOException
      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 value
      IOException - if the stream fails
    • readZonedDateTime

      public ZonedDateTime readZonedDateTime() throws IOException
      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 malformed
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readDuration

      public Duration readDuration() throws IOException
      Reads a duration: the seconds, then the nanoseconds added to them.
      Returns:
      the value
      Throws:
      BufferFormatException - if the nanoseconds are not 0–999 999 999
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readString

      public String readString() throws IOException
      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-8
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readBytes

      public byte[] readBytes() throws IOException
      Reads bytes: their number, then the bytes.
      Returns:
      the bytes
      Throws:
      BufferFormatException - if the number is negative
      EOFException - if the data ends inside the value
      IOException - if the stream fails
    • readRaw

      public byte[] readRaw(int length) throws IOException
      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 negative
      EOFException - if the data ends inside them
      IOException - if the stream fails
    • close

      public void close() throws IOException
      Closes the stream.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException - if the stream fails to close