java.lang.Object
java.io.OutputStream
io.jstach.jstachio.output.ByteBufferedOutputStream
- All Implemented Interfaces:
Output<RuntimeException>,BufferedEncodedOutput,ByteBufferEncodedOutput,Output.CloseableEncodedOutput<RuntimeException>,Output.EncodedOutput<RuntimeException>,Closeable,Flushable,AutoCloseable
A custom OutputStream that is designed for generating bytes from pre-encoded output as
well as reused carefully either by threadlocals or some other pooling
mechanism.
If the buffer is to be reused close() should be called first before it is used
or after every time it is used and toBuffer() should be called to get a
correct view of the internal buffer.
This is basically the same as Joobys Rockers byte buffer but as an OutputStream because JStachio wants that interface. Consequently this code was heavily inspired from Jooby's custom Rocker Output.
Apache License Version 2.0 https://jooby.io/LICENSE.txt Copyright 2014 Edgar Espina
- Author:
- agentgt, jknack
-
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 -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]The buffer where data is stored.static final intDefault buffer size:4k.protected final CharsetThe expected charset of the output.protected intThe number of valid bytes in the buffer. -
Constructor Summary
ConstructorsConstructorDescriptionCreates buffered stream of buffer initial size: 4096.ByteBufferedOutputStream(int bufferSize) Creates buffered stream of given size.ByteBufferedOutputStream(int bufferSize, Charset charset) Creates buffered Output of given size and given charset if used as a Jstachio Output. -
Method Summary
Modifier and TypeMethodDescription<E extends Exception>
voidaccept(OutputConsumer<E> consumer) Transfers the entire buffered output to a consumervoidAnalogous toAppendable.append(CharSequence)which by default treats the String as a CharSequence.Gets a byte buffer view of the data.charset()The charset that the encoded output should be.voidclose()Signals that the buffer should be reset for reuse or destroyed.booleanIf this instance can be reused afterBufferedEncodedOutput.close()is called.intsize()How many bytes have been written so far.toBuffer()Get a view of the internal byte buffer.byte[]Copy internal byte array into a new array.voidtransferTo(OutputStream stream) Transfers the entire buffered output by writing to an OutputStream.voidwrite(byte[] bytes) Analogous toOutputStream.write(byte[]).voidwrite(byte[] bytes, int off, int len) Analogous toOutputStream.write(byte[], int, int).voidwrite(int b) Methods inherited from class java.io.OutputStream
flush, nullOutputStreamMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.jstach.jstachio.output.BufferedEncodedOutput
append, bufferSizeHint, limitMethods inherited from interface io.jstach.jstachio.output.ByteBufferEncodedOutput
asReadableByteChannelMethods inherited from interface io.jstach.jstachio.Output
append, append, append, append, append, toAppendableMethods inherited from interface io.jstach.jstachio.Output.EncodedOutput
append, append
-
Field Details
-
BUFFER_SIZE
Default buffer size:4k.- See Also:
-
buf
The buffer where data is stored. -
count
The number of valid bytes in the buffer. -
charset
The expected charset of the output.
-
-
Constructor Details
-
ByteBufferedOutputStream
Creates buffered stream of given size.- Parameters:
bufferSize- initial size.
-
ByteBufferedOutputStream
Creates buffered Output of given size and given charset if used as a Jstachio Output.- Parameters:
bufferSize- initial size.charset- the charset of the output
-
ByteBufferedOutputStream
public ByteBufferedOutputStream()Creates buffered stream of buffer initial size: 4096.
-
-
Method Details
-
close
Description copied from interface:BufferedEncodedOutputSignals that the buffer should be reset for reuse or destroyed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBufferedEncodedOutput- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceOutput.CloseableEncodedOutput<RuntimeException>- Overrides:
closein classOutputStream
-
charset
Description copied from interface:Output.EncodedOutputThe charset that the encoded output should be.- Specified by:
charsetin interfaceOutput.EncodedOutput<RuntimeException>- Returns:
- expected charset
-
write
Description copied from interface:Output.EncodedOutputAnalogous toOutputStream.write(byte[]). Implementations should not alter the byte array.- Specified by:
writein interfaceOutput.EncodedOutput<RuntimeException>- Overrides:
writein classOutputStream- Parameters:
bytes- already encoded bytes
-
write
Description copied from interface:Output.EncodedOutputAnalogous toOutputStream.write(byte[], int, int). Generated templates do not call this method as great care as to be taken to preserve the encoding. It is only provided in the case of future found optimizations and is not currently required.The default implementation creates an array copies the data and then calls
Output.EncodedOutput.write(byte[]).- Specified by:
writein interfaceOutput.EncodedOutput<RuntimeException>- Overrides:
writein classOutputStream- Parameters:
bytes- already encoded bytesoff- offsetlen- length to copy
-
append
Description copied from interface:OutputAnalogous toAppendable.append(CharSequence)which by default treats the String as a CharSequence.- Specified by:
appendin interfaceBufferedEncodedOutput- Specified by:
appendin interfaceOutput<RuntimeException>- Specified by:
appendin interfaceOutput.EncodedOutput<RuntimeException>- Parameters:
s- unlike appendable always non null.
-
size
How many bytes have been written so far.- Specified by:
sizein interfaceBufferedEncodedOutput- Returns:
- 0 if empty, otherwise how many bytes so far
- See Also:
-
toByteArray
Copy internal byte array into a new array.- Specified by:
toByteArrayin interfaceBufferedEncodedOutput- Returns:
- Byte array.
-
toBuffer
Get a view of the internal byte buffer. Care must be taken if this instance is to be reused in multithreaded environment!- Returns:
- Byte buffer.
-
asByteBuffer
Description copied from interface:ByteBufferEncodedOutputGets a byte buffer view of the data.- Specified by:
asByteBufferin interfaceByteBufferEncodedOutput- Returns:
- byte buffer
-
write
- Specified by:
writein classOutputStream
-
transferTo
Description copied from interface:BufferedEncodedOutputTransfers the entire buffered output by writing to an OutputStream.- Specified by:
transferToin interfaceBufferedEncodedOutput- Parameters:
stream- not null and will not be closed or flushed.- Throws:
IOException- if the stream throws an IOException.- See Also:
-
accept
Description copied from interface:BufferedEncodedOutputTransfers the entire buffered output to a consumer- Specified by:
acceptin interfaceBufferedEncodedOutput- Type Parameters:
E- the exception type- Parameters:
consumer- not null.- Throws:
E- if the consumer throws an exception- See Also:
-
isReusable
Description copied from interface:BufferedEncodedOutputIf this instance can be reused afterBufferedEncodedOutput.close()is called.- Specified by:
isReusablein interfaceBufferedEncodedOutput- Returns:
- true if reuse is allowed by default false is returned.
-