public final class BufferedContentChannel extends Object implements ContentChannel
ContentChannel
methods are implemented
by pushing to a thread-safe internal queue. All of the queued calls are forwarded to another ContentChannel when
connectTo(ContentChannel)
is called. Once connected, this class becomes a non-buffering proxy for the
connected ContentChannel.Modifier and Type | Class and Description |
---|---|
private static class |
BufferedContentChannel.Entry |
Modifier and Type | Field and Description |
---|---|
private CompletionHandler |
closeCompletion |
private boolean |
closed |
private ContentChannel |
content |
private Object |
lock |
private List<BufferedContentChannel.Entry> |
queue |
Constructor and Description |
---|
BufferedContentChannel() |
Modifier and Type | Method and Description |
---|---|
void |
close(CompletionHandler handler)
Closes this ContentChannel.
|
void |
connectTo(ContentChannel content)
Connects this BufferedContentChannel to a ContentChannel.
|
boolean |
isConnected()
Returns whether or not
connectTo(ContentChannel) has been called. |
ReadableContentChannel |
toReadable()
Creates a
ReadableContentChannel and connects to it. |
ContentInputStream |
toStream()
Creates a
ContentInputStream and connects to its internal
ContentChannel. |
void |
write(ByteBuffer buf,
CompletionHandler handler)
Schedules the given
ByteBuffer to be written to the content corresponding to this ContentChannel. |
private final Object lock
private List<BufferedContentChannel.Entry> queue
private ContentChannel content
private boolean closed
private CompletionHandler closeCompletion
public void connectTo(ContentChannel content)
Connects this BufferedContentChannel to a ContentChannel. First, this method forwards all queued calls to the
connected ContentChannel. Once this method has been called, all future calls to write(ByteBuffer,
CompletionHandler)
and close(CompletionHandler)
are synchronously forwarded to the connected
ContentChannel.
content
- The ContentChannel to connect to.NullPointerException
- If the content argument is null.IllegalStateException
- If another ContentChannel has already been connected.public boolean isConnected()
Returns whether or not connectTo(ContentChannel)
has been called. Even if this method returns false,
calling connectTo(ContentChannel)
might still throw an IllegalStateException if there is a race.
connectTo(ContentChannel)
has been called.public ReadableContentChannel toReadable()
Creates a ReadableContentChannel
and connects
to it.
public ContentInputStream toStream()
Creates a ContentInputStream
and connects
to its internal
ContentChannel.
public void write(ByteBuffer buf, CompletionHandler handler)
ContentChannel
ByteBuffer
to be written to the content corresponding to this ContentChannel. This
call transfers ownership of the given ByteBuffer to this ContentChannel, i.e. no further calls can be
made to the buffer. The execution of writes happen in the same order as this method was invoked.write
in interface ContentChannel
buf
- The ByteBuffer
to schedule for write. No further calls can be made to this buffer.handler
- The CompletionHandler
to call after the write has been executed.public void close(CompletionHandler handler)
ContentChannel
ContentChannel.write(ByteBuffer,
CompletionHandler)
upon it will cause an IllegalStateException
to be thrown. If this channel is already
closed then invoking this method has no effect, but CompletionHandler.completed()
will still be called.
Notice that you MUST call this method, regardless of any exceptions that might have occurred while writing to this
ContentChannel. Failure to do so will prevent the Container
from ever shutting down.close
in interface ContentChannel
handler
- The CompletionHandler
to call after the close has been executed.Copyright © 2018. All rights reserved.