Class BinaryReader

All Implemented Interfaces:
Closeable, AutoCloseable

public class BinaryReader extends InputStream
Class used to read from a byte array.

WARNING: not thread-safe.

  • Constructor Details

    • BinaryReader

      public BinaryReader(@NotNull @NotNull ByteBuffer buffer)
    • BinaryReader

      public BinaryReader(byte[] bytes)
  • Method Details

    • readVarInt

      public int readVarInt()
    • readVarLong

      public long readVarLong()
    • readBoolean

      public boolean readBoolean()
    • readByte

      public byte readByte()
    • readShort

      public short readShort()
    • readChar

      public char readChar()
    • readUnsignedShort

      public int readUnsignedShort()
    • readInteger

      public int readInteger()
      Same as readInt
    • readInt

      public int readInt()
      Same as readInteger, created for parity with BinaryWriter
    • readLong

      public long readLong()
    • readFloat

      public float readFloat()
    • readDouble

      public double readDouble()
    • readSizedString

      public String readSizedString(int maxLength)
      Reads a string size by a var-int.

      If the string length is higher than maxLength, the code throws an exception and the string bytes are not read.

      maxLength - the max length of the string
      the string
      IllegalStateException - if the string length is invalid or higher than maxLength
    • readSizedString

      public String readSizedString()
    • readBytes

      public byte[] readBytes(int length)
    • readSizedStringArray

      public String[] readSizedStringArray(int maxLength)
    • readSizedStringArray

      public String[] readSizedStringArray()
    • readVarIntArray

      public int[] readVarIntArray()
    • readVarLongArray

      public long[] readVarLongArray()
    • readLongArray

      public long[] readLongArray()
    • readRemainingBytes

      public byte[] readRemainingBytes()
    • readBlockPosition

      public Point readBlockPosition()
    • readUuid

      public UUID readUuid()
    • readItemStack

      public ItemStack readItemStack()
      Tries to read an ItemStack.
      the read item
      NullPointerException - if the item could not get read
    • readComponent

      public Component readComponent(int maxLength)
    • readComponent

      public Component readComponent()
    • read

      public <T extends Readable> T read(@NotNull @NotNull Supplier<@NotNull T> supplier)
      Creates a new object from the given supplier and calls its method with this reader.
      Type Parameters:
      T - the readable object type
      supplier - supplier to create new instances of your object
      the read object
    • readArray

      @NotNull public <T extends Readable> @NotNull T[] readArray(@NotNull @NotNull Supplier<@NotNull T> supplier)
      Reads the length of the array to read as a varint, creates the array to contain the readable objects and call their respective methods.
      Type Parameters:
      T - the readable object type
      supplier - supplier to create new instances of your object
      the read objects
    • readVarIntList

      public <T> List<T> readVarIntList(@NotNull @NotNull Function<BinaryReader,T> supplier)
    • readByteList

      public <T> List<T> readByteList(@NotNull @NotNull Function<BinaryReader,T> supplier)
    • getBuffer

      public ByteBuffer getBuffer()
    • read

      public int read()
      Specified by:
      read in class InputStream
    • available

      public int available()
      available in class InputStream
    • readTag

      public org.jglrxavpok.hephaistos.nbt.NBT readTag()
    • extractBytes

      public byte[] extractBytes(Runnable extractor)
      Records the current position, runs the given Runnable, and then returns the bytes between the position before running the runnable and the position after. Can be used to extract a subsection of this reader's buffer with complex data
      extractor - the extraction code, simply call the reader's read* methods here.