Class NettyImmutableDataBuf

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

public class NettyImmutableDataBuf extends Object implements DataBuf
The default implementation of an immutable data buf wrapping a netty byte buf.
Since:
4.0
  • Nested Class Summary

    Nested classes/interfaces inherited from interface eu.cloudnetservice.driver.network.buffer.DataBuf

    DataBuf.Mutable
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final io.netty5.buffer.Buffer
     
    protected int
     
    protected boolean
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    NettyImmutableDataBuf(@NonNull io.netty5.buffer.Buffer buffer)
    Constructs a new netty immutable data buf instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Get if the current buffer is still accessible or if it was released already.
    Converts this immutable buffer to a mutable one.
    @NonNull io.netty5.buffer.Buffer
    Get the wrapped netty byte buf of this buffer, for internal use only.
    void
    Explicitly releases all data associated with this buffer making it unavailable for further reads.
    Disables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call to release() or close() is made.
    Enables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call to release() or close() is made.
    protected <T> T
    hotRead(@NonNull Function<io.netty5.buffer.Buffer,T> reader)
    Reads from this buffer, releasing it when the end of the input has been reached and releasing is enabled to prevent memory leaks.
    int
    Get the number of remaining bytes in the buffer until the buffer gets released (when enabled).
    boolean
    Reads a boolean from this buffer at the current reader index.
    byte
    Reads a byte from this buffer at the current reader index.
    byte[]
    Reads the next array of bytes from the buffer.
    char
    Reads a 16-bit (UTF-16) char from this buffer at the current reader index.
    Reads the next data buf from the buffer.
    double
    Reads a 64-bit double from this buffer at the current reader index.
    float
    Reads a 32-bit float from this buffer at the current reader index.
    int
    Reads a 32-bit integer from this buffer at the current reader index.
    long
    Reads a 64-bit long from this buffer at the current reader index.
    <T> T
    readNullable(@NonNull Function<DataBuf,T> readerWhenNonNull)
    Reads the next requested data from the buffer.
    <T> T
    readNullable(@NonNull Function<DataBuf,T> readerWhenNonNull, T valueWhenNull)
    Reads the next requested data from the buffer.
    <T> T
    Reads the next object from the buffer at the current reader index.
    <T> T
    Reads the next object from the buffer at the current reader index.
    short
    Reads a 16-bit short from this buffer at the current reader index.
    Reads the next UTF-8 encoded string from the buffer.
    Reads the next unique id from the buffer at the current reader index.
    Redoes the currently running transaction on the buffer.
    void
    Explicitly releases all data associated with this buffer making it unavailable for further reads.
    Starts a transaction to the buffer.
    byte[]
    Converts the remaining bytes in this buffer into a byte array.

    Methods inherited from class java.lang.Object

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

    • buffer

      protected final io.netty5.buffer.Buffer buffer
    • releasable

      protected boolean releasable
    • readOffset

      protected volatile int readOffset
    • writeOffset

      protected volatile int writeOffset
  • Constructor Details

    • NettyImmutableDataBuf

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

    • readBoolean

      public boolean readBoolean()
      Reads a boolean from this buffer at the current reader index. Exactly one byte is read from the buffer.
      Specified by:
      readBoolean in interface DataBuf
      Returns:
      the boolean representation of the byte at the current position.
    • readByte

      public byte readByte()
      Reads a byte from this buffer at the current reader index. Exactly one byte is read from the buffer.
      Specified by:
      readByte in interface DataBuf
      Returns:
      the byte at the current reader position.
    • readInt

      public int readInt()
      Reads a 32-bit integer from this buffer at the current reader index. Exactly four bytes are read from the buffer.
      Specified by:
      readInt in interface DataBuf
      Returns:
      the next integer in the buffer at the current reader index.
    • readShort

      public short readShort()
      Reads a 16-bit short from this buffer at the current reader index. Exactly two bytes are read from the buffer.
      Specified by:
      readShort in interface DataBuf
      Returns:
      the next short in the buffer at the current reader index.
    • readLong

      public long readLong()
      Reads a 64-bit long from this buffer at the current reader index. Exactly eight bytes are read from the buffer.
      Specified by:
      readLong in interface DataBuf
      Returns:
      the next long in the buffer at the current reader index.
    • readFloat

      public float readFloat()
      Reads a 32-bit float from this buffer at the current reader index. Exactly four bytes are read from the buffer.
      Specified by:
      readFloat in interface DataBuf
      Returns:
      the next float in the buffer at the current reader index.
    • readDouble

      public double readDouble()
      Reads a 64-bit double from this buffer at the current reader index. Exactly eight bytes are read from the buffer.
      Specified by:
      readDouble in interface DataBuf
      Returns:
      the next double in the buffer at the current reader index.
    • readChar

      public char readChar()
      Reads a 16-bit (UTF-16) char from this buffer at the current reader index. Exactly two bytes are read from the buffer.
      Specified by:
      readChar in interface DataBuf
      Returns:
      the next UTF-16 char in the buffer at the current reader index.
    • readByteArray

      public byte[] readByteArray()
      Reads the next array of bytes from the buffer. A byte array is serialized in a special way. The bytes in the buffer are prefixed with the number of bytes in the array. Two steps are made to read an array from the buffer:
      1. The number of bytes in the following array are read from the buffer (by default a var int).
      2. The number of bytes the array is prefixed with are read from the buffer and put into a new array.

      As the operation is dynamic there is no way to pre-calculate the amount of bytes needed to read the next byte array.

      Specified by:
      readByteArray in interface DataBuf
      Returns:
      the next byte array in the buffer at the current reader index.
    • readUniqueId

      @NonNull public @NonNull UUID readUniqueId()
      Reads the next unique id from the buffer at the current reader index. The operation reads two longs from the buffer: the most significant bits of the unique id, and the least significant bits of the unique id. This totals to exactly sixteen bytes which are read from the buffer.
      Specified by:
      readUniqueId in interface DataBuf
      Returns:
      the next unique id in the buffer at the current reader index.
    • readString

      @NonNull public @NonNull String readString()
      Reads the next UTF-8 encoded string from the buffer. A string during write is converted to a byte array containing all bytes in UTF-8 form. Reading just reverses this operation. See DataBuf.readByteArray() for an explanation how the read operation works in detail (it's the same operation, the result is just wrapped using the string constructor).
      Specified by:
      readString in interface DataBuf
      Returns:
      the next string in the buffer at the current reader index.
    • readDataBuf

      @NonNull public @NonNull DataBuf readDataBuf()
      Reads the next data buf from the buffer. A data buf write works like a byte array write operation because the buffer is essentially just wrapping a byte array. See DataBuf.readByteArray() about the expected format.

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

      Specified by:
      readDataBuf in interface DataBuf
      Returns:
      the data buf in the buffer at the current reader index.
    • toByteArray

      public byte[] toByteArray()
      Converts the remaining bytes in this buffer into a byte array. This operation moves the reader index to the end of the buffer.
      Specified by:
      toByteArray in interface DataBuf
      Returns:
      the remaining bytes in this buffer converted to a byte array.
    • readObject

      @Nullable public <T> T readObject(@NonNull @NonNull Class<T> type)
      Reads the next object from the buffer at the current reader index. The object is read using the default object mapper of the system.
      Specified by:
      readObject in interface DataBuf
      Type Parameters:
      T - the generic type of the object to read.
      Parameters:
      type - the type of the object to read.
      Returns:
      the next object in the buffer at the current reader index.
      See Also:
    • readObject

      public <T> T readObject(@NonNull @NonNull Type type)
      Reads the next object from the buffer at the current reader index. The object is read using the default object mapper of the system.
      Specified by:
      readObject in interface DataBuf
      Type Parameters:
      T - the generic type of the object to read.
      Parameters:
      type - the type of the object to read.
      Returns:
      the next object in the buffer at the current reader index.
      See Also:
    • readNullable

      @Nullable public <T> T readNullable(@NonNull @NonNull Function<DataBuf,T> readerWhenNonNull)
      Reads the next requested data from the buffer. This method call is equivalent to readNullable(readerWhenNonNull, null).
      Specified by:
      readNullable in interface DataBuf
      Type Parameters:
      T - the generic type of the data to read.
      Parameters:
      readerWhenNonNull - the reader to read the data from the buffer when the next value is non-null.
      Returns:
      the value read from the buffer or the fallback value when the buffered contained null at the position.
    • readNullable

      public <T> T readNullable(@NonNull @NonNull Function<DataBuf,T> readerWhenNonNull, T valueWhenNull)
      Reads the next requested data from the buffer. To determine whether the given reader for further reading should be called, the boolean before the actual data is read. If the boolean is true, the following data is present (non-null), otherwise the given value to return when null is returned.
      Specified by:
      readNullable in interface DataBuf
      Type Parameters:
      T - the generic type of the data to read.
      Parameters:
      readerWhenNonNull - the reader to read the data from the buffer when the next value is non-null.
      valueWhenNull - the value to return when the buffer contains a null value at the current reader index.
      Returns:
      the value read from the buffer or the fallback value when the buffered contained null at the position.
    • readableBytes

      public int readableBytes()
      Get the number of remaining bytes in the buffer until the buffer gets released (when enabled).
      Specified by:
      readableBytes in interface DataBuf
      Returns:
      the number of remaining bytes in the buffer.
    • startTransaction

      @NonNull public @NonNull DataBuf startTransaction()
      Starts a transaction to the buffer. Starting a transaction while another transaction is active will override the current transaction marker. A transaction can be redone by using DataBuf.redoTransaction().
      Specified by:
      startTransaction in interface DataBuf
      Returns:
      the same instance as used to call the method, for chaining.
    • redoTransaction

      @NonNull public @NonNull DataBuf redoTransaction()
      Redoes the currently running transaction on the buffer. If no transaction was started before, the reader and writer index will go back to 0.
      Specified by:
      redoTransaction in interface DataBuf
      Returns:
      the same instance as used to call the method, for chaining.
    • asMutable

      @NonNull public DataBuf.Mutable asMutable()
      Converts this immutable buffer to a mutable one. There is no need to copy the underlying byte tracker, meaning that all writes will be reflected into this buffer and vise-versa.
      Specified by:
      asMutable in interface DataBuf
      Returns:
      a mutable variant of this buffer.
    • accessible

      public boolean accessible()
      Get if the current buffer is still accessible or if it was released already.
      Specified by:
      accessible in interface DataBuf
      Returns:
      if the current buffer is still accessible.
    • disableReleasing

      @NonNull public @NonNull DataBuf disableReleasing()
      Disables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call to release() or close() is made.
      Specified by:
      disableReleasing in interface DataBuf
      Returns:
      the same instance as used to call the method, for chaining.
    • enableReleasing

      @NonNull public @NonNull DataBuf enableReleasing()
      Enables that the underlying buffer can be released when the reader index of this buffer reaches the tail or an explicit call to release() or close() is made.
      Specified by:
      enableReleasing in interface DataBuf
      Returns:
      the same instance as used to call the method, for chaining.
    • release

      public void release()
      Explicitly releases all data associated with this buffer making it unavailable for further reads. This method does nothing if releasing was disables before calling this method.
      Specified by:
      release in interface DataBuf
    • close

      public void close()
      Explicitly releases all data associated with this buffer making it unavailable for further reads. This method does nothing if releasing was disables before calling this method.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface DataBuf
    • buffer

      @Internal @NonNull public @NonNull io.netty5.buffer.Buffer buffer()
      Get the wrapped netty byte buf of this buffer, for internal use only.
      Returns:
      the wrapped netty byte buf.
    • hotRead

      @NonNull protected <T> T hotRead(@NonNull @NonNull Function<io.netty5.buffer.Buffer,T> reader)
      Reads from this buffer, releasing it when the end of the input has been reached and releasing is enabled to prevent memory leaks.
      Type Parameters:
      T - the type of data to read.
      Parameters:
      reader - the function which reads the requested data from the buffer.
      Returns:
      the data read from the buffer.
      Throws:
      NullPointerException - if the given reader function is null.