Class CommittingOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
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
8192 bytes specified in DEFAULT_BUFFER_SIZE
.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate OutputStream
Adapted output stream.private ByteArrayOutputStream
Entity buffer.private int
Internal buffer size.private static final String
static final int
Default size of the buffer which will be used if no user defined size is specified.private boolean
Whentrue
, the data are written directly to output stream and not to the buffer.private boolean
Whentrue
, the stream is already closed.private boolean
Whentrue
, the stream is already committed (redirected to adaptedOutput).private static final Logger
private static final OutboundMessageContext.StreamProvider
Null stream provider.private static final String
Buffering stream provider. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
commit()
Commit the output stream.private void
private void
commitStream
(int currentSize) (package private) void
Enable buffering of the serialized entity with thedefault 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
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) Methods inherited from class java.io.OutputStream
nullOutputStream
-
Field Details
-
LOGGER
-
NULL_STREAM_PROVIDER
Null stream provider. -
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEDefault size of the buffer which will be used if no user defined size is specified.- See Also:
-
adaptedOutput
Adapted output stream. -
streamProvider
Buffering stream provider. -
bufferSize
private int bufferSizeInternal buffer size. -
buffer
Entity buffer. -
directWrite
private boolean directWriteWhentrue
, the data are written directly to output stream and not to the buffer. -
isCommitted
private boolean isCommittedWhentrue
, the stream is already committed (redirected to adaptedOutput). -
isClosed
private boolean isClosedWhentrue
, the stream is already closed. -
STREAM_PROVIDER_NULL
-
COMMITTING_STREAM_BUFFERING_ILLEGAL_STATE
-
-
Constructor Details
-
CommittingOutputStream
public CommittingOutputStream()Creates new committing output stream. The returned stream instance still needs to be initialized before writing first bytes.
-
-
Method Details
-
setStreamProvider
Set the buffering output stream provider. If the committing output stream works in buffering mode this method must be called before first bytes are written into this stream.- Parameters:
streamProvider
- non-null stream provider callback.
-
enableBuffering
public void enableBuffering(int bufferSize) Enable buffering of the serialized entity.- Parameters:
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 thecallback
.
-
enableBuffering
void enableBuffering()Enable buffering of the serialized entity with thedefault buffer size
. -
isCommitted
public boolean isCommitted()Determine whether the stream was already committed or not.- Returns:
true
if this stream was already committed,false
otherwise.
-
commitStream
- Throws:
IOException
-
commitStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
commit
Commit the output stream.- Throws:
IOException
- when underlying stream returned from the callback method throws the io exception.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
isClosed
public boolean isClosed()Check if the committing output stream has been closed already.- Returns:
true
if the stream has been closed,false
otherwise.
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
flushBuffer
- Throws:
IOException
-