- All Superinterfaces:
AutoCloseable
,Output<RuntimeException>
,Output.CloseableEncodedOutput<RuntimeException>
,Output.EncodedOutput<RuntimeException>
- All Known Subinterfaces:
ByteBufferEncodedOutput
,ChunkEncodedOutput<T>
- All Known Implementing Classes:
ByteBufferedOutputStream
public sealed interface BufferedEncodedOutput
extends Output.CloseableEncodedOutput<RuntimeException>
permits ChunkEncodedOutput<T>, ByteBufferEncodedOutput
An encoded output that will store the output in its preferred memory structure and can
then be copied to an
OutputStream
or read from a ReadableByteChannel
.
The total size()
of the output can also be retrieved before being copied which
is useful for setting "Content-Length
" HTTP header or for allocating
buffers.
The major impetus for this interface is needing the length of the output prior to
outputing. If the length is not needed before writing then better performance and
memory savings can probably be had by just using
Output.EncodedOutput.of(OutputStream, Charset)
.
In general instances will be empty and not have correct results till
Template.EncodedTemplate.write(Object, EncodedOutput)
is called.
Currently there are two approaches to buffered output:
ByteBufferEncodedOutput
: according to benchmarks on most platforms other than Windows it is more CPU friendly but less memory friendly. It is ideal for platforms where you can returnByteBuffer
directly or need to return abyte[]
. The default implementation uses a growing array and can be created withByteBufferEncodedOutput.ofByteArray(Charset, int)
and can be reused providedclose()
is called. Reuse can be useful if using ThreadLocals or some other pooling mechanism.ChunkEncodedOutput
: according to benchmarks is more optimized for memory savings as well as possible reduction of copying. This approach originated from Rocker's purported "near zero copy".
Output.EncodedOutput
that uses the platform's
underlying buffer pool will probably yield the best results.- Author:
- agentgt
- See Also:
- API Note
- Methods starting with "
as
" are a view where as methods starting with "to
" (or ending in "To" for the case of transferTo) are a copy. If the output is to be reused "as
" methods should be copied or fully used before the output is reused.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.jstach.jstachio.Output
Output.CloseableEncodedOutput<E extends Exception>, Output.EncodedOutput<E extends Exception>, Output.StringOutput
-
Method Summary
Modifier and TypeMethodDescription<E extends Exception>
voidaccept
(OutputConsumer<E> consumer) Transfers the entire buffered output to a consumerdefault void
Analogous toAppendable.append(CharSequence)
.default void
Analogous toAppendable.append(CharSequence)
which by default treats the String as a CharSequence.Represents the encoded output as readable channel.default int
The recommend buffer size to use for extracting withasReadableByteChannel()
ortransferTo(OutputStream)
.void
close()
Signals that the buffer should be reset for reuse or destroyed.default boolean
If this instance can be reused afterclose()
is called.default LimitEncodedOutput<OutputStream,
IOException> limit
(int limit, OutputFactory<OutputStream, IOException> factory) Decorates this buffer so that buffering is limited to certain amount and will eventually send all output to the OutputStream created by the factory.static BufferedEncodedOutput
Create a buffered encoded output backed by a sequence of chunks.int
size()
Total size in number of bytes of the output.default byte[]
Copies the output to a byte array.default void
transferTo
(OutputStream stream) Transfers the entire buffered output by writing to an OutputStream.
-
Method Details
-
size
int size()Total size in number of bytes of the output.- Returns:
- size
- See Also:
-
transferTo
Transfers the entire buffered output by writing to an OutputStream.- Parameters:
stream
- not null and will not be closed or flushed.- Throws:
IOException
- if the stream throws an IOException.- See Also:
- API Note
- For nonblocking
asReadableByteChannel()
is generally accepted as the better aproach as it is a pull model.
-
limit
default LimitEncodedOutput<OutputStream,IOException> limit(int limit, OutputFactory<OutputStream, IOException> factory) Decorates this buffer so that buffering is limited to certain amount and will eventually send all output to the OutputStream created by the factory. The factory will be passed-1
if the limit is exceeded and create will only be called once and only once provided that the returned object is closed.This method should be called before passed to JStachio and the result is the output that should be passed.
- Parameters:
limit
- the maximum amount of bytes to buffer.factory
- create the output stream on demand and will always be used before close is called.- Returns:
- output that will need to be closed eventually.
- See Also:
-
accept
Transfers the entire buffered output to a consumer- Type Parameters:
E
- the exception type- Parameters:
consumer
- not null.- Throws:
E
- if the consumer throws an exception- See Also:
- API Note
- For nonblocking
asReadableByteChannel()
is generally accepted as the better aproach as it is a pull model.
-
bufferSizeHint
The recommend buffer size to use for extracting withasReadableByteChannel()
ortransferTo(OutputStream)
.- Returns:
- buffer size to use which by default is
size()
.
-
asReadableByteChannel
Represents the encoded output as readable channel. The channel should be closed when finished to signal reuse or destruction of the buffered output!. To possibly help determine the buffer to use forReadableByteChannel.read(java.nio.ByteBuffer)
one can usebufferSizeHint()
orsize()
.- Returns:
- channel open and ready to read from at the start of the output.
- See Also:
-
toByteArray
Copies the output to a byte array.- Returns:
- a copied byte array of the output
-
append
Description copied from interface:Output
Analogous toAppendable.append(CharSequence)
which by default treats the String as a CharSequence.- Specified by:
append
in interfaceOutput<RuntimeException>
- Specified by:
append
in interfaceOutput.EncodedOutput<RuntimeException>
- Parameters:
s
- unlike appendable always non null.
-
append
Description copied from interface:Output
Analogous toAppendable.append(CharSequence)
.- Specified by:
append
in interfaceOutput<RuntimeException>
- Parameters:
s
- unlike appendable always non null.
-
close
void close()Signals that the buffer should be reset for reuse or destroyed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceOutput.CloseableEncodedOutput<RuntimeException>
- API Note
- This does not throw an IOException on purpose since everything is in memory.
-
isReusable
If this instance can be reused afterclose()
is called.- Returns:
- true if reuse is allowed by default false is returned.
-
ofChunked
Create a buffered encoded output backed by a sequence of chunks.- Parameters:
charset
- the expected encoding- Returns:
- buffered output
-