CommandAPI 12.1.0
An API for the command UI introduced in Minecraft 1.13
Loading...
Searching...
No Matches
dev.jorel.commandapi.network.FriendlyByteBuffer Class Reference

A sequence of bytes that can be written to and read from. More...

Public Member Functions

 FriendlyByteBuffer ()
 Creates a new FriendlyByteBuffer with no bytes, ready to be written to.
 FriendlyByteBuffer (byte[] bytes)
 Creates a new FriendlyByteBuffer with the given bytes already written, ready to be read from.
void checkReadIndexIsInBounds () throws IllegalStateException
 Checks if the current readIndex for this buffer is out of bounds.
void checkReadIndexIsInBounds (int readIndex) throws IllegalStateException
 Checks if the given readIndex is out of bounds.
boolean isReadIndexOutOfBounds ()
 Evaluates if the current readIndex for this buffer is out of bounds.
boolean isReadIndexOutOfBounds (int readIndex)
 Evaluates if the given readIndex is out of bounds.
void checkWriteIndexIsInBounds () throws IllegalStateException
 Checks if the current writeIndex for this buffer is out of bounds.
void checkWriteIndexIsInBounds (int writeIndex) throws IllegalStateException
 Checks if the given writeIndex is out of bounds.
boolean isWriteIndexOutOfBounds ()
 Evaluates if the current writeIndex for this buffer is out of bounds.
boolean isWriteIndexOutOfBounds (int writeIndex)
 Evaluates if the given writeIndex is out of bounds.
int getReadIndex ()
void setReadIndex (int index) throws IllegalStateException
 Sets the readIndex for this buffer.
int getWriteIndex ()
void setWriteIndex (int index) throws IllegalStateException
 Sets the writeIndex for this buffer.
void resetIndices ()
 Sets the readIndex and writeIndex to 0.
byte[] toByteArray () throws IllegalStateException
byte[] getRemainingBytes () throws IllegalStateException
int countTotalBytes ()
int countReadableBytes ()
void writeByte (byte b) throws IllegalStateException
 Writes a single byte (8 bits) into this buffer.
void writeByte (int i) throws IllegalStateException
 Writes a single byte (8 bits) into this buffer using writeByte(byte).
byte readByte () throws IllegalStateException
 Reads a single byte (8 bits) from this buffer.
void writeBytes (byte... bytes) throws IllegalStateException
 Writes multiple bytes to this buffer.
byte[] readBytes (int n) throws IllegalStateException
 Reads n bytes from this buffer.
void writeInt (int i) throws IllegalStateException
 Writes a 32 bit int to this buffer.
int readInt () throws IllegalStateException
 Reads a 32 bit int from this buffer.
void writeVarInt (int value) throws IllegalStateException
 Writes a 32 bit int to this buffer using between 1 and 5 bytes, with small positive numbers using fewer bytes.
int readVarInt () throws IllegalStateException
 Reads a 32 bit int from this buffer, encoded as a VarInt.
void writeString (String string, int maxLength)
 Writes a String to this buffer.
String readString (int maxLength) throws IllegalStateException
 Reads a String from this buffer.

Detailed Description

A sequence of bytes that can be written to and read from.

Reading and writing is handled by two ints, the readIndex and writeIndex, which can be accessed using getReadIndex() and getWriteIndex() respectively. These indices are automatically incremented when reading and writing bytes. If the readIndex is ever greater than or equal to the writeIndex when a byte is read, an IllegalStateException will be thrown for attempting to read out of bounds.

This class provides methods that make it easier to read and write certain data types, using methods like readVarInt() and writeVarInt(int). These methods handle formatting the bytes correctly, letting developers simply worry about the structure of their data. If a specific method has a problem while reading bytes, an appropriate IllegalStateException will be thrown to indicate something is misformatted.

This buffer can be created empty (FriendlyByteBuffer()), or pre-written with a byte array (FriendlyByteBuffer(byte[])).

Constructor & Destructor Documentation

◆ FriendlyByteBuffer()

dev.jorel.commandapi.network.FriendlyByteBuffer.FriendlyByteBuffer ( byte[] bytes)

Creates a new FriendlyByteBuffer with the given bytes already written, ready to be read from.

Parameters
bytesThe initial bytes to put in this buffer. This array is written to the buffer using writeBytes(byte...).

Member Function Documentation

◆ checkReadIndexIsInBounds() [1/2]

void dev.jorel.commandapi.network.FriendlyByteBuffer.checkReadIndexIsInBounds ( ) throws IllegalStateException

Checks if the current readIndex for this buffer is out of bounds.

Exceptions
IllegalStateExceptionIf isReadIndexOutOfBounds() returns true.

◆ checkReadIndexIsInBounds() [2/2]

