Interface DataBufFactory


public interface DataBufFactory
A factory for creation of data buf instances in various forms.
Since:
4.0
  • Method Details

    • defaultFactory

      @NonNull static @NonNull DataBufFactory defaultFactory()
      Get the default factory for data buffers integrated into cloudnet. This is current a factory which wraps netty byte buffers.

      Returned buffers created by this factory should NEVER get cast to any implementation of a buffer as the factory might change and return different buffers when we should ever decide to use another networking library for CloudNet.

      Returns:
      the default integrated data buf factory.
    • createEmpty

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

      @NonNull @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.
      Parameters:
      bytes - the bytes to wrap.
      Returns:
      a new buffer wrapping the given bytes.
    • copyOf

      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.

      Parameters:
      dataBuf - the buffer to copy.
      Returns:
      a copied variant of the given buffer.
      Throws:
      IllegalArgumentException - if the buffer cannot be copied.
      NullPointerException - if the given buffer is null.
    • mutableCopyOf

      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.

      Parameters:
      dataBuf - the buffer to copy.
      Returns:
      a copied variant of the given buffer.
      Throws:
      IllegalArgumentException - if the buffer cannot be copied.
      NullPointerException - if the given buffer is null.
    • createWithExpectedSize

      @NonNull 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.
      Parameters:
      byteSize - the expected amount of bytes which get written.
      Returns:
      a new, mutable buffer with the given amount of bytes pre-allocated.