Class ByteBodyFactory

java.lang.Object
io.micronaut.http.body.ByteBodyFactory

public class ByteBodyFactory extends Object
Factory methods for ByteBodys.

While this is public API, extension is only allowed by micronaut-core.

Since:
4.8.0
  • Constructor Details

    • ByteBodyFactory

      @Internal protected ByteBodyFactory(@NonNull @NonNull io.micronaut.core.io.buffer.ByteBufferFactory<?,?> byteBufferFactory, io.micronaut.core.io.buffer.ReadBufferFactory readBufferFactory)
      Internal constructor.
      Parameters:
      byteBufferFactory - The buffer factory
      readBufferFactory - The read buffer factory
  • Method Details

    • createDefault

      @NonNull public static @NonNull ByteBodyFactory createDefault(@NonNull @NonNull io.micronaut.core.io.buffer.ByteBufferFactory<?,?> byteBufferFactory)
      Create a default body factory. Where possible, prefer using an existing factory that may have runtime-specific optimizations, such as the factory passed to ResponseBodyWriter.
      Parameters:
      byteBufferFactory - The base buffer factory
      Returns:
      The body factory
    • byteBufferFactory

      @NonNull @Deprecated public final @NonNull io.micronaut.core.io.buffer.ByteBufferFactory<?,?> byteBufferFactory()
      Deprecated.
      Get the underlying ByteBufferFactory. Where possible, prefer using methods on the body factory directly.
      Returns:
      The buffer factory
    • readBufferFactory

      @NonNull public @NonNull io.micronaut.core.io.buffer.ReadBufferFactory readBufferFactory()
      Get the underlying ReadBufferFactory.
      Returns:
      The factory
      Since:
      4.10.0
    • adapt

      @NonNull public @NonNull CloseableAvailableByteBody adapt(@NonNull @NonNull io.micronaut.core.io.buffer.ByteBuffer<?> buffer)
      Create a new CloseableAvailableByteBody from the given buffer. Ownership of the buffer is transferred to this method; the original buffer may be copied or used as-is depending on implementation. If the buffer is ReferenceCounted, release ownership is also transferred to this method.
      Parameters:
      buffer - The buffer
      Returns:
      A ByteBody with the same content as the buffer
    • adapt

      @NonNull public @NonNull CloseableAvailableByteBody adapt(byte @NonNull [] array)
      Create a new CloseableAvailableByteBody from the given array. Ownership of the array is transferred to this method; the array may be copied or used as-is, so do not modify the array after passing it to this method.
      Parameters:
      array - The array
      Returns:
      A ByteBody with the same content as the array
    • adapt

      @NonNull public @NonNull CloseableAvailableByteBody adapt(@NonNull @NonNull io.micronaut.core.io.buffer.ReadBuffer readBuffer)
      Create a new CloseableAvailableByteBody from the given buffer. Ownership of the buffer is transferred to this method.
      Parameters:
      readBuffer - The buffer
      Returns:
      A ByteBody with the same content as the buffer
      Since:
      4.10.0
    • buffer

      @NonNull public <T extends Throwable> @NonNull CloseableAvailableByteBody buffer(@NonNull @NonNull io.micronaut.core.util.functional.ThrowingConsumer<? super OutputStream,T> writer) throws T
      Buffer any data written to an OutputStream and return it as a ByteBody.
      Type Parameters:
      T - Exception type thrown by the consumer
      Parameters:
      writer - The function that will write to the OutputStream
      Returns:
      The data written to the stream
      Throws:
      T - Exception thrown by the consumer
    • createEmpty

      @NonNull public @NonNull CloseableAvailableByteBody createEmpty()
      Create an empty body.
      Returns:
      The empty body
    • copyOf

      @NonNull public @NonNull CloseableAvailableByteBody copyOf(@NonNull @NonNull CharSequence cs, @NonNull @NonNull Charset charset)
      Encode the given CharSequence and create a ByteBody from it.
      Parameters:
      cs - The input string
      charset - The charset to use for encoding
      Returns:
      The encoded body
    • copyOf

      @NonNull public @NonNull CloseableAvailableByteBody copyOf(@NonNull @NonNull InputStream stream) throws IOException
      Copy the data of the given InputStream into an available ByteBody. If the input is blocking, this method will also block.
      Parameters:
      stream - The input to copy
      Returns:
      A body containing the data read from the input
      Throws:
      IOException - Any exception thrown by the InputStream read methods
    • createStreamingBody

      @Internal @NonNull public @NonNull ByteBodyFactory.StreamingBody createStreamingBody(@NonNull @NonNull BodySizeLimits limits, @NonNull BufferConsumer.Upstream upstream)
      Create a new streaming body to push data into. Internal API.
      Parameters:
      limits - The input limits
      upstream - The upstream for backpressure
      Returns:
      The streaming body tuple
    • createBodyAdapter

      @Internal protected AbstractBodyAdapter createBodyAdapter(@NonNull @NonNull org.reactivestreams.Publisher<io.micronaut.core.io.buffer.ReadBuffer> publisher, @Nullable @Nullable Runnable onDiscard)
      Create a new body adapter for transforming a publisher into a ByteBody. Internal API.
      Parameters:
      publisher - The publisher to transform
      onDiscard - Optional runnable to run on BufferConsumer.Upstream.allowDiscard()
      Returns:
      The adapter
    • adapt

      @NonNull public @NonNull CloseableByteBody adapt(@NonNull @NonNull org.reactivestreams.Publisher<io.micronaut.core.io.buffer.ReadBuffer> publisher)
      Create a new ByteBody that streams the given input buffers.
      Parameters:
      publisher - The input buffer publisher
      Returns:
      The combined ByteBody
    • adapt

      @NonNull public @NonNull CloseableByteBody adapt(@NonNull @NonNull org.reactivestreams.Publisher<io.micronaut.core.io.buffer.ReadBuffer> publisher, @NonNull @NonNull BodySizeLimits sizeLimits, @Nullable @Nullable HttpHeaders headersForLength, @Nullable @Nullable Runnable onDiscard)
      Create a new ByteBody that streams the given input buffers.
      Parameters:
      publisher - The input buffer publisher
      sizeLimits - The input size limit
      headersForLength - A HttpHeaders object to introspect for determining the ByteBody.expectedLength()
      onDiscard - An optional Runnable to run when the body is discarded
      Returns:
      The combined ByteBody
    • adapt

      @NonNull public @NonNull CloseableByteBody adapt(@NonNull @NonNull org.reactivestreams.Publisher<io.micronaut.core.io.buffer.ReadBuffer> publisher, @NonNull @NonNull OptionalLong contentLength)
      Create a new ByteBody that streams the given input buffers.
      Parameters:
      publisher - The input buffer publisher
      contentLength - The optional content length for ByteBody.expectedLength()
      Returns:
      The combined ByteBody
    • toStreaming

      @Internal public BaseStreamingByteBody<?> toStreaming(@NonNull @NonNull ByteBody body)
      Convert a ByteBody into a BaseStreamingByteBody with the same content. Internal API.
      Parameters:
      body - The body to convert
      Returns:
      A streaming body with the same content