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
Modifier and TypeFieldDescriptionprotected byte[]
The buffer where data is stored.static final int
Default buffer size:4k
.protected final Charset
The expected charset of the output.protected int
The number of valid bytes in the buffer. -
Constructor Summary
ConstructorDescriptionCreates 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 consumervoid
Analogous 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.void
close()
Signals that the buffer should be reset for reuse or destroyed.boolean
If this instance can be reused afterBufferedEncodedOutput.close()
is called.int
size()
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.void
transferTo
(OutputStream stream) Transfers the entire buffered output by writing to an OutputStream.void
write
(byte[] bytes) Analogous toOutputStream.write(byte[])
.void
write
(byte[] bytes, int off, int len) Analogous toOutputStream.write(byte[], int, int)
.void
write
(int b) Methods inherited from class java.io.OutputStream
flush, nullOutputStream
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jstach.jstachio.output.BufferedEncodedOutput
append, bufferSizeHint, limit
Methods inherited from interface io.jstach.jstachio.output.ByteBufferEncodedOutput
asReadableByteChannel
Methods inherited from interface io.jstach.jstachio.Output
append, append, append, append, append, toAppendable
Methods 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:BufferedEncodedOutput
Signals that the buffer should be reset for reuse or destroyed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBufferedEncodedOutput
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceOutput.CloseableEncodedOutput<RuntimeException>
- Overrides:
close
in classOutputStream
-
charset
Description copied from interface:Output.EncodedOutput
The charset that the encoded output should be.- Specified by:
charset
in interfaceOutput.EncodedOutput<RuntimeException>
- Returns:
- expected charset
-
write
Description copied from interface:Output.EncodedOutput
Analogous toOutputStream.write(byte[])
. Implementations should not alter the byte array.- Specified by:
write
in interfaceOutput.EncodedOutput<RuntimeException>
- Overrides:
write
in classOutputStream
- Parameters:
bytes
- already encoded bytes
-
write
Description copied from interface:Output.EncodedOutput
Analogous 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:
write
in interfaceOutput.EncodedOutput<RuntimeException>
- Overrides:
write
in classOutputStream
- Parameters:
bytes
- already encoded bytesoff
- offsetlen
- length to copy
-
append
Description copied from interface:Output
Analogous toAppendable.append(CharSequence)
which by default treats the String as a CharSequence.- Specified by:
append
in interfaceBufferedEncodedOutput
- Specified by:
append
in interfaceOutput<RuntimeException>
- Specified by:
append
in interfaceOutput.EncodedOutput<RuntimeException>
- Parameters:
s
- unlike appendable always non null.
-
size
How many bytes have been written so far.- Specified by:
size
in interfaceBufferedEncodedOutput
- Returns:
- 0 if empty, otherwise how many bytes so far
- See Also:
-
toByteArray
Copy internal byte array into a new array.- Specified by:
toByteArray
in 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:ByteBufferEncodedOutput
Gets a byte buffer view of the data.- Specified by:
asByteBuffer
in interfaceByteBufferEncodedOutput
- Returns:
- byte buffer
-
write
- Specified by:
write
in classOutputStream
-
transferTo
Description copied from interface:BufferedEncodedOutput
Transfers the entire buffered output by writing to an OutputStream.- Specified by:
transferTo
in 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:BufferedEncodedOutput
Transfers the entire buffered output to a consumer- Specified by:
accept
in 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:BufferedEncodedOutput
If this instance can be reused afterBufferedEncodedOutput.close()
is called.- Specified by:
isReusable
in interfaceBufferedEncodedOutput
- Returns:
- true if reuse is allowed by default false is returned.
-