Class NettyDataBufFactory

java.lang.Object
eu.cloudnetservice.driver.network.netty.buffer.NettyDataBufFactory
All Implemented Interfaces:
DataBufFactory

@Singleton @Provides(DataBufFactory.class) public class NettyDataBufFactory extends Object implements DataBufFactory
An implementation (and currently the default one) of a data buf factory wrapping netty byte buffers.
Since:
4.0
See Also:
  • Field Details

    • INSTANCE

      public static final NettyDataBufFactory INSTANCE
    • ALLOCATOR

      protected static final io.netty5.buffer.BufferAllocator ALLOCATOR
  • Constructor Details

    • NettyDataBufFactory

      protected NettyDataBufFactory()
      Creates a new instance of this factory. This method is protected to allow developers to create their own variant of this factory. In normal cases DataBufFactory.defaultFactory() should be used to obtain the instance of this factory.
  • Method Details

    • createEmpty

      @NonNull public DataBuf.Mutable createEmpty()
      Creates an empty buffer which can be expanded by writing to it.
      Specified by:
      createEmpty in interface DataBufFactory
      Returns:
      a new, empty data buf.
    • fromBytes

      @NonNull public @NonNull DataBuf fromBytes(byte[] bytes)
      Creates a new readonly buffer wrapping the given byte array and using it as it's data source. Modification to the given bytes will be visible in the buffer.
      Specified by:
      fromBytes in interface DataBufFactory
      Parameters:
      bytes - the bytes to wrap.
      Returns:
      a new buffer wrapping the given bytes.
    • copyOf

      @NonNull public @NonNull DataBuf copyOf(@NonNull @NonNull DataBuf dataBuf)
      Creates a readonly copy of the given data buffer. The copied variant of the buffer will start the read process from the first byte rather than re-using the current index of the original buffer.

      A factory is only expected to be able to copy a buffer created by it. Cross factory copy might be possible but is not a requirement. Use factory.createOf(buffer.toByteArray) if you want to be sure that the buffer can be copied.

      Specified by:
      copyOf in interface DataBufFactory
      Parameters:
      dataBuf - the buffer to copy.
      Returns:
      a copied variant of the given buffer.
    • mutableCopyOf

      @NonNull public DataBuf.Mutable mutableCopyOf(@NonNull @NonNull DataBuf dataBuf)
      Creates a mutable copy of the given data buffer. The copied variant of the buffer will start the read and write process from the first byte rather than re-using the current index of the original buffer.

      A factory is only expected to be able to copy a buffer created by it. Cross factory copy might be possible but is not a requirement. Use factory.createOf(buffer.toByteArray) if you want to be sure that the buffer can be copied.

      Specified by:
      mutableCopyOf in interface DataBufFactory
      Parameters:
      dataBuf - the buffer to copy.
      Returns:
      a copied variant of the given buffer.
    • createWithExpectedSize

      @NonNull public DataBuf.Mutable createWithExpectedSize(int byteSize)
      Creates an empty mutable data buffer which pre-allocates the specified amount of expected bytes rather than dynamically growing during write operations.
      Specified by:
      createWithExpectedSize in interface DataBufFactory
      Parameters:
      byteSize - the expected amount of bytes which get written.
      Returns:
      a new, mutable buffer with the given amount of bytes pre-allocated.