Class FriendlyByteBuffer
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 Summary
ConstructorsConstructorDescriptionCreates a newFriendlyByteBufferwith no bytes, ready to be written to.FriendlyByteBuffer(byte[] bytes) Creates a newFriendlyByteBufferwith the given bytes already written, ready to be read from. -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks if the currentreadIndexfor this buffer is out of bounds.voidcheckReadIndexIsInBounds(int readIndex) Checks if the givenreadIndexis out of bounds.voidChecks if the currentwriteIndexfor this buffer is out of bounds.voidcheckWriteIndexIsInBounds(int writeIndex) Checks if the givenwriteIndexis out of bounds.intintintbyte[]intbooleanEvaluates if the currentreadIndexfor this buffer is out of bounds.booleanisReadIndexOutOfBounds(int readIndex) Evaluates if the givenreadIndexis out of bounds.booleanEvaluates if the currentwriteIndexfor this buffer is out of bounds.booleanisWriteIndexOutOfBounds(int writeIndex) Evaluates if the givenwriteIndexis out of bounds.bytereadByte()Reads a single byte (8 bits) from this buffer.byte[]readBytes(int n) Readsnbytes from this buffer.intreadInt()Reads a 32 bit int from this buffer.Reads a String from this buffer.intReads a 32 bit int from this buffer, encoded as a VarInt.voidSets thereadIndexandwriteIndexto 0.voidsetReadIndex(int index) Sets thereadIndexfor this buffer.voidsetWriteIndex(int index) Sets thewriteIndexfor this buffer.byte[]voidwriteByte(byte b) Writes a single byte (8 bits) into this buffer.voidwriteByte(int i) Writes a single byte (8 bits) into this buffer usingwriteByte(byte).voidwriteBytes(byte... bytes) Writes multiple bytes to this buffer.voidwriteInt(int i) Writes a 32 bit int to this buffer.voidwriteString(String string) Writes a String to this buffer.voidwriteVarInt(int value) Writes a 32 bit int to this buffer using between 1 and 5 bytes, with small positive numbers using fewer bytes.
-
Constructor Details
-
FriendlyByteBuffer
public FriendlyByteBuffer()Creates a newFriendlyByteBufferwith no bytes, ready to be written to. -
FriendlyByteBuffer
public FriendlyByteBuffer(byte[] bytes) Creates a newFriendlyByteBufferwith the given bytes already written, ready to be read from.- Parameters:
bytes- The initial bytes to put in this buffer. This array is written to the buffer usingwriteBytes(byte...).
-
-
Method Details
-
checkReadIndexIsInBounds
Checks if the currentreadIndexfor this buffer is out of bounds.- Throws:
IllegalStateException- IfisReadIndexOutOfBounds()returns true.
-
checkReadIndexIsInBounds
Checks if the givenreadIndexis out of bounds.- Parameters:
readIndex- The index to check.- Throws:
IllegalStateException- IfisReadIndexOutOfBounds(int)returns true for the givenreadIndex.
-
isReadIndexOutOfBounds
public boolean isReadIndexOutOfBounds()Evaluates if the currentreadIndexfor this buffer is out of bounds.- Returns:
- True if
this.readIndex < 0 || this.readIndex >= this.writeIndex, and false otherwise.
-
isReadIndexOutOfBounds
public boolean isReadIndexOutOfBounds(int readIndex) Evaluates if the givenreadIndexis out of bounds.- Parameters:
readIndex- The index to check.- Returns:
- True if
readIndex < 0 || readIndex >= this.writeIndex, and false otherwise.
-
checkWriteIndexIsInBounds
Checks if the currentwriteIndexfor this buffer is out of bounds.- Throws:
IllegalStateException- IfisWriteIndexOutOfBounds()returns true.
-
checkWriteIndexIsInBounds
Checks if the givenwriteIndexis out of bounds.- Parameters:
writeIndex- The index to check.- Throws:
IllegalStateException- IfisWriteIndexOutOfBounds(int)returns true for the givenwriteIndex.
-
isWriteIndexOutOfBounds
public boolean isWriteIndexOutOfBounds()Evaluates if the currentwriteIndexfor this buffer is out of bounds.- Returns:
- True if
this.writeIndex < 0, and false otherwise.
-
isWriteIndexOutOfBounds
public boolean isWriteIndexOutOfBounds(int writeIndex) Evaluates if the givenwriteIndexis out of bounds.- Parameters:
writeIndex- The index to check.- Returns:
- True if
writeIndex < 0, and false otherwise.
-
getReadIndex
public int getReadIndex()- Returns:
- The current
readIndexfor this buffer.
-
setReadIndex
Sets thereadIndexfor 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:
index- The newreadIndex.- Throws:
IllegalStateException- If the given index is out of bounds.
-
getWriteIndex
public int getWriteIndex()- Returns:
- The current
writeIndexfor this buffer.
-
setWriteIndex
Sets thewriteIndexfor 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:
index- The newwriteIndex.- Throws:
IllegalStateException- If the given index is out of bounds.
-
resetIndices
public void resetIndices()Sets thereadIndexandwriteIndexto 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. -
toByteArray
- Returns:
- An array containing all the bytes written to this buffer.
- Throws:
IllegalStateException- If the currentwriteIndexis out of bounds according tocheckWriteIndexIsInBounds().
-
getRemainingBytes
- Returns:
- An array containing all the bytes left to be read from this buffer.
- Throws:
IllegalStateException- If the currentwriteIndexis out of bounds according tocheckWriteIndexIsInBounds(), or if the currentreadIndexis out of bounds according tocheckReadIndexIsInBounds().
-
countTotalBytes
public int countTotalBytes()- Returns:
- The number of bytes currently written to this buffer.
-
countReadableBytes
public int countReadableBytes()- Returns:
- The number of bytes left to read fom this buffer.
-
writeByte
Writes a single byte (8 bits) into this buffer.- Parameters:
b- The byte to write.- Throws:
IllegalStateException- If thewriteIndexis out of bounds according tocheckWriteIndexIsInBounds().
-
writeByte
Writes a single byte (8 bits) into this buffer usingwriteByte(byte).- Parameters:
i- The byte to write, given as a 32-bit integer. Only the 8 least significant bits are written.- Throws:
IllegalStateException- If the write index goes out of bounds while writing the byte.
-
readByte
Reads a single byte (8 bits) from this buffer.- Returns:
- The next byte in this buffer.
- Throws:
IllegalStateException- If thereadIndexis out of bounds according tocheckReadIndexIsInBounds().
-
writeBytes
Writes multiple bytes to this buffer. This happens one at a time usingwriteByte(byte).- Parameters:
bytes- An array of bytes to write.- Throws:
IllegalStateException- If the write index goes out of bounds while writing bytes.
-
readBytes
Readsnbytes from this buffer. This happens one at a time usingreadByte().- Parameters:
n- The number of bytes to read.- Returns:
- An array of the next
nbytes in this buffer. - Throws:
IllegalStateException- If the read index goes out of bounds while reading bytes.
-
writeInt
Writes a 32 bit int to this buffer. This method writes the int as 4 bytes, starting with the 8 most significant bits.- Parameters:
i- The int to write.- Throws:
IllegalStateException- If the write index goes out of bounds while writing bytes.
-
readInt
Reads a 32 bit int from this buffer. This method assumes the int was written bywriteInt(int).- Returns:
- The int read from this buffer.
- Throws:
IllegalStateException- If the read index goes out of bounds while reading bytes.
-
writeVarInt
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:
value- The int to write.- Throws:
IllegalStateException- If the write index goes out of bounds while writing bytes.
-
readVarInt
Reads a 32 bit int from this buffer, encoded as a VarInt. This method assumes the VarInt was written bywriteVarInt(int).- Returns:
- The int read from this buffer.
- Throws:
IllegalStateException- If the read index goes out of bounds while reading bytes or if the VarInt is improperly formatted.
-
writeString
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:
string- The String to write.
-
readString
Reads a String from this buffer. This method assumes the String was written bywriteString(String).- Returns:
- The String read from this buffer.
- Throws:
IllegalStateException- If the read index goes out of bounds while reading bytes.
-