Class ReadBufferFactory

java.lang.Object
io.micronaut.core.io.buffer.ReadBufferFactory
Direct Known Subclasses:
NettyReadBufferFactory

public class ReadBufferFactory extends Object
Factory for ReadBuffers.
Since:
4.10.0
Author:
Jonas Konrad
  • Constructor Details

    • ReadBufferFactory

      @Internal protected ReadBufferFactory()
      Internal constructor. Extension is only allowed in micronaut-core.
  • Method Details

    • getJdkFactory

      public static ReadBufferFactory getJdkFactory()
      Get the default ReadBufferFactory backed by JDK-only data structures.
      Returns:
      The factory
    • createEmpty

      public ReadBuffer createEmpty()
      Create an empty ReadBuffer.
      Returns:
      An empty buffer
    • copyOf

      public ReadBuffer copyOf(CharSequence cs, Charset charset)
      Create a new buffer containing the given text.
      Parameters:
      cs - The input text
      charset - The charset to use for encoding
      Returns:
      The text buffer
    • copyOf

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

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

      public ReadBuffer adapt(ByteBuffer nioBuffer)
      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.
      Parameters:
      nioBuffer - A NIO buffer
      Returns:
      The adapted buffer
    • adapt

      public ReadBuffer adapt(ByteBuffer<?> 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. If the input buffer is reference counted, release ownership also transfers to this class.
      Parameters:
      buffer - A buffer
      Returns:
      The adapted buffer
    • adapt

      public ReadBuffer adapt(byte[] array)
      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.
      Parameters:
      array - A byte array
      Returns:
      The adapted buffer
    • buffer

      public <T extends Throwable> ReadBuffer buffer(ThrowingConsumer<? super OutputStream,T> writer) throws T
      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.
      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 ReadBufferFactory.BufferingOutputStream outputStreamBuffer()
      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, buffer(ThrowingConsumer) may be a bit more convenient, but this method offers more control over the stream lifecycle.

      Returns:
      The ReadBufferFactory.BufferingOutputStream wrapper
    • compose

      public ReadBuffer compose(Iterable<ReadBuffer> buffers)
      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.
      Parameters:
      buffers - The input buffers to compose
      Returns:
      The composite buffer
      Throws:
      IllegalStateException - If any given buffer is already closed or consumed