Class ByteBuffer

java.lang.Object
de.craftsblock.craftsnet.utils.ByteBuffer

public class ByteBuffer extends Object
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

    Constructors
    Constructor
    Description
    ByteBuffer(byte[] source)
    Constructs a new ByteBuffer from an existing byte array.
    ByteBuffer(byte[] source, boolean readOnly)
    Constructs a new ByteBuffer from an existing byte array with the option to set the buffer as read-only.
    ByteBuffer(byte[] source, boolean readOnly, boolean fixedSize)
    Constructs a new ByteBuffer from an existing byte array with options for read-only and fixed size.
    ByteBuffer(int initialSize)
    Constructs a new ByteBuffer with the given initial size.
    ByteBuffer(int initialSize, boolean fixedSize)
    Constructs a new ByteBuffer with the given initial size and the option to set a fixed size.
    ByteBuffer(int initialSize, boolean readOnly, boolean fixedSize)
    Constructs a new ByteBuffer with the given initial size, read-only, and fixed-size flags.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    Returns the underlying byte array used by the buffer.
    boolean
    Checks if the buffer has a fixed size.
    boolean
    Checks if there are any readable bytes remaining in the buffer.
    boolean
    isReadable(int length)
    Checks if the buffer has enough readable bytes for a specified length.
    boolean
    Checks if the buffer is read-only.
    boolean
    Checks if the buffer is writable (i.e., there is space to write).
    boolean
    isWriteable(int length)
    Checks if the buffer has enough space to write the specified number of bytes.
    void
    Marks the current reader index, so it can be restored later.
    void
    Marks the current writer index, so it can be restored later.
    int
    Returns the number of readable bytes left in the buffer.
    boolean
    Reads a boolean value from the buffer.
    byte
    Reads a single byte from the buffer.
    char
    Reads a single character from the buffer.
    readCharSequence(int length, Charset charset)
    Reads a sequence of characters from the buffer using the specified Charset.
    double
    Reads a double value (8 bytes) from the buffer.
    <T extends Enum<?>>
    T
    readEnum(Class<T> type)
    Reads an enum constant from the buffer by ordinal value.
    int
    Returns the current reader index of the buffer.
    void
    readerIndex(int readerIndex)
    Sets the reader index to the specified value.
    float
    Reads a floating-point value (4 bytes) from the buffer.
    int
    Reads an integer (4 bytes) from the buffer.
    long
    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.
    int
    readShifted(int length, int shift)
    Reads a shifted integer from the buffer by combining bytes.
    short
    Reads a short (2 bytes) from the buffer.
    int
    Reads an unsigned short (2 bytes) from the buffer.
    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.
    Reads a UUID from the buffer, which consists of two long values (most significant bits and least significant bits).
    int
    Reads a variable-length integer from the buffer.
    long
    Reads a variable-length long from the buffer.
    void
    Resets the reader index to the previously marked index.
    void
    Resets the writer index to the previously marked index.
    int
    Returns the size of the buffer.
    void
    write(byte[] data)
    Writes the specified byte array to the buffer at the current writer index.
    int
    Returns the number of writable bytes remaining in the buffer.
    void
    writeBoolean(boolean value)
    Writes a boolean value to the buffer.
    void
    writeByte(byte b)
    Writes a single byte to the buffer.
    void
    writeByte(int b)
    Writes a single byte (represented as an integer) to the buffer.
    void
    writeChar(char value)
    Writes a single character to the buffer.
    void
    Writes a CharSequence to the buffer using the specified Charset.
    void
    writeDouble(double value)
    Writes a double value (8 bytes) to the buffer.
    <T extends Enum<?>>
    void
    writeEnum(T t)
    Writes an enum constant to the buffer by its ordinal value.
    void
    writeFloat(float value)
    Writes a floating-point value (4 bytes) to the buffer.
    void
    writeInt(int value)
    Writes an integer (4 bytes) to the buffer.
    void
    writeLong(long value)
    Writes a long (8 bytes) to the buffer.
    int
    Returns the current writer index of the buffer.
    void
    writerIndex(int writerIndex)
    Sets the writer index to the specified value.
    void
    writeShort(short value)
    Writes a short (2 bytes) to the buffer.
    void
    writeUnsignedShort(int value)
    Writes an unsigned short (2 bytes) to the buffer.
    void
    Writes a UTF-8 encoded string to the buffer.
    void
    Writes a UUID to the buffer.
    void
    writeVarInt(int value)
    Writes a variable-length integer to the buffer using the VarInt format.
    void
    writeVarLong(long value)
    Writes a variable-length long to the buffer using the VarLong format.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteBuffer

      public ByteBuffer(int initialSize)
      Constructs a new ByteBuffer with 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 new ByteBuffer with 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 new ByteBuffer with 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 new ByteBuffer from 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 new ByteBuffer from 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 new ByteBuffer from 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 reads length bytes, 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, a RuntimeException is 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 using Float.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, a RuntimeException is 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 using Double.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 returns true if the byte is non-zero, or false if 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 a char.
      Returns:
      the character read from the buffer.
    • readUTF

      public String 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

      public String readUTF(int length)
      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

      public String readCharSequence(int length, Charset charset)
      Reads a sequence of characters from the buffer using the specified Charset. 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 - the Charset to use for decoding.
      Returns:
      the decoded character sequence.
    • readEnum

      public <T extends Enum<?>> T readEnum(Class<T> type)
      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 provided type.
      Type Parameters:
      T - the enum type.
      Parameters:
      type - the class of the enum type.
      Returns:
      the enum constant corresponding to the ordinal value.
    • readUUID

      public UUID readUUID()
      Reads a UUID from the buffer, which consists of two long values (most significant bits and least significant bits).
      Returns:
      the UUID read 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:
      true if there are readable bytes remaining, otherwise false.
    • 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:
      true if the buffer has at least length readable bytes, otherwise false.
    • 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 is true, it writes a byte with value 1; if false, it writes a byte with value 0.
      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

      public void writeUTF(String value)
      Writes a UTF-8 encoded string to the buffer. This method first writes the length of the UTF string using writeVarInt(), followed by the UTF-8 encoded bytes of the string.
      Parameters:
      value - the UTF string to write.
    • writeCharSequence

      public void writeCharSequence(CharSequence value, Charset charset)
      Writes a CharSequence to the buffer using the specified Charset. 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 - the CharSequence to write.
      charset - the Charset to use for encoding.
    • writeEnum

      public <T extends Enum<?>> void writeEnum(T t)
      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

      public void writeUUID(UUID uuid)
      Writes a UUID to the buffer. A UUID consists of two long values: the most significant bits and the least significant bits.
      Parameters:
      uuid - the UUID to 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:
      true if there is at least one writable byte, otherwise false.
    • 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:
      true if there is enough space to write the specified number of bytes, otherwise false.
    • isReadOnly

      public boolean isReadOnly()
      Checks if the buffer is read-only.
      Returns:
      true if the buffer is read-only, false otherwise.
    • isFixedSize

      public boolean isFixedSize()
      Checks if the buffer has a fixed size.
      Returns:
      true if the buffer has a fixed size, false otherwise.
    • 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.