Class ReadableContentChannel
- java.lang.Object
-
- com.yahoo.jdisc.handler.ReadableContentChannel
-
- All Implemented Interfaces:
ContentChannel
,java.lang.Iterable<java.nio.ByteBuffer>
public final class ReadableContentChannel extends java.lang.Object implements ContentChannel, java.lang.Iterable<java.nio.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(java.lang.Throwable t)
This method callsCompletionHandler.failed(Throwable)
on all pendingCompletionHandler
s, and blocks all future operations to this ContentChannel (i.e.java.util.Iterator<java.nio.ByteBuffer>
iterator()
java.nio.ByteBuffer
read()
Returns the next ByteBuffer in the internal queue.ContentInputStream
toStream()
Creates aContentInputStream
that wraps this ReadableContentChannel.void
write(java.nio.ByteBuffer buf, CompletionHandler handler)
Schedules the givenByteBuffer
to be written to the content corresponding to this ContentChannel.
-
-
-
Method Detail
-
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.
-
iterator
public java.util.Iterator<java.nio.ByteBuffer> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.nio.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 java.nio.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:
java.lang.IllegalStateException
- If the current thread is interrupted while waiting.
-
failed
public void failed(java.lang.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:
java.lang.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.
-
-