Class ReadableContentChannel
- java.lang.Object
-
- com.yahoo.jdisc.handler.ReadableContentChannel
-
- All Implemented Interfaces:
ContentChannel
,Iterable<ByteBuffer>
public final class ReadableContentChannel extends Object implements ContentChannel, Iterable<ByteBuffer>
This class implements a
ContentChannel
that has a blocking read interface. Use this class if you intend to consume the content of the ContentChannel yourself. If you intend to forward the content to another ContentChannel, useBufferedContentChannel
instead. If you might want to consume the content, return aBufferedContentChannel
up front, andconnect
that to a ReadableContentChannel at the point where you decide to consume the data.- Author:
- Simon Thoresen Hult
-
-
Constructor Summary
Constructors Constructor Description ReadableContentChannel()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
Returns a lower-bound estimate on the number of bytes available to beread()
without blocking.void
close(CompletionHandler handler)
Closes this ContentChannel.void
failed(Throwable t)
This method callsCompletionHandler.failed(Throwable)
on all pendingCompletionHandler
s, and blocks all future operations to this ContentChannel (i.e.Iterator<ByteBuffer>
iterator()
ByteBuffer
read()
Returns the next ByteBuffer in the internal queue.ContentInputStream
toStream()
Creates aContentInputStream
that wraps this ReadableContentChannel.void
write(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
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
write
public void write(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.
-
iterator
public Iterator<ByteBuffer> iterator()
- Specified by:
iterator
in interfaceIterable<ByteBuffer>
-
available
public int available()
Returns a lower-bound estimate on the number of bytes available to be
read()
without blocking. If the returned number is larger than zero, the next call toread()
is guaranteed to not block.- Returns:
- The number of bytes available to be read without blocking.
-
read
public ByteBuffer read()
Returns the next ByteBuffer in the internal queue. Before returning, this method calls
CompletionHandler.completed()
on theCompletionHandler
that was submitted along with the ByteBuffer. If there are no ByteBuffers in the queue, this method waits indefinitely for eitherwrite(ByteBuffer, CompletionHandler)
orclose(CompletionHandler)
to be called. Once closed and the internal queue drained, this method returns null.- Returns:
- The next ByteBuffer in queue, or null if this ReadableContentChannel is closed.
- Throws:
IllegalStateException
- If the current thread is interrupted while waiting.
-
failed
public void failed(Throwable t)
This method calls
CompletionHandler.failed(Throwable)
on all pendingCompletionHandler
s, and blocks all future operations to this ContentChannel (i.e. calls towrite(ByteBuffer, CompletionHandler)
andclose(CompletionHandler)
throw IllegalStateExceptions).This method will also notify any thread waiting in
read()
.- Parameters:
t
- The Throwable to pass to all pending CompletionHandlers.- Throws:
IllegalStateException
- If this method is called more than once.
-
toStream
public ContentInputStream toStream()
Creates a
ContentInputStream
that wraps this ReadableContentChannel.- Returns:
- The new ContentInputStream that wraps this.
-
-