final class CommittingOutputStream
extends java.io.OutputStream
When buffering functionality is enabled the output stream buffers
the written bytes into an internal buffer of a configurable size. After the last
written byte the commit()
method is expected to be called to notify
a callback
with an actual measured entity size. If the entity is too large to
fit into the internal buffer and the buffer exceeds before the commit()
is called then the stream is automatically committed and the callback is called
with parameter size
value of -1
.
Callback method also returns the output stream in which the output will be written. The committing output stream
must be initialized with the callback using
setStreamProvider(org.glassfish.jersey.message.internal.OutboundMessageContext.StreamProvider)
before first byte is written.
enableBuffering()
or enableBuffering(int)
before writing the first byte into this output stream. The former
method enables buffering with the default size
bytes specified in DEFAULT_BUFFER_SIZE
.
Modifier and Type | Field and Description |
---|---|
private java.io.OutputStream |
adaptedOutput
Adapted output stream.
|
private java.io.ByteArrayOutputStream |
buffer
Entity buffer.
|
private int |
bufferSize
Internal buffer size.
|
private static java.lang.String |
COMMITTING_STREAM_BUFFERING_ILLEGAL_STATE |
(package private) static int |
DEFAULT_BUFFER_SIZE
Default size of the buffer which will be used if no user defined size is specified.
|
private boolean |
directWrite
When
true , the data are written directly to output stream and not to the buffer. |
private boolean |
isClosed
When
true , the stream is already closed. |
private boolean |
isCommitted
When
true , the stream is already committed (redirected to adaptedOutput). |
private static java.util.logging.Logger |
LOGGER |
private static OutboundMessageContext.StreamProvider |
NULL_STREAM_PROVIDER
Null stream provider.
|
private static java.lang.String |
STREAM_PROVIDER_NULL |
private OutboundMessageContext.StreamProvider |
streamProvider
Buffering stream provider.
|
Constructor and Description |
---|
CommittingOutputStream()
Creates new committing output stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
(package private) void |
commit()
Commit the output stream.
|
private void |
commitStream() |
private void |
commitStream(int currentSize) |
void |
enableBuffering()
Enable buffering of the serialized entity with the
default buffer size . |
void |
enableBuffering(int bufferSize)
Enable buffering of the serialized entity.
|
void |
flush() |
private void |
flushBuffer(boolean endOfStream) |
boolean |
isClosed()
Check if the committing output stream has been closed already.
|
boolean |
isCommitted()
Determine whether the stream was already committed or not.
|
void |
setStreamProvider(OutboundMessageContext.StreamProvider streamProvider)
Set the buffering output stream provider.
|
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
private static final java.util.logging.Logger LOGGER
private static final OutboundMessageContext.StreamProvider NULL_STREAM_PROVIDER
static final int DEFAULT_BUFFER_SIZE
private java.io.OutputStream adaptedOutput
private OutboundMessageContext.StreamProvider streamProvider
private int bufferSize
private java.io.ByteArrayOutputStream buffer
private boolean directWrite
true
, the data are written directly to output stream and not to the buffer.private boolean isCommitted
true
, the stream is already committed (redirected to adaptedOutput).private boolean isClosed
true
, the stream is already closed.private static final java.lang.String STREAM_PROVIDER_NULL
private static final java.lang.String COMMITTING_STREAM_BUFFERING_ILLEGAL_STATE
public CommittingOutputStream()
public void setStreamProvider(OutboundMessageContext.StreamProvider streamProvider)
streamProvider
- non-null stream provider callback.public void enableBuffering(int bufferSize)
bufferSize
- size of the buffer. When the value is less or equal to zero the buffering will be disabled and -1
will be passed to the
callback
.public void enableBuffering()
default buffer size
.public boolean isCommitted()
true
if this stream was already committed, false
otherwise.private void commitStream() throws java.io.IOException
java.io.IOException
private void commitStream(int currentSize) throws java.io.IOException
java.io.IOException
public void write(byte[] b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
void commit() throws java.io.IOException
java.io.IOException
- when underlying stream returned from the callback method throws the io exception.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
public boolean isClosed()
true
if the stream has been closed, false
otherwise.public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
private void flushBuffer(boolean endOfStream) throws java.io.IOException
java.io.IOException