Class CommittingOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.glassfish.jersey.message.internal.CommittingOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class CommittingOutputStream extends java.io.OutputStreamA committing output stream with optional serialized entity buffering functionality which allows measuring of the entity size.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 acallbackwith an actual measured entity size. If the entity is too large to fit into the internal buffer and the buffer exceeds before thecommit()is called then the stream is automatically committed and the callback is called with parametersizevalue 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
The buffering is by default disabled and can be enabled by callingsetStreamProvider(org.glassfish.jersey.message.internal.OutboundMessageContext.StreamProvider)before first byte is written.enableBuffering()orenableBuffering(int)before writing the first byte into this output stream. The former method enables buffering with the default size 8192 bytes specified inDEFAULT_BUFFER_SIZE.
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.OutputStreamadaptedOutputAdapted output stream.private java.io.ByteArrayOutputStreambufferEntity buffer.private intbufferSizeInternal buffer size.private static java.lang.StringCOMMITTING_STREAM_BUFFERING_ILLEGAL_STATEstatic intDEFAULT_BUFFER_SIZEDefault size of the buffer which will be used if no user defined size is specified.private booleandirectWriteWhentrue, the data are written directly to output stream and not to the buffer.private booleanisClosedWhentrue, the stream is already closed.private booleanisCommittedWhentrue, the stream is already committed (redirected to adaptedOutput).private static java.util.logging.LoggerLOGGERprivate static OutboundMessageContext.StreamProviderNULL_STREAM_PROVIDERNull stream provider.private static java.lang.StringSTREAM_PROVIDER_NULLprivate OutboundMessageContext.StreamProviderstreamProviderBuffering stream provider.
-
Constructor Summary
Constructors Constructor Description CommittingOutputStream()Creates new committing output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidcommit()Commit the output stream.private voidcommitStream()private voidcommitStream(int currentSize)(package private) voidenableBuffering()Enable buffering of the serialized entity with thedefault buffer size.voidenableBuffering(int bufferSize)Enable buffering of the serialized entity.voidflush()private voidflushBuffer(boolean endOfStream)booleanisClosed()Check if the committing output stream has been closed already.booleanisCommitted()Determine whether the stream was already committed or not.voidsetStreamProvider(OutboundMessageContext.StreamProvider streamProvider)Set the buffering output stream provider.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
NULL_STREAM_PROVIDER
private static final OutboundMessageContext.StreamProvider NULL_STREAM_PROVIDER
Null stream provider.
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
Default size of the buffer which will be used if no user defined size is specified.- See Also:
- Constant Field Values
-
adaptedOutput
private java.io.OutputStream adaptedOutput
Adapted output stream.
-
streamProvider
private OutboundMessageContext.StreamProvider streamProvider
Buffering stream provider.
-
bufferSize
private int bufferSize
Internal buffer size.
-
buffer
private java.io.ByteArrayOutputStream buffer
Entity buffer.
-
directWrite
private boolean directWrite
Whentrue, the data are written directly to output stream and not to the buffer.
-
isCommitted
private boolean isCommitted
Whentrue, the stream is already committed (redirected to adaptedOutput).
-
isClosed
private boolean isClosed
Whentrue, the stream is already closed.
-
STREAM_PROVIDER_NULL
private static final java.lang.String STREAM_PROVIDER_NULL
-
COMMITTING_STREAM_BUFFERING_ILLEGAL_STATE
private static final java.lang.String COMMITTING_STREAM_BUFFERING_ILLEGAL_STATE
-
-
Method Detail
-
setStreamProvider
public void setStreamProvider(OutboundMessageContext.StreamProvider streamProvider)
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-1will 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:
trueif this stream was already committed,falseotherwise.
-
commitStream
private void commitStream() throws java.io.IOException- Throws:
java.io.IOException
-
commitStream
private void commitStream(int currentSize) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
commit
public void commit() throws java.io.IOExceptionCommit the output stream.- Throws:
java.io.IOException- when underlying stream returned from the callback method throws the io exception.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException
-
isClosed
public boolean isClosed()
Check if the committing output stream has been closed already.- Returns:
trueif the stream has been closed,falseotherwise.
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException
-
flushBuffer
private void flushBuffer(boolean endOfStream) throws java.io.IOException- Throws:
java.io.IOException
-
-