void dev.jorel.commandapi.network.FriendlyByteBuffer.checkReadIndexIsInBounds ( int readIndex) throws IllegalStateException

Checks if the given readIndex is out of bounds.

Parameters
readIndexThe index to check.
Exceptions
IllegalStateExceptionIf isReadIndexOutOfBounds(int) returns true for the given readIndex.

◆ checkWriteIndexIsInBounds() [1/2]

void dev.jorel.commandapi.network.FriendlyByteBuffer.checkWriteIndexIsInBounds ( ) throws IllegalStateException

Checks if the current writeIndex for this buffer is out of bounds.

Exceptions
IllegalStateExceptionIf isWriteIndexOutOfBounds() returns true.

◆ checkWriteIndexIsInBounds() [2/2]

void dev.jorel.commandapi.network.FriendlyByteBuffer.checkWriteIndexIsInBounds ( int writeIndex) throws IllegalStateException

Checks if the given writeIndex is out of bounds.

Parameters
writeIndexThe index to check.
Exceptions
IllegalStateExceptionIf isWriteIndexOutOfBounds(int) returns true for the given writeIndex.

◆ countReadableBytes()

int dev.jorel.commandapi.network.FriendlyByteBuffer.countReadableBytes ( )
Returns
The number of bytes left to read fom this buffer.

◆ countTotalBytes()

int dev.jorel.commandapi.network.FriendlyByteBuffer.countTotalBytes ( )
Returns
The number of bytes currently written to this buffer.

◆ getReadIndex()

int dev.jorel.commandapi.network.FriendlyByteBuffer.getReadIndex ( )
Returns
The current readIndex for this buffer.

◆ getRemainingBytes()

byte[] dev.jorel.commandapi.network.FriendlyByteBuffer.getRemainingBytes ( ) throws IllegalStateException
Returns
An array containing all the bytes left to be read from this buffer.
Exceptions
IllegalStateExceptionIf the current writeIndex is out of bounds according to checkWriteIndexIsInBounds(), or if the current readIndex is out of bounds according to checkReadIndexIsInBounds().

◆ getWriteIndex()

int dev.jorel.commandapi.network.FriendlyByteBuffer.getWriteIndex ( )
Returns
The current writeIndex for this buffer.

◆ isReadIndexOutOfBounds() [1/2]

boolean dev.jorel.commandapi.network.FriendlyByteBuffer.isReadIndexOutOfBounds ( )

Evaluates if the current readIndex for this buffer is out of bounds.

Returns
True if this.readIndex < 0 || this.readIndex >= this.writeIndex, and false otherwise.

◆ isReadIndexOutOfBounds() [2/2]

boolean dev.jorel.commandapi.network.FriendlyByteBuffer.isReadIndexOutOfBounds ( int readIndex)

Evaluates if the given readIndex is out of bounds.

Parameters
readIndexThe index to check.
Returns
True if readIndex < 0 || readIndex >= this.writeIndex, and false otherwise.

◆ isWriteIndexOutOfBounds() [1/2]

boolean dev.jorel.commandapi.network.FriendlyByteBuffer.isWriteIndexOutOfBounds ( )

Evaluates if the current writeIndex for this buffer is out of bounds.

Returns
True if this.writeIndex < 0, and false otherwise.

◆ isWriteIndexOutOfBounds() [2/2]

boolean dev.jorel.commandapi.network.FriendlyByteBuffer.isWriteIndexOutOfBounds ( int writeIndex)

Evaluates if the given writeIndex is out of bounds.

Parameters
writeIndexThe index to check.
Returns
True if writeIndex < 0, and false otherwise.

◆ readByte()

byte dev.jorel.commandapi.network.FriendlyByteBuffer.readByte ( ) throws IllegalStateException

Reads a single byte (8 bits) from this buffer.

Returns
The next byte in this buffer.
Exceptions
IllegalStateExceptionIf the readIndex is out of bounds according to checkReadIndexIsInBounds().

◆ readBytes()

byte[] dev.jorel.commandapi.network.FriendlyByteBuffer.readBytes ( int n) throws IllegalStateException

Reads n bytes from this buffer.

This happens one at a time using readByte().

Parameters
nThe number of bytes to read.
Returns
An array of the next n bytes in this buffer.
Exceptions
IllegalStateExceptionIf the read index goes out of bounds while reading bytes.

◆ readInt()

int dev.jorel.commandapi.network.FriendlyByteBuffer.readInt ( ) throws IllegalStateException

Reads a 32 bit int from this buffer.

This method assumes the int was written by writeInt(int).

Returns
The int read from this buffer.
Exceptions
IllegalStateExceptionIf the read index goes out of bounds while reading bytes.

◆ readString()

