Package io.micronaut.http.body
Class ByteBodyFactory
java.lang.Object
io.micronaut.http.body.ByteBodyFactory
- Direct Known Subclasses:
NettyByteBodyFactory
Factory methods for
ByteBody
s.
While this is public API, extension is only allowed by micronaut-core.
- Since:
- 4.8.0
- Author:
- Jonas Konrad
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
Return type forcreateStreamingBody(BodySizeLimits, BufferConsumer.Upstream)
. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ByteBodyFactory
(@NonNull ByteBufferFactory<?, ?> byteBufferFactory, ReadBufferFactory readBufferFactory) Internal constructor. -
Method Summary
Modifier and TypeMethodDescriptionCreate a newCloseableAvailableByteBody
from the given array.adapt
(@NonNull ByteBuffer<?> buffer) Create a newCloseableAvailableByteBody
from the given buffer.adapt
(@NonNull ReadBuffer readBuffer) Create a newCloseableAvailableByteBody
from the given buffer.adapt
(@NonNull Publisher<ReadBuffer> publisher) Create a newByteBody
that streams the given input buffers.adapt
(@NonNull Publisher<ReadBuffer> publisher, @NonNull BodySizeLimits sizeLimits, @Nullable HttpHeaders headersForLength, @Nullable Runnable onDiscard) Create a newByteBody
that streams the given input buffers.adapt
(@NonNull Publisher<ReadBuffer> publisher, @NonNull OptionalLong contentLength) Create a newByteBody
that streams the given input buffers.<T extends Throwable>
@NonNull CloseableAvailableByteBodybuffer
(@NonNull ThrowingConsumer<? super OutputStream, T> writer) Buffer any data written to anOutputStream
and return it as aByteBody
.final @NonNull ByteBufferFactory<?,
?> Deprecated.copyOf
(@NonNull InputStream stream) Copy the data of the givenInputStream
into an availableByteBody
.copyOf
(@NonNull CharSequence cs, @NonNull Charset charset) Encode the givenCharSequence
and create aByteBody
from it.protected AbstractBodyAdapter
createBodyAdapter
(@NonNull Publisher<ReadBuffer> publisher, @Nullable Runnable onDiscard) Create a new body adapter for transforming a publisher into aByteBody
.static @NonNull ByteBodyFactory
createDefault
(@NonNull ByteBufferFactory<?, ?> byteBufferFactory) Create a default body factory.Create an empty body.createStreamingBody
(@NonNull BodySizeLimits limits, BufferConsumer.Upstream upstream) Create a new streaming body to push data into.Get the underlyingReadBufferFactory
.toStreaming
(@NonNull ByteBody body) Convert aByteBody
into aBaseStreamingByteBody
with the same content.
-
Constructor Details
-
ByteBodyFactory
@Internal protected ByteBodyFactory(@NonNull @NonNull ByteBufferFactory<?, ?> byteBufferFactory, ReadBufferFactory readBufferFactory) Internal constructor.- Parameters:
byteBufferFactory
- The buffer factoryreadBufferFactory
- The read buffer factory
-
-
Method Details
-
createDefault
@NonNull public static @NonNull ByteBodyFactory createDefault(@NonNull @NonNull 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 toResponseBodyWriter
.- Parameters:
byteBufferFactory
- The base buffer factory- Returns:
- The body factory
-
byteBufferFactory
Deprecated.Get the underlyingByteBufferFactory
. Where possible, prefer using methods on the body factory directly.- Returns:
- The buffer factory
-
readBufferFactory
Get the underlyingReadBufferFactory
.- Returns:
- The factory
- Since:
- 4.10.0
-
adapt
Create a newCloseableAvailableByteBody
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 isReferenceCounted
, release ownership is also transferred to this method.- Parameters:
buffer
- The buffer- Returns:
- A
ByteBody
with the same content as the buffer
-
adapt
Create a newCloseableAvailableByteBody
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
Create a newCloseableAvailableByteBody
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 ThrowingConsumer<? super OutputStream, T> writer) throws TBuffer any data written to anOutputStream
and return it as aByteBody
.- Type Parameters:
T
- Exception type thrown by the consumer- Parameters:
writer
- The function that will write to theOutputStream
- Returns:
- The data written to the stream
- Throws:
T
- Exception thrown by the consumer
-
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 givenCharSequence
and create aByteBody
from it.- Parameters:
cs
- The input stringcharset
- 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 givenInputStream
into an availableByteBody
. 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 theInputStream
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 limitsupstream
- The upstream for backpressure- Returns:
- The streaming body tuple
-
createBodyAdapter
@Internal protected AbstractBodyAdapter createBodyAdapter(@NonNull @NonNull Publisher<ReadBuffer> publisher, @Nullable @Nullable Runnable onDiscard) Create a new body adapter for transforming a publisher into aByteBody
. Internal API.- Parameters:
publisher
- The publisher to transformonDiscard
- Optional runnable to run onBufferConsumer.Upstream.allowDiscard()
- Returns:
- The adapter
-
adapt
Create a newByteBody
that streams the given input buffers.- Parameters:
publisher
- The input buffer publisher- Returns:
- The combined
ByteBody
-
adapt
@NonNull public @NonNull CloseableByteBody adapt(@NonNull @NonNull Publisher<ReadBuffer> publisher, @NonNull @NonNull BodySizeLimits sizeLimits, @Nullable @Nullable HttpHeaders headersForLength, @Nullable @Nullable Runnable onDiscard) Create a newByteBody
that streams the given input buffers.- Parameters:
publisher
- The input buffer publishersizeLimits
- The input size limitheadersForLength
- AHttpHeaders
object to introspect for determining theByteBody.expectedLength()
onDiscard
- An optionalRunnable
to run when the body isdiscarded
- Returns:
- The combined
ByteBody
-
adapt
@NonNull public @NonNull CloseableByteBody adapt(@NonNull @NonNull Publisher<ReadBuffer> publisher, @NonNull @NonNull OptionalLong contentLength) Create a newByteBody
that streams the given input buffers.- Parameters:
publisher
- The input buffer publishercontentLength
- The optional content length forByteBody.expectedLength()
- Returns:
- The combined
ByteBody
-
toStreaming
Convert aByteBody
into aBaseStreamingByteBody
with the same content. Internal API.- Parameters:
body
- The body to convert- Returns:
- A streaming body with the same content
-
readBufferFactory()