Package de.craftsblock.craftsnet.utils
Class ByteBuffer
java.lang.Object
de.craftsblock.craftsnet.utils.ByteBuffer
A custom
ByteBuffer implementation that provides both read and write operations
for byte arrays. It supports features such as fixed-size buffers, read-only buffers,
and the ability to read/write various data types including integers, floating point values,
strings, and UUIDs.
This class also allows the marking and resetting of read and write positions, and can handle both little-endian and big-endian data representation through shifted reads and writes.
Implements AutoCloseable to support automatic resource management.
- Since:
- 3.1.0-SNAPSHOT
-
Constructor Summary
ConstructorsConstructorDescriptionByteBuffer(byte[] source) Constructs a newByteBufferfrom an existing byte array.ByteBuffer(byte[] source, boolean readOnly) Constructs a newByteBufferfrom an existing byte array with the option to set the buffer as read-only.ByteBuffer(byte[] source, boolean readOnly, boolean fixedSize) Constructs a newByteBufferfrom an existing byte array with options for read-only and fixed size.ByteBuffer(int initialSize) Constructs a newByteBufferwith the given initial size.ByteBuffer(int initialSize, boolean fixedSize) Constructs a newByteBufferwith the given initial size and the option to set a fixed size.ByteBuffer(int initialSize, boolean readOnly, boolean fixedSize) Constructs a newByteBufferwith the given initial size, read-only, and fixed-size flags. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Returns the underlying byte array used by the buffer.booleanChecks if the buffer has a fixed size.booleanChecks if there are any readable bytes remaining in the buffer.booleanisReadable(int length) Checks if the buffer has enough readable bytes for a specified length.booleanChecks if the buffer is read-only.booleanChecks if the buffer is writable (i.e., there is space to write).booleanisWriteable(int length) Checks if the buffer has enough space to write the specified number of bytes.voidMarks the current reader index, so it can be restored later.voidMarks the current writer index, so it can be restored later.intReturns the number of readable bytes left in the buffer.booleanReads a boolean value from the buffer.bytereadByte()Reads a single byte from the buffer.charreadChar()Reads a single character from the buffer.readCharSequence(int length, Charset charset) Reads a sequence of characters from the buffer using the specifiedCharset.doubleReads a double value (8 bytes) from the buffer.<T extends Enum<?>>
TReads an enum constant from the buffer by ordinal value.intReturns the current reader index of the buffer.voidreaderIndex(int readerIndex) Sets the reader index to the specified value.floatReads a floating-point value (4 bytes) from the buffer.intreadInt()Reads an integer (4 bytes) from the buffer.longreadLong()Reads a long (8 bytes) from the buffer.byte[]readNBytes(int length) Reads the specified number of bytes from the buffer and advances the reader index.byte[]Reads all remaining bytes from the current reader index.intreadShifted(int length, int shift) Reads a shifted integer from the buffer by combining bytes.shortReads a short (2 bytes) from the buffer.intReads an unsigned short (2 bytes) from the buffer.readUTF()Reads a UTF-8 encoded string from the buffer.readUTF(int length) Reads a UTF-8 encoded string of the given length from the buffer.readUUID()Reads aUUIDfrom the buffer, which consists of two long values (most significant bits and least significant bits).intReads a variable-length integer from the buffer.longReads a variable-length long from the buffer.voidResets the reader index to the previously marked index.voidResets the writer index to the previously marked index.intsize()Returns the size of the buffer.voidwrite(byte[] data) Writes the specified byte array to the buffer at the current writer index.intReturns the number of writable bytes remaining in the buffer.voidwriteBoolean(boolean value) Writes a boolean value to the buffer.voidwriteByte(byte b) Writes a single byte to the buffer.voidwriteByte(int b) Writes a single byte (represented as an integer) to the buffer.voidwriteChar(char value) Writes a single character to the buffer.voidwriteCharSequence(CharSequence value, Charset charset) Writes aCharSequenceto the buffer using the specifiedCharset.voidwriteDouble(double value) Writes a double value (8 bytes) to the buffer.<T extends Enum<?>>
voidwriteEnum(T t) Writes an enum constant to the buffer by its ordinal value.voidwriteFloat(float value) Writes a floating-point value (4 bytes) to the buffer.voidwriteInt(int value) Writes an integer (4 bytes) to the buffer.voidwriteLong(long value) Writes a long (8 bytes) to the buffer.intReturns the current writer index of the buffer.voidwriterIndex(int writerIndex) Sets the writer index to the specified value.voidwriteShort(short value) Writes a short (2 bytes) to the buffer.voidwriteUnsignedShort(int value) Writes an unsigned short (2 bytes) to the buffer.voidWrites a UTF-8 encoded string to the buffer.voidWrites aUUIDto the buffer.voidwriteVarInt(int value) Writes a variable-length integer to the buffer using the VarInt format.voidwriteVarLong(long value) Writes a variable-length long to the buffer using the VarLong format.
-
Constructor Details
-
ByteBuffer
public ByteBuffer(int initialSize) Constructs a newByteBufferwith the given initial size. The buffer is both read-only and fixed in size by default.- Parameters:
initialSize- the initial size of the buffer.
-
ByteBuffer
public ByteBuffer(int initialSize, boolean fixedSize) Constructs a newByteBufferwith the given initial size and the option to set a fixed size.- Parameters:
initialSize- the initial size of the buffer.fixedSize- whether the buffer size is fixed or can be expanded.
-
ByteBuffer
public ByteBuffer(int initialSize, boolean readOnly, boolean fixedSize) Constructs a newByteBufferwith the given initial size, read-only, and fixed-size flags.- Parameters:
initialSize- the initial size of the buffer.readOnly- whether the buffer is read-only.fixedSize- whether the buffer size is fixed or can be expanded.
-
ByteBuffer
public ByteBuffer(byte[] source) Constructs a newByteBufferfrom an existing byte array. The buffer is read-only and fixed in size by default.- Parameters:
source- the byte array to initialize the buffer with.
-
ByteBuffer
public ByteBuffer(byte[] source, boolean readOnly) Constructs a newByteBufferfrom an existing byte array with the option to set the buffer as read-only.- Parameters:
source- the byte array to initialize the buffer with.readOnly- whether the buffer is read-only.
-
ByteBuffer
public ByteBuffer(byte[] source, boolean readOnly, boolean fixedSize) Constructs a newByteBufferfrom an existing byte array with options for read-only and fixed size.- Parameters:
source- the byte array to initialize the buffer with.readOnly- whether the buffer is read-only.fixedSize- whether the buffer size is fixed or can be expanded.
-
-
Method Details
-
readNBytes
public byte[] readNBytes(int length) Reads the specified number of bytes from the buffer and advances the reader index.- Parameters:
length- the number of bytes to read.- Returns:
- the bytes read as a new byte array.
-
readRemaining
public byte[] readRemaining()Reads all remaining bytes from the current reader index.- Returns:
- the remaining bytes in the buffer.
-
readByte
public byte readByte()Reads a single byte from the buffer.- Returns:
- the byte that was read.
-
readShifted
public int readShifted(int length, int shift) Reads a shifted integer from the buffer by combining bytes. This method readslengthbytes, shifts them according to the given shift value, and assembles the result.- Parameters:
length- the number of bytes to read.shift- the initial shift amount.- Returns:
- the assembled integer value.
-
readShort
public short readShort()Reads a short (2 bytes) from the buffer.- Returns:
- the short value read from the buffer.
-
readUnsignedShort
public int readUnsignedShort()Reads an unsigned short (2 bytes) from the buffer.- Returns:
- the unsigned short value as an integer.
-
readInt
public int readInt()Reads an integer (4 bytes) from the buffer.- Returns:
- the integer value read from the buffer.
-
readVarInt
public int readVarInt()Reads a variable-length integer from the buffer. This method reads the VarInt format, where the integer is encoded in one to five bytes. If the VarInt exceeds 5 bytes, aRuntimeExceptionis thrown.- Returns:
- the decoded VarInt.
- Throws:
RuntimeException- if the VarInt is too large (more than 5 bytes).
-
readFloat
public float readFloat()Reads a floating-point value (4 bytes) from the buffer. The float is read as its raw integer bits, which are then converted back to a float usingFloat.intBitsToFloat(int).- Returns:
- the float value read from the buffer.
-
readLong
public long readLong()Reads a long (8 bytes) from the buffer. The long is constructed by combining two integer values.- Returns:
- the long value read from the buffer.
-
readVarLong
public long readVarLong()Reads a variable-length long from the buffer. This method reads the VarLong format, where the long is encoded in one to ten bytes. If the VarLong exceeds 10 bytes, aRuntimeExceptionis thrown.- Returns:
- the decoded VarLong.
- Throws:
RuntimeException- if the VarLong is too large (more than 10 bytes).
-
readDouble
public double readDouble()Reads a double value (8 bytes) from the buffer. The double is read as its raw long bits, which are then converted back to a double usingDouble.longBitsToDouble(long).- Returns:
- the double value read from the buffer.
-
readBoolean
public boolean readBoolean()Reads a boolean value from the buffer. This method reads a single byte and returnstrueif the byte is non-zero, orfalseif the byte is zero.- Returns:
- the boolean value read from the buffer.
-
readChar
public char readChar()Reads a single character from the buffer. This method reads a byte and casts it to achar.- Returns:
- the character read from the buffer.
-
readUTF
Reads a UTF-8 encoded string from the buffer. This method first reads the length of the string (stored as a VarInt), followed by the actual UTF-8 encoded bytes of the string.- Returns:
- the UTF string read from the buffer.
-
readUTF
Reads a UTF-8 encoded string of the given length from the buffer.- Parameters:
length- the length of the UTF string to read.- Returns:
- the UTF string read from the buffer.
-
readCharSequence
Reads a sequence of characters from the buffer using the specifiedCharset. This method reads the specified number of bytes from the buffer and decodes them as a character sequence in the provided charset.- Parameters:
length- the number of bytes to read.charset- theCharsetto use for decoding.- Returns:
- the decoded character sequence.
-
readEnum
Reads an enum constant from the buffer by ordinal value. The method reads an integer from the buffer, then returns the corresponding enum constant for the providedtype.- Type Parameters:
T- the enum type.- Parameters:
type- the class of the enum type.- Returns:
- the enum constant corresponding to the ordinal value.
-
readUUID
Reads aUUIDfrom the buffer, which consists of two long values (most significant bits and least significant bits).- Returns:
- the
UUIDread from the buffer.
-
readerIndex
public int readerIndex()Returns the current reader index of the buffer.- Returns:
- the reader index.
-
readerIndex
public void readerIndex(int readerIndex) Sets the reader index to the specified value.- Parameters:
readerIndex- the new reader index.
-
markReaderIndex
public void markReaderIndex()Marks the current reader index, so it can be restored later. -
resetReaderIndex
public void resetReaderIndex()Resets the reader index to the previously marked index. -
readableBytes
public int readableBytes()Returns the number of readable bytes left in the buffer. This is the number of bytes between the current reader index and the end of the buffer.- Returns:
- the number of readable bytes.
-
isReadable
public boolean isReadable()Checks if there are any readable bytes remaining in the buffer.- Returns:
trueif there are readable bytes remaining, otherwisefalse.
-
isReadable
public boolean isReadable(int length) Checks if the buffer has enough readable bytes for a specified length.- Parameters:
length- the number of bytes to check for.- Returns:
trueif the buffer has at leastlengthreadable bytes, otherwisefalse.
-
write
public void write(byte[] data) Writes the specified byte array to the buffer at the current writer index.- Parameters:
data- the byte array to write.
-
writeByte
public void writeByte(byte b) Writes a single byte to the buffer.- Parameters:
b- the byte to write.
-
writeByte
public void writeByte(int b) Writes a single byte (represented as an integer) to the buffer. This method casts the integer to a byte before writing.- Parameters:
b- the byte to write, as an integer.
-
writeShort
public void writeShort(short value) Writes a short (2 bytes) to the buffer. The value is written with a shift of 8 bits.- Parameters:
value- the short value to write.
-
writeUnsignedShort
public void writeUnsignedShort(int value) Writes an unsigned short (2 bytes) to the buffer. The value is written with a shift of 8 bits.- Parameters:
value- the unsigned short value to write.
-
writeVarInt
public void writeVarInt(int value) Writes a variable-length integer to the buffer using the VarInt format. The integer is encoded into one to five bytes. This method writes one byte at a time until all significant bits are encoded.- Parameters:
value- the VarInt to write.
-
writeInt
public void writeInt(int value) Writes an integer (4 bytes) to the buffer. The value is written with a shift of 24 bits.- Parameters:
value- the integer value to write.
-
writeFloat
public void writeFloat(float value) Writes a floating-point value (4 bytes) to the buffer. The float is first converted to its raw integer bits, then written to the buffer.- Parameters:
value- the float value to write.
-
writeLong
public void writeLong(long value) Writes a long (8 bytes) to the buffer. The value is written with a shift of 56 bits.- Parameters:
value- the long value to write.
-
writeVarLong
public void writeVarLong(long value) Writes a variable-length long to the buffer using the VarLong format. The long is encoded into one to ten bytes. This method writes one byte at a time until all significant bits are encoded.- Parameters:
value- the VarLong to write.
-
writeDouble
public void writeDouble(double value) Writes a double value (8 bytes) to the buffer. The double is first converted to its raw long bits, then written to the buffer.- Parameters:
value- the double value to write.
-
writeBoolean
public void writeBoolean(boolean value) Writes a boolean value to the buffer. If the value istrue, it writes a byte with value1; iffalse, it writes a byte with value0.- Parameters:
value- the boolean value to write.
-
writeChar
public void writeChar(char value) Writes a single character to the buffer. This method writes the character as a single byte.- Parameters:
value- the character to write.
-
writeUTF
Writes a UTF-8 encoded string to the buffer. This method first writes the length of the UTF string usingwriteVarInt(), followed by the UTF-8 encoded bytes of the string.- Parameters:
value- the UTF string to write.
-
writeCharSequence
Writes aCharSequenceto the buffer using the specifiedCharset. The char sequence is converted into a byte array using the given character encoding, and the byte array is then written to the buffer.- Parameters:
value- theCharSequenceto write.charset- theCharsetto use for encoding.
-
writeEnum
Writes an enum constant to the buffer by its ordinal value. This method writes the ordinal (position in enum declaration) of the given enum constant.- Type Parameters:
T- the enum type.- Parameters:
t- the enum constant to write.
-
writeUUID
Writes aUUIDto the buffer. A UUID consists of two long values: the most significant bits and the least significant bits.- Parameters:
uuid- theUUIDto write.
-
writerIndex
public int writerIndex()Returns the current writer index of the buffer.- Returns:
- the writer index.
-
writerIndex
public void writerIndex(int writerIndex) Sets the writer index to the specified value.- Parameters:
writerIndex- the new writer index.
-
markWriterIndex
public void markWriterIndex()Marks the current writer index, so it can be restored later. -
resetWriterIndex
public void resetWriterIndex()Resets the writer index to the previously marked index. -
writeableBytes
public int writeableBytes()Returns the number of writable bytes remaining in the buffer. This is the number of bytes that can still be written without exceeding the buffer's capacity.- Returns:
- the number of writable bytes.
-
isWriteable
public boolean isWriteable()Checks if the buffer is writable (i.e., there is space to write).- Returns:
trueif there is at least one writable byte, otherwisefalse.
-
isWriteable
public boolean isWriteable(int length) Checks if the buffer has enough space to write the specified number of bytes.- Parameters:
length- the number of bytes to check for.- Returns:
trueif there is enough space to write the specified number of bytes, otherwisefalse.
-
isReadOnly
public boolean isReadOnly()Checks if the buffer is read-only.- Returns:
trueif the buffer is read-only,falseotherwise.
-
isFixedSize
public boolean isFixedSize()Checks if the buffer has a fixed size.- Returns:
trueif the buffer has a fixed size,falseotherwise.
-
size
public int size()Returns the size of the buffer.- Returns:
- the size of the buffer in bytes.
-
getSource
public byte[] getSource()Returns the underlying byte array used by the buffer.- Returns:
- the byte array representing the buffer's contents.
-