Package io.micronaut.http.body
Class ByteBodyFactory
java.lang.Object
io.micronaut.http.body.ByteBodyFactory
- Direct Known Subclasses:
NettyByteBodyFactory
Factory methods for
ByteBodys.
While this is public API, extension is only allowed by micronaut-core.
- Since:
- 4.8.0
- Author:
- Jonas Konrad
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedByteBodyFactory(@NonNull ByteBufferFactory<?, ?> byteBufferFactory) Internal constructor. -
Method Summary
Modifier and TypeMethodDescriptionCreate a newCloseableAvailableByteBodyfrom the given array.adapt(@NonNull ByteBuffer<?> buffer) Create a newCloseableAvailableByteBodyfrom the given buffer.<T extends Throwable>
@NonNull CloseableAvailableByteBodybuffer(@NonNull ThrowingConsumer<? super OutputStream, T> writer) Buffer any data written to anOutputStreamand return it as aByteBody.final @NonNull ByteBufferFactory<?,?> Get the underlyingByteBufferFactory.copyOf(@NonNull InputStream stream) Copy the data of the givenInputStreaminto an availableByteBody.copyOf(@NonNull CharSequence cs, @NonNull Charset charset) Encode the givenCharSequenceand create aByteBodyfrom it.static @NonNull ByteBodyFactorycreateDefault(@NonNull ByteBufferFactory<?, ?> byteBufferFactory) Create a default body factory.Create an empty body.
-
Constructor Details
-
ByteBodyFactory
Internal constructor.- Parameters:
byteBufferFactory- The 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
Get the underlyingByteBufferFactory. Where possible, prefer using methods on the body factory directly.- Returns:
- The buffer factory
-
adapt
Create a newCloseableAvailableByteBodyfrom 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
ByteBodywith the same content as the buffer
-
adapt
Create a newCloseableAvailableByteBodyfrom 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
ByteBodywith the same content as the array
-
buffer
@NonNull public <T extends Throwable> @NonNull CloseableAvailableByteBody buffer(@NonNull @NonNull ThrowingConsumer<? super OutputStream, T> writer) throws TBuffer any data written to anOutputStreamand 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 givenCharSequenceand create aByteBodyfrom 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 givenInputStreaminto 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 theInputStreamread methods
-