Package io.micronaut.core.io.buffer
Class ReadBufferFactory
java.lang.Object
io.micronaut.core.io.buffer.ReadBufferFactory
- Direct Known Subclasses:
NettyReadBufferFactory
Factory for
ReadBuffers.- Since:
- 4.10.0
- Author:
- Jonas Konrad
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceWrapper around aOutputStreamthat buffers into aReadBuffer. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreate 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<@NonNull ? 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 ReadBufferFactoryGet the defaultReadBufferFactorybacked by JDK-only data structures.Create a newOutputStreamthat buffers into aReadBuffer.
-
Constructor Details
-
ReadBufferFactory
Internal constructor. Extension is only allowed in micronaut-core.
-
-
Method Details
-
getJdkFactory
Get the defaultReadBufferFactorybacked by JDK-only data structures.- Returns:
- The factory
-
createEmpty
Create an emptyReadBuffer.- Returns:
- An empty buffer
-
copyOf
@NonNull public @NonNull ReadBuffer copyOf(@NonNull @NonNull CharSequence cs, @NonNull @NonNull Charset charset) Create a new buffer containing the given text.- Parameters:
cs- The input textcharset- The charset to use for encoding- Returns:
- The text buffer
-
copyOf
Create a new buffer containing all data from the given stream. This is a blocking operation.- Parameters:
stream- The stream to read from- Returns:
- The buffer
- Throws:
IOException
-
copyOf
Create a buffer, copying the given input data.- Parameters:
nioBuffer- A NIO buffer to read data from- Returns:
- The copied 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.- Parameters:
nioBuffer- A NIO buffer- 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. If the input buffer isreference counted, release ownership also transfers to this class.- Parameters:
buffer- A buffer- 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 array transfers to this class, so it should not be modified elsewhere after this method is called.- Parameters:
array- A byte array- Returns:
- The adapted buffer
-
buffer
@NonNull public <T extends Throwable> @NonNull ReadBuffer buffer(@NonNull @NonNull ThrowingConsumer<@NonNull ? super OutputStream, T> writer) throws TWrite to a new buffer using anOutputStream. When the given writer completes, the written data is combined into aReadBufferthat is then returned.- 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
Create 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,
buffer(ThrowingConsumer)may be a bit more convenient, but this method offers more control over the stream lifecycle.- Returns:
- The
ReadBufferFactory.BufferingOutputStreamwrapper
-
compose
@NonNull public @NonNull ReadBuffer compose(@NonNull @NonNull Iterable<@NonNull ReadBuffer> buffers) 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.- Parameters:
buffers- The input buffers to compose- Returns:
- The composite buffer
- Throws:
IllegalStateException- If any given buffer is already closed or consumed
-