- java.lang.Object
-
- java.io.OutputStream
-
- org.omnifaces.io.ResettableBufferedOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
,ResettableBuffer
public class ResettableBufferedOutputStream extends OutputStream implements ResettableBuffer
This resettable buffered output stream will buffer everything until the given threshold buffer size, regardless of flush calls. Only when the specified threshold buffer size is exceeded, or whenclose()
is called, then the buffer will be actually flushed.There is a
reset()
method which enables the developer to reset the buffer, as long as it's not flushed yet, which can be determined byisResettable()
.- Author:
- Bauke Scholtz
- See Also:
ResettableBufferedWriter
-
-
Constructor Summary
Constructors Constructor Description ResettableBufferedOutputStream(int thresholdBufferSize)
Construct a new resettable buffered output stream which forcibly buffers everything until the given threshold buffer size, regardless of flush calls and calls.ResettableBufferedOutputStream(OutputStream output, int thresholdBufferSize)
Construct a new resettable buffered output stream which wraps the given output stream and forcibly buffers everything until the given threshold buffer size, regardless of flush calls.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
protected OutputStream
createOutputStream(boolean thresholdBufferSizeExceeded)
Returns the custom implementation of theOutputStream
.void
flush()
protected int
getWrittenBytes()
Returns the amount of so far written bytes in the threshold buffer.boolean
isResettable()
Returns true if buffer can be reset.void
reset()
Perform a buffer reset.void
write(byte[] bytes)
void
write(byte[] bytes, int offset, int length)
void
write(int b)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
ResettableBufferedOutputStream
public ResettableBufferedOutputStream(int thresholdBufferSize)
Construct a new resettable buffered output stream which forcibly buffers everything until the given threshold buffer size, regardless of flush calls and calls. You need to overridecreateOutputStream(boolean)
when using this constructor.- Parameters:
thresholdBufferSize
- The threshold buffer size.
-
ResettableBufferedOutputStream
public ResettableBufferedOutputStream(OutputStream output, int thresholdBufferSize)
Construct a new resettable buffered output stream which wraps the given output stream and forcibly buffers everything until the given threshold buffer size, regardless of flush calls. You do not need to overridecreateOutputStream(boolean)
when using this constructor.- Parameters:
output
- The wrapped output stream .thresholdBufferSize
- The threshold buffer size.
-
-
Method Detail
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] bytes) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] bytes, int offset, int length) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
createOutputStream
protected OutputStream createOutputStream(boolean thresholdBufferSizeExceeded) throws IOException
Returns the custom implementation of theOutputStream
. This will only be called when the specified threshold buffer size is exceeded, or whenclose()
is called.- Parameters:
thresholdBufferSizeExceeded
- Whether the threshold buffer size has exceeded.- Returns:
- The custom implementation of the
OutputStream
. - Throws:
IOException
- When an I/O error occurs.
-
getWrittenBytes
protected int getWrittenBytes()
Returns the amount of so far written bytes in the threshold buffer. This will be 0 when the threshold buffer is empty and this will be -1 when the threshold has exceeded.- Returns:
- The amount of so far written bytes in the threshold buffer.
-
reset
public void reset()
Description copied from interface:ResettableBuffer
Perform a buffer reset.- Specified by:
reset
in interfaceResettableBuffer
-
flush
public void flush() throws IOException
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
isResettable
public boolean isResettable()
Description copied from interface:ResettableBuffer
Returns true if buffer can be reset.- Specified by:
isResettable
in interfaceResettableBuffer
- Returns:
true
if buffer can be reset, otherwisefalse
.
-
-