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, use BufferedContentChannel
instead. If you might want to consume the content, return
a BufferedContentChannel
up front, and connect
that
to a ReadableContentChannel at the point where you decide to consume the data.
Constructor and Description |
---|
ReadableContentChannel() |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns a lower-bound estimate on the number of bytes available to be
read() without blocking. |
void |
close(CompletionHandler handler)
Closes this ContentChannel.
|
void |
failed(Throwable t)
This method calls
CompletionHandler.failed(Throwable) on all pending CompletionHandler 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 a
ContentInputStream that wraps this ReadableContentChannel. |
void |
write(ByteBuffer buf,
CompletionHandler handler)
Schedules the given
ByteBuffer to be written to the content corresponding to this ContentChannel. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
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.public Iterator<ByteBuffer> iterator()
iterator
in interface Iterable<ByteBuffer>
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 to read()
is guaranteed to not block.
public ByteBuffer read()
Returns the next ByteBuffer in the internal queue. Before returning, this method calls CompletionHandler.completed()
on the CompletionHandler
that was submitted along with the ByteBuffer. If
there are no ByteBuffers in the queue, this method waits indefinitely for either write(ByteBuffer, CompletionHandler)
or close(CompletionHandler)
to be called. Once closed and the
internal queue drained, this method returns null.
IllegalStateException
- If the current thread is interrupted while waiting.public void failed(Throwable t)
This method calls CompletionHandler.failed(Throwable)
on all pending CompletionHandler
s, and
blocks all future operations to this ContentChannel (i.e. calls to write(ByteBuffer, CompletionHandler)
and close(CompletionHandler)
throw IllegalStateExceptions).
This method will also notify any thread waiting in read()
.
t
- The Throwable to pass to all pending CompletionHandlers.IllegalStateException
- If this method is called more than once.public ContentInputStream toStream()
Creates a ContentInputStream
that wraps this ReadableContentChannel.
Copyright © 2018. All rights reserved.