Class NettyMutableDataBuf

java.lang.Object
eu.cloudnetservice.driver.network.netty.buffer.NettyImmutableDataBuf
eu.cloudnetservice.driver.network.netty.buffer.NettyMutableDataBuf
All Implemented Interfaces:
DataBuf, DataBuf.Mutable, AutoCloseable

public class NettyMutableDataBuf extends NettyImmutableDataBuf implements DataBuf.Mutable
Represents the default implementation of a mutable data buf, wrapping a netty byte buffer.
Since:
4.0
  • Constructor Details

    • NettyMutableDataBuf

      public NettyMutableDataBuf(@NonNull @NonNull io.netty5.buffer.Buffer buffer)
      Constructs a new mutable data buf instance.
      Parameters:
      buffer - the netty buffer to wrap.
      Throws:
      NullPointerException - if the given buffer is null.
  • Method Details

    • writeBoolean

      @NonNull public DataBuf.Mutable writeBoolean(boolean b)
      Writes the given boolean at the current writer index, increasing the index by one.
      Specified by:
      writeBoolean in interface DataBuf.Mutable
      Parameters:
      b - the boolean to write.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeInt

      @NonNull public DataBuf.Mutable writeInt(int integer)
      Writes the given integer at the current writer index, increasing the index by four.
      Specified by:
      writeInt in interface DataBuf.Mutable
      Parameters:
      integer - the integer to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeByte

      @NonNull public DataBuf.Mutable writeByte(byte b)
      Writes the given byte at the current writer index, increasing the index by one.
      Specified by:
      writeByte in interface DataBuf.Mutable
      Parameters:
      b - the byte to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeShort

      @NonNull public DataBuf.Mutable writeShort(short s)
      Writes the given short at the current writer index, increasing the index by two.
      Specified by:
      writeShort in interface DataBuf.Mutable
      Parameters:
      s - the short to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeLong

      @NonNull public DataBuf.Mutable writeLong(long l)
      Writes the given long at the current writer index, increasing the index by eight.
      Specified by:
      writeLong in interface DataBuf.Mutable
      Parameters:
      l - the long to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeFloat

      @NonNull public DataBuf.Mutable writeFloat(float f)
      Writes the given float at the current writer index, increasing the index by four.
      Specified by:
      writeFloat in interface DataBuf.Mutable
      Parameters:
      f - the float to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeDouble

      @NonNull public DataBuf.Mutable writeDouble(double d)
      Writes the given double at the current writer index, increasing the index by eight.
      Specified by:
      writeDouble in interface DataBuf.Mutable
      Parameters:
      d - the double to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeChar

      @NonNull public DataBuf.Mutable writeChar(char c)
      Writes the given UTF-16 char at the current writer index, increasing the index by two.
      Specified by:
      writeChar in interface DataBuf.Mutable
      Parameters:
      c - the char to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeByteArray

      @NonNull public DataBuf.Mutable writeByteArray(byte[] b)
      Writes the given byte array into the buffer, prefixed by an integer containing the amount of bytes following in the array.

      This method call is equivalent to writeByteArray(b, b.length).

      Specified by:
      writeByteArray in interface DataBuf.Mutable
      Parameters:
      b - the byte array to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeByteArray

      @NonNull public DataBuf.Mutable writeByteArray(byte[] b, int amount)
      Writes the given byte array into the buffer, prefixed by an integer containing the amount of bytes following in the array.
      Specified by:
      writeByteArray in interface DataBuf.Mutable
      Parameters:
      b - the byte array to write into the buffer.
      amount - the amount of bytes of the array to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeUniqueId

      @NonNull public DataBuf.Mutable writeUniqueId(@NonNull @NonNull UUID uuid)
      Writes the unique id into the buffer by first writing the most significant bits of the id followed by the last significant bits of the id.
      Specified by:
      writeUniqueId in interface DataBuf.Mutable
      Parameters:
      uuid - the id to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeString

      @NonNull public DataBuf.Mutable writeString(@NonNull @NonNull String string)
      Writes the string into the buffer. This method does the same thing as DataBuf.Mutable.writeByteArray(byte[]). The string gets converted into it's byte array representation and then written into the buffer like that.
      Specified by:
      writeString in interface DataBuf.Mutable
      Parameters:
      string - the string to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeDataBuf

      @NonNull public DataBuf.Mutable writeDataBuf(@NonNull @NonNull DataBuf buf)
      Writes all data of the given data buffer into this data buffer starting at the current reader index of the given buffer.

      Buffers are not expected to be cross-implementation-compatible. For instance, a netty buffer can only be written to netty buffers.

      Specified by:
      writeDataBuf in interface DataBuf.Mutable
      Parameters:
      buf - the buffer to write into this buffer.
      Returns:
      the same buffer used to call the method, for chaining.
    • writeObject

      Writes the given object into this buffer. The object is written using the default object mapper of the system.
      Specified by:
      writeObject in interface DataBuf.Mutable
      Parameters:
      obj - the object to write into the buffer.
      Returns:
      the same buffer used to call the method, for chaining.
      See Also:
    • writeNullable

      @NonNull public <T> @NonNull DataBuf.Mutable writeNullable(@Nullable T object, @NonNull @NonNull BiConsumer<DataBuf.Mutable,T> handlerWhenNonNull)
      Writes the given object null-safe into this buffer. It appends a boolean before the actual object data (if the object is present) whether the data is present. The writer consumer is only called when the data is present and can then safely proceed to write all the required data into the buffer. The supplied buffer is the same buffer used for calling the method.
      Specified by:
      writeNullable in interface DataBuf.Mutable
      Type Parameters:
      T - the generic type of the object being written.
      Parameters:
      object - the object which should be safely written into this buffer.
      handlerWhenNonNull - the writer of the object when it's non-null.
      Returns:
      the same buffer used to call the method, for chaining.
    • ensureWriteable

      @NonNull public DataBuf.Mutable ensureWriteable(int bytes)
      Ensures that this buffer has at least the given amount of bytes unused for writing data. If the buffer already has the amount of bytes present, this method returns immediately.
      Specified by:
      ensureWriteable in interface DataBuf.Mutable
      Parameters:
      bytes - the bytes that must be available in the buffer.
      Returns:
      this buffer, for chaining.
    • asImmutable

      @NonNull public @NonNull DataBuf asImmutable()
      Converts this buffer into an immutable version of it. The underlying buffer is not expected to be clones, that means that writes to this buffer are still reflected into the immutable version of it and vise-versa.
      Specified by:
      asImmutable in interface DataBuf.Mutable
      Returns:
      an immutable version of this buffer.