Package com.yahoo.jdisc.handler
Class BufferedContentChannel
- java.lang.Object
-
- com.yahoo.jdisc.handler.BufferedContentChannel
-
- All Implemented Interfaces:
ContentChannel
public final class BufferedContentChannel extends java.lang.Object implements ContentChannel
This class implements an unlimited, non-blocking content queue. AllContentChannel
methods are implemented by pushing to a thread-safe internal queue. All of the queued calls are forwarded to another ContentChannel whenconnectTo(ContentChannel)
is called. Once connected, this class becomes a non-buffering proxy for the connected ContentChannel.- Author:
- Simon Thoresen Hult
-
-
Constructor Summary
Constructors Constructor Description BufferedContentChannel()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close(CompletionHandler handler)
Closes this ContentChannel.void
connectTo(ContentChannel content)
Connects this BufferedContentChannel to a ContentChannel.boolean
isConnected()
Returns whether or notconnectTo(ContentChannel)
has been called.ReadableContentChannel
toReadable()
Creates aReadableContentChannel
andconnects
to it.ContentInputStream
toStream()
Creates aContentInputStream
andconnects
to its internal ContentChannel.void
write(java.nio.ByteBuffer buf, CompletionHandler handler)
Schedules the givenByteBuffer
to be written to the content corresponding to this ContentChannel.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.yahoo.jdisc.handler.ContentChannel
onError
-
-
-
-
Method Detail
-
connectTo
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)
andclose(CompletionHandler)
are synchronously forwarded to the connected ContentChannel.- Parameters:
content
- The ContentChannel to connect to.- Throws:
java.lang.NullPointerException
- If the content argument is null.java.lang.IllegalStateException
- If another ContentChannel has already been connected.
-
isConnected
public boolean isConnected()
Returns whether or not
connectTo(ContentChannel)
has been called. Even if this method returns false, callingconnectTo(ContentChannel)
might still throw an IllegalStateException if there is a race.- Returns:
- True if
connectTo(ContentChannel)
has been called.
-
toReadable
public ReadableContentChannel toReadable()
Creates a
ReadableContentChannel
andconnects
to it.- Returns:
- The new ReadableContentChannel that this connected to.
-
toStream
public ContentInputStream toStream()
Creates a
ContentInputStream
andconnects
to its internal ContentChannel.- Returns:
- The new ContentInputStream that this connected to.
-
write
public void write(java.nio.ByteBuffer buf, CompletionHandler handler)
Description copied from interface:ContentChannel
Schedules the givenByteBuffer
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.- Specified by:
write
in interfaceContentChannel
- Parameters:
buf
- TheByteBuffer
to schedule for write. No further calls can be made to this buffer.handler
- TheCompletionHandler
to call after the write has been executed.
-
close
public void close(CompletionHandler handler)
Description copied from interface:ContentChannel
Closes this ContentChannel. After a channel is closed, any further attempt to invokeContentChannel.write(ByteBuffer, CompletionHandler)
upon it will cause anIllegalStateException
to be thrown. If this channel is already closed then invoking this method has no effect, butCompletionHandler.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 theContainer
from ever shutting down.- Specified by:
close
in interfaceContentChannel
- Parameters:
handler
- TheCompletionHandler
to call after the close has been executed.
-
-