Class ChunkedBodyOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.glassfish.jersey.jdk.connector.internal.BodyOutputStream
-
- org.glassfish.jersey.jdk.connector.internal.ChunkedBodyOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
class ChunkedBodyOutputStream extends BodyOutputStream
Body stream that can operate either synchronously or asynchronously. SeeBodyOutputStream
for details.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static interface
ChunkedBodyOutputStream.Listener
Set a close listener which will be called when the user closes the stream.private static class
ChunkedBodyOutputStream.Mode
-
Field Summary
Fields Modifier and Type Field Description private boolean
callListener
private int
chunkSize
private boolean
closed
private ChunkedBodyOutputStream.Listener
closeListener
private java.nio.ByteBuffer
dataBuffer
private Filter<java.nio.ByteBuffer,?,?,?>
downstreamFilter
private static java.nio.ByteBuffer
EMPTY_BUFFER
private int
encodedFullChunkSize
private java.util.concurrent.CountDownLatch
initialBlockingLatch
private ChunkedBodyOutputStream.Mode
mode
private boolean
ready
private WriteListener
writeListener
-
Constructor Summary
Constructors Constructor Description ChunkedBodyOutputStream(int chunkSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
assertAsynchronousOperation()
private void
assertValidState()
private void
callOnWritePossible()
void
close()
protected void
commitToMode()
protected void
doInitialBlocking()
protected java.nio.ByteBuffer
encodeToHttp(java.nio.ByteBuffer byteBuffer)
Transform raw application data into HTTP body.void
flush()
boolean
isReady()
Returns true if data can be written without blocking else returns false.(package private) void
open(Filter<java.nio.ByteBuffer,?,?,?> downstreamFilter)
(package private) void
setCloseListener(ChunkedBodyOutputStream.Listener closeListener)
Set a close listener which will be called when the user closes the stream.void
setWriteListener(WriteListener writeListener)
Instructs the stream to invoke the providedWriteListener
when it is possible to write.void
write(byte[] b, int off, int len)
void
write(int b)
protected void
write(java.nio.ByteBuffer byteBuffer)
-
-
-
Field Detail
-
EMPTY_BUFFER
private static final java.nio.ByteBuffer EMPTY_BUFFER
-
chunkSize
private final int chunkSize
-
encodedFullChunkSize
private final int encodedFullChunkSize
-
dataBuffer
private final java.nio.ByteBuffer dataBuffer
-
initialBlockingLatch
private final java.util.concurrent.CountDownLatch initialBlockingLatch
-
downstreamFilter
private volatile Filter<java.nio.ByteBuffer,?,?,?> downstreamFilter
-
writeListener
private volatile WriteListener writeListener
-
closeListener
private volatile ChunkedBodyOutputStream.Listener closeListener
-
mode
private volatile ChunkedBodyOutputStream.Mode mode
-
ready
private volatile boolean ready
-
callListener
private volatile boolean callListener
-
closed
private volatile boolean closed
-
-
Method Detail
-
setWriteListener
public void setWriteListener(WriteListener writeListener)
Description copied from class:BodyOutputStream
Instructs the stream to invoke the providedWriteListener
when it is possible to write. If the stream is in UNDECIDED state, invoking this method will commit the stream to ASYNCHRONOUS mode.- Specified by:
setWriteListener
in classBodyOutputStream
- Parameters:
writeListener
- theWriteListener
that should be notified when it's possible to write.
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
isReady
public boolean isReady()
Description copied from class:BodyOutputStream
Returns true if data can be written without blocking else returns false. If the stream is in ASYNCHRONOUS mode and the user attempts to write to it even though this method returns false, anIllegalStateException
is thrown.- Specified by:
isReady
in classBodyOutputStream
- Returns:
true
if data can be obtained without blocking, otherwise returnsfalse
.
-
assertValidState
private void assertValidState()
-
write
protected void write(java.nio.ByteBuffer byteBuffer) throws java.io.IOException
- Throws:
java.io.IOException
-
open
void open(Filter<java.nio.ByteBuffer,?,?,?> downstreamFilter)
-
doInitialBlocking
protected void doInitialBlocking() throws java.io.IOException
- Throws:
java.io.IOException
-
commitToMode
protected void commitToMode()
-
assertAsynchronousOperation
private void assertAsynchronousOperation()
-
callOnWritePossible
private void callOnWritePossible()
-
setCloseListener
void setCloseListener(ChunkedBodyOutputStream.Listener closeListener)
Set a close listener which will be called when the user closes the stream. This is used to indicate that the body has been completely written.- Parameters:
closeListener
- close listener.
-
encodeToHttp
protected java.nio.ByteBuffer encodeToHttp(java.nio.ByteBuffer byteBuffer)
Transform raw application data into HTTP body.- Parameters:
byteBuffer
- application data.- Returns:
- http body part.
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
-