Class BinaryReader

java.lang.Object
java.io.InputStream
net.minestom.server.utils.binary.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.

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

      public String readSizedString()
    • readBytes

      public byte[] readBytes(int length)
    • readByteArray

      public byte[] readByteArray()
    • 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.
      Returns:
      the read item
      Throws:
      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 Readable.read(BinaryReader) method with this reader.
      Type Parameters:
      T - the readable object type
      Parameters:
      supplier - supplier to create new instances of your object
      Returns:
      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 Readable.read(BinaryReader) methods.
      Type Parameters:
      T - the readable object type
      Parameters:
      supplier - supplier to create new instances of your object
      Returns:
      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()
      Overrides:
      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
      Parameters:
      extractor - the extraction code, simply call the reader's read* methods here.