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 TypeMethodDescriptionadapt(byte[] array) Create a buffer with the given input data.adapt(ByteBuffer<?> buffer) Create a buffer with the given input data.adapt(io.netty.buffer.ByteBuf buffer) Create a buffer with the given input data.adapt(ByteBuffer nioBuffer) Create a buffer with the given input data.<T extends Throwable>
ReadBufferbuffer(ThrowingConsumer<? super OutputStream, T> writer) Write to a new buffer using anOutputStream.compose(Iterable<ReadBuffer> buffers) Create a new composite buffer out of the given collection of buffers.copyOf(InputStream stream) Create a new buffer containing all data from the given stream.copyOf(CharSequence cs, Charset charset) Create a new buffer containing the given text.copyOf(ByteBuffer nioBuffer) Create a buffer, copying the given input data.Create an emptyReadBuffer.static NettyReadBufferFactoryof(io.netty.buffer.ByteBufAllocator allocator) Get a buffer factory associated with the given allocator.Create a newOutputStreamthat buffers into aReadBuffer.static io.netty.buffer.ByteBuftoByteBuf(ReadBuffer readBuffer) Convert the givenReadBufferto a nettyByteBuf.Methods inherited from class io.micronaut.core.io.buffer.ReadBufferFactory
getJdkFactory
-
Method Details
-
of
Get a buffer factory associated with the given allocator.- Parameters:
allocator- The allocator to use- Returns:
- The buffer factory
-
createEmpty
Description copied from class:ReadBufferFactoryCreate an emptyReadBuffer.- Overrides:
createEmptyin classReadBufferFactory- Returns:
- An empty buffer
-
copyOf
Description copied from class:ReadBufferFactoryCreate a new buffer containing the given text.- Overrides:
copyOfin classReadBufferFactory- Parameters:
cs- The input textcharset- The charset to use for encoding- Returns:
- The text buffer
-
copyOf
Description copied from class:ReadBufferFactoryCreate a new buffer containing all data from the given stream. This is a blocking operation.- Overrides:
copyOfin classReadBufferFactory- Parameters:
stream- The stream to read from- Returns:
- The buffer
- Throws:
IOException
-
copyOf
Description copied from class:ReadBufferFactoryCreate a buffer, copying the given input data.- Overrides:
copyOfin classReadBufferFactory- Parameters:
nioBuffer- A NIO buffer to read data from- Returns:
- The copied buffer
-
adapt
Description copied from class:ReadBufferFactoryCreate 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:
adaptin classReadBufferFactory- Parameters:
nioBuffer- A NIO buffer- Returns:
- The adapted buffer
-
adapt
Description copied from class:ReadBufferFactoryCreate 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:
adaptin classReadBufferFactory- Parameters:
buffer- A buffer- Returns:
- The adapted buffer
-
adapt
Description copied from class:ReadBufferFactoryCreate 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:
adaptin 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
Convert the givenReadBufferto a nettyByteBuf. This is a consuming operation.- Parameters:
readBuffer- The buffer to read from- Returns:
- The read data
-
buffer
public <T extends Throwable> ReadBuffer buffer(ThrowingConsumer<? super OutputStream, T> writer) throws TDescription copied from class:ReadBufferFactoryWrite to a new buffer using anOutputStream. When the given writer completes, the written data is combined into aReadBufferthat is then returned.- Overrides:
bufferin 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:ReadBufferFactoryCreate a newOutputStreamthat buffers into aReadBuffer. Used like this:CloseableAvailableByteBody body; try (BufferingOutputStream bos = byteBodyFactory.outputStreamBuffer()) { bos.stream().write(123); // ... body = bos.finishBuffer(); } // use bodyNote 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:
outputStreamBufferin classReadBufferFactory- Returns:
- The
ReadBufferFactory.BufferingOutputStreamwrapper
-
compose
Description copied from class:ReadBufferFactoryCreate 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:
composein classReadBufferFactory- Parameters:
buffers- The input buffers to compose- Returns:
- The composite buffer
-