Class CommittingOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.glassfish.jersey.message.internal.CommittingOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public final class CommittingOutputStream extends OutputStream
A 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.- Author:
- Paul Sandoz, Marek Potociar, Miroslav Fuksa
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFFER_SIZEDefault size of the buffer which will be used if no user defined size is specified.
-
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.voidenableBuffering(int bufferSize)Enable buffering of the serialized entity.voidflush()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)-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
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
-
-
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.
-
isCommitted
public boolean isCommitted()
Determine whether the stream was already committed or not.- Returns:
trueif this stream was already committed,falseotherwise.
-
write
public void write(byte[] b) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
write
public void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
commit
public void commit() throws IOExceptionCommit the output stream.- Throws:
IOException- when underlying stream returned from the callback method throws the io exception.
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
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 IOException- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
-