Package net.minestom.server.utils.binary
Class BinaryReader
java.lang.Object
java.io.InputStream
net.minestom.server.utils.binary.BinaryReader
- All Implemented Interfaces:
Closeable,AutoCloseable
Class used to read from a byte array.
WARNING: not thread-safe.
-
Constructor Summary
ConstructorsConstructorDescriptionBinaryReader(byte[] bytes) BinaryReader(@NotNull ByteBuffer buffer) BinaryReader(@NotNull NetworkBuffer buffer) -
Method Summary
Modifier and TypeMethodDescriptionintbyte[]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.intread()<T extends Readable>
TCreates a new object from the given supplier and calls itsReadable.read(BinaryReader)method with this reader.<T extends Readable>
T[]Reads the length of the array to read as a varint, creates the array to contain the readable objects and call their respectiveReadable.read(BinaryReader)methods.booleanbytereadByte()byte[]<T> List<T> readByteList(@NotNull Function<BinaryReader, T> supplier) byte[]readBytes(int length) readComponent(int maxLength) double<L,R> Either <L, R> readEither(Function<BinaryReader, L> leftReader, Function<BinaryReader, R> rightReader) floatintreadInt()Same as readInteger, created for parity with BinaryWriterintSame as readIntlongreadLong()long[]byte[]shortreadSizedString(int maxLength) Reads a string size by a var-int.String[]String[]readSizedStringArray(int maxLength) org.jglrxavpok.hephaistos.nbt.NBTreadTag()intreadUuid()intint[]<T> List<T> readVarIntList(@NotNull Function<BinaryReader, T> supplier) longlong[]Methods inherited from class java.io.InputStream
close, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
BinaryReader
-
BinaryReader
-
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() -
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
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 thanmaxLength
-
readSizedString
-
readBytes
public byte[] readBytes(int length) -
readByteArray
public byte[] readByteArray() -
readSizedStringArray
-
readSizedStringArray
-
readVarIntArray
public int[] readVarIntArray() -
readVarLongArray
public long[] readVarLongArray() -
readLongArray
public long[] readLongArray() -
readRemainingBytes
public byte[] readRemainingBytes() -
readBlockPosition
-
readUuid
-
readItemStack
-
readComponent
-
readComponent
-
read
Creates a new object from the given supplier and calls itsReadable.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
Reads the length of the array to read as a varint, creates the array to contain the readable objects and call their respectiveReadable.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
-
readByteList
-
readEither
public <L,R> Either<L,R> readEither(Function<BinaryReader, L> leftReader, Function<BinaryReader, R> rightReader) -
read
public int read()- Specified by:
readin classInputStream
-
available
public int available()- Overrides:
availablein classInputStream
-
readTag
public org.jglrxavpok.hephaistos.nbt.NBT readTag() -
extractBytes
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.
-