Class NettyReadBufferFactory

java.lang.Object
io.micronaut.core.io.buffer.ReadBufferFactory
io.micronaut.buffer.netty.NettyReadBufferFactory

public final class NettyReadBufferFactory extends ReadBufferFactory
Netty-based ReadBufferFactory. Also has additional utilities for dealing with netty buffers.
Since:
4.10.0
Author:
Jonas Konrad
  • Method Details

    • of

      public static @NonNull NettyReadBufferFactory of(@NonNull io.netty.buffer.ByteBufAllocator allocator)
      Get a buffer factory associated with the given allocator.
      Parameters:
      allocator - The allocator to use
      Returns:
      The buffer factory
    • createEmpty

      public ReadBuffer createEmpty()
      Description copied from class: ReadBufferFactory
      Create an empty ReadBuffer.
      Overrides:
      createEmpty in class ReadBufferFactory
      Returns:
      An empty buffer
    • copyOf

      public @NonNull ReadBuffer copyOf(@NonNull CharSequence cs, @NonNull Charset charset)
      Description copied from class: ReadBufferFactory
      Create a new buffer containing the given text.
      Overrides:
      copyOf in class ReadBufferFactory
      Parameters:
      cs - The input text
      charset - The charset to use for encoding
      Returns:
      The text buffer
    • copyOf

      public @NonNull ReadBuffer copyOf(@NonNull InputStream stream) throws IOException
      Description copied from class: ReadBufferFactory
      Create a new buffer containing all data from the given stream. This is a blocking operation.
      Overrides:
      copyOf in class ReadBufferFactory
      Parameters:
      stream - The stream to read from
      Returns:
      The buffer
      Throws:
      IOException
    • copyOf

      public @NonNull ReadBuffer copyOf(@NonNull ByteBuffer nioBuffer)
      Description copied from class: ReadBufferFactory
      Create a buffer, copying the given input data.
      Overrides:
      copyOf in class ReadBufferFactory
      Parameters:
      nioBuffer - A NIO buffer to read data from
      Returns:
      The copied buffer
    • adapt

      public @NonNull ReadBuffer adapt(@NonNull ByteBuffer nioBuffer)
      Description copied from class: ReadBufferFactory
      Create a buffer with the given input data. Whether the data is copied or used as-is is implementation-defined. Ownership of the given buffer transfers to this class, so it should not be modified elsewhere after this method is called.
      Overrides:
      adapt in class ReadBufferFactory
      Parameters:
      nioBuffer - A NIO buffer
      Returns:
      The adapted buffer
    • adapt

      public @NonNull ReadBuffer adapt(@NonNull ByteBuffer<?> buffer)
      Description copied from class: ReadBufferFactory
      Create a buffer with the given input data. Whether the data is copied or used as-is is implementation-defined. Ownership of the given buffer transfers to this class, so it should not be modified elsewhere after this method is called. If the input buffer is reference counted, release ownership also transfers to this class.
      Overrides:
      adapt in class ReadBufferFactory
      Parameters:
      buffer - A buffer
      Returns:
      The adapted buffer
    • adapt

      public ReadBuffer adapt(byte @NonNull [] array)
      Description copied from class: ReadBufferFactory
      Create a buffer with the given input data. Whether the data is copied or used as-is is implementation-defined. Ownership of the given array transfers to this class, so it should not be modified elsewhere after this method is called.
      Overrides:
      adapt in class ReadBufferFactory
      Parameters:
      array - A byte array
      Returns:
      The adapted buffer
    • adapt

      public @NonNull ReadBuffer adapt(@NonNull io.netty.buffer.ByteBuf buffer)
      Create a buffer with the given input data. Whether the data is copied or used as-is is implementation-defined. Ownership of the given buffer transfers to this class, so it should not be modified elsewhere after this method is called. Release ownership also transfers to this class.
      Parameters:
      buffer - A buffer
      Returns:
      The adapted buffer
    • toByteBuf

      public static @NonNull io.netty.buffer.ByteBuf toByteBuf(@NonNull ReadBuffer readBuffer)
      Convert the given ReadBuffer to a netty ByteBuf. This is a consuming operation.
      Parameters:
      readBuffer - The buffer to read from
      Returns:
      The read data
    • buffer

      public <T extends Throwable> @NonNull ReadBuffer buffer(@NonNull ThrowingConsumer<? super OutputStream,T> writer) throws T
      Description copied from class: ReadBufferFactory
      Write to a new buffer using an OutputStream. When the given writer completes, the written data is combined into a ReadBuffer that is then returned.
      Overrides:
      buffer in class ReadBufferFactory
      Type Parameters:
      T - An exception thrown by the writer
      Parameters:
      writer - The writer
      Returns:
      The written data
      Throws:
      T - If the writer throws an exception
    • outputStreamBuffer

      public @NonNull ReadBufferFactory.BufferingOutputStream outputStreamBuffer()
      Description copied from class: ReadBufferFactory
      Create a new OutputStream that buffers into a ReadBuffer. Used like this:
      
       CloseableAvailableByteBody body;
       try (BufferingOutputStream bos = byteBodyFactory.outputStreamBuffer()) {
           bos.stream().write(123);
           // ...
           body = bos.finishBuffer();
       }
       // use body
       

      Note that for simple use cases, ReadBufferFactory.buffer(ThrowingConsumer) may be a bit more convenient, but this method offers more control over the stream lifecycle.

      Overrides:
      outputStreamBuffer in class ReadBufferFactory
      Returns:
      The ReadBufferFactory.BufferingOutputStream wrapper
    • compose

      public @NonNull ReadBuffer compose(@NonNull Iterable<@NonNull ReadBuffer> buffers)
      Description copied from class: ReadBufferFactory
      Create a new composite buffer out of the given collection of buffers. This operation consumes all input buffers, even if there is an exception along the way.
      Overrides:
      compose in class ReadBufferFactory
      Parameters:
      buffers - The input buffers to compose
      Returns:
      The composite buffer