String dev.jorel.commandapi.network.FriendlyByteBuffer.readString ( int maxLength) throws IllegalStateException

Reads a String from this buffer.

This method assumes the String was written by writeString(String, int).

Parameters
maxLengthThe length of the longest String that is allowed.
Returns
The String read from this buffer.
Exceptions
IllegalStateExceptionIf the read String is longer than the given maximum length.

◆ readVarInt()

int dev.jorel.commandapi.network.FriendlyByteBuffer.readVarInt ( ) throws IllegalStateException

Reads a 32 bit int from this buffer, encoded as a VarInt.

This method assumes the VarInt was written by writeVarInt(int).

Returns
The int read from this buffer.
Exceptions
IllegalStateExceptionIf the read index goes out of bounds while reading bytes or if the VarInt is improperly formatted.

◆ resetIndices()

void dev.jorel.commandapi.network.FriendlyByteBuffer.resetIndices ( )

Sets the readIndex and writeIndex to 0.

This effectively clears this buffer, since future writes will overwrite the old bytes. The bytes are not actually forgotten, since if the write index is jumped forward, the bytes can still be read.

◆ setReadIndex()

void dev.jorel.commandapi.network.FriendlyByteBuffer.setReadIndex ( int index) throws IllegalStateException

Sets the readIndex for this buffer.

This method checks the given index using isReadIndexOutOfBounds(int) to make sure it is not out of bounds before setting the index of this buffer.

Parameters
indexThe new readIndex.
Exceptions
IllegalStateExceptionIf the given index is out of bounds.

◆ setWriteIndex()

void dev.jorel.commandapi.network.FriendlyByteBuffer.setWriteIndex ( int index) throws IllegalStateException

Sets the writeIndex for this buffer.

This method checks the given index using isWriteIndexOutOfBounds(int) to make sure it is not out of bounds before setting the index of this buffer.

Parameters
indexThe new writeIndex.
Exceptions
IllegalStateExceptionIf the given index is out of bounds.

◆ toByteArray()

byte[] dev.jorel.commandapi.network.FriendlyByteBuffer.toByteArray ( ) throws IllegalStateException
Returns
An array containing all the bytes written to this buffer.
Exceptions
IllegalStateExceptionIf the current writeIndex is out of bounds according to checkWriteIndexIsInBounds().

◆ writeByte() [1/2]

void dev.jorel.commandapi.network.FriendlyByteBuffer.writeByte ( byte b) throws IllegalStateException

Writes a single byte (8 bits) into this buffer.

Parameters
bThe byte to write.
Exceptions
IllegalStateExceptionIf the writeIndex is out of bounds according to checkWriteIndexIsInBounds().

◆ writeByte() [2/2]

void dev.jorel.commandapi.network.FriendlyByteBuffer.writeByte ( int i) throws IllegalStateException

Writes a single byte (8 bits) into this buffer using writeByte(byte).

Parameters
iThe byte to write, given as a 32-bit integer. Only the 8 least significant bits are written.
Exceptions
IllegalStateExceptionIf the write index goes out of bounds while writing the byte.

◆ writeBytes()

void dev.jorel.commandapi.network.FriendlyByteBuffer.writeBytes ( byte... bytes) throws IllegalStateException

Writes multiple bytes to this buffer.

This happens one at a time using writeByte(byte).

Parameters
bytesAn array of bytes to write.
Exceptions
IllegalStateExceptionIf the write index goes out of bounds while writing bytes.

◆ writeInt()

void dev.jorel.commandapi.network.FriendlyByteBuffer.writeInt ( int i) throws IllegalStateException

Writes a 32 bit int to this buffer.

This method writes the int as 4 bytes, starting with the 8 most significant bits.

Parameters
iThe int to write.
Exceptions
IllegalStateExceptionIf the write index goes out of bounds while writing bytes.

◆ writeString()

void dev.jorel.commandapi.network.FriendlyByteBuffer.writeString ( String string,
int maxLength )

Writes a String to this buffer.

The String is encoded as an array of bytes using the UTF-8 encoding. For the details of this encoding, see UTF-8 - Wikipedia

Parameters
stringThe String to write.
maxLengthThe length of the longest String that is allowed.
Exceptions
IllegalStateExceptionIf the given String is longer than the given maximum length.

◆ writeVarInt()

void dev.jorel.commandapi.network.FriendlyByteBuffer.writeVarInt ( int value) throws IllegalStateException

Writes a 32 bit int to this buffer using between 1 and 5 bytes, with small positive numbers using fewer bytes.

For the details of this encoding, see VarInt and VarLong.

Parameters
valueThe int to write.
Exceptions
IllegalStateExceptionIf the write index goes out of bounds while writing bytes.