Package io.micronaut.buffer.netty
Class NettyReadBufferFactory
java.lang.Object
io.micronaut.core.io.buffer.ReadBufferFactory
io.micronaut.buffer.netty.NettyReadBufferFactory
Netty-based
ReadBufferFactory
. Also has additional utilities for dealing with netty
buffers.- Since:
- 4.10.0
- Author:
- Jonas Konrad
-
Nested Class Summary
Nested classes/interfaces inherited from class io.micronaut.core.io.buffer.ReadBufferFactory
ReadBufferFactory.BufferingOutputStream
-
Method Summary
Modifier and TypeMethodDescriptionCreate a buffer with the given input data.Create a buffer with the given input data.adapt
(@NonNull ByteBuffer nioBuffer) Create a buffer with the given input data.adapt
(ByteBuffer<?> buffer) Create a buffer with the given input data.<T extends Throwable>
@NonNull ReadBufferbuffer
(@NonNull ThrowingConsumer<? super OutputStream, T> writer) Write to a new buffer using anOutputStream
.compose
(@NonNull Iterable<@NonNull ReadBuffer> buffers) Create a new composite buffer out of the given collection of buffers.copyOf
(@NonNull InputStream stream) Create a new buffer containing all data from the given stream.copyOf
(@NonNull CharSequence cs, @NonNull Charset charset) Create a new buffer containing the given text.copyOf
(@NonNull ByteBuffer nioBuffer) Create a buffer, copying the given input data.Create an emptyReadBuffer
.static @NonNull NettyReadBufferFactory
Get a buffer factory associated with the given allocator.Create a newOutputStream
that buffers into aReadBuffer
.static @NonNull io.netty.buffer.ByteBuf
toByteBuf
(@NonNull ReadBuffer readBuffer) Convert the givenReadBuffer
to a nettyByteBuf
.Methods inherited from class io.micronaut.core.io.buffer.ReadBufferFactory
getJdkFactory
-
Method Details
-
of
@NonNull public static @NonNull NettyReadBufferFactory of(@NonNull @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
Description copied from class:ReadBufferFactory
Create an emptyReadBuffer
.- Overrides:
createEmpty
in classReadBufferFactory
- Returns:
- An empty buffer
-
copyOf
@NonNull public @NonNull ReadBuffer copyOf(@NonNull @NonNull CharSequence cs, @NonNull @NonNull Charset charset) Description copied from class:ReadBufferFactory
Create a new buffer containing the given text.- Overrides:
copyOf
in classReadBufferFactory
- Parameters:
cs
- The input textcharset
- The charset to use for encoding- Returns:
- The text buffer
-
copyOf
Description copied from class:ReadBufferFactory
Create a new buffer containing all data from the given stream. This is a blocking operation.- Overrides:
copyOf
in classReadBufferFactory
- Parameters:
stream
- The stream to read from- Returns:
- The buffer
- Throws:
IOException
-
copyOf
Description copied from class:ReadBufferFactory
Create a buffer, copying the given input data.- Overrides:
copyOf
in classReadBufferFactory
- Parameters:
nioBuffer
- A NIO buffer to read data from- Returns:
- The copied buffer
-
adapt
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 classReadBufferFactory
- Parameters:
nioBuffer
- A NIO buffer- Returns:
- The adapted buffer
-
adapt
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 isreference counted
, release ownership also transfers to this class.- Overrides:
adapt
in classReadBufferFactory
- Parameters:
buffer
- A buffer- Returns:
- The adapted buffer
-
adapt
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 classReadBufferFactory
- Parameters:
array
- A byte array- Returns:
- The adapted buffer
-
adapt
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
@NonNull public static @NonNull io.netty.buffer.ByteBuf toByteBuf(@NonNull @NonNull ReadBuffer readBuffer) Convert the givenReadBuffer
to a nettyByteBuf
. This is a consuming operation.- Parameters:
readBuffer
- The buffer to read from- Returns:
- The read data
-
buffer
@NonNull public <T extends Throwable> @NonNull ReadBuffer buffer(@NonNull @NonNull ThrowingConsumer<? super OutputStream, T> writer) throws TDescription copied from class:ReadBufferFactory
Write to a new buffer using anOutputStream
. When the given writer completes, the written data is combined into aReadBuffer
that is then returned.- Overrides:
buffer
in classReadBufferFactory
- 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
Description copied from class:ReadBufferFactory
Create a newOutputStream
that buffers into aReadBuffer
. 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 classReadBufferFactory
- Returns:
- The
ReadBufferFactory.BufferingOutputStream
wrapper
-
compose
@NonNull public @NonNull ReadBuffer compose(@NonNull @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 classReadBufferFactory
- Parameters:
buffers
- The input buffers to compose- Returns:
- The composite buffer
-