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
ConstructorDescriptionBinaryReader
(byte[] bytes) BinaryReader
(@NotNull ByteBuffer buffer) BinaryReader
(@NotNull NetworkBuffer buffer) -
Method Summary
Modifier and TypeMethodDescriptionint
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.int
read()
<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.boolean
byte
readByte()
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) float
int
readInt()
Same as readInteger, created for parity with BinaryWriterint
Same as readIntlong
readLong()
long[]
byte[]
short
readSizedString
(int maxLength) Reads a string size by a var-int.String[]
String[]
readSizedStringArray
(int maxLength) org.jglrxavpok.hephaistos.nbt.NBT
readTag()
int
readUuid()
int
int[]
<T> List
<T> readVarIntList
(@NotNull Function<BinaryReader, T> supplier) long
long[]
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:
read
in classInputStream
-
available
public int available()- Overrides:
available
in 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.
-