Class 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, 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.

    Author:
    Simon Thoresen Hult
    • 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 be read() without blocking.
      void close​(CompletionHandler handler)
      Closes this ContentChannel.
      void failed​(java.lang.Throwable t)
      This method calls CompletionHandler.failed(Throwable) on all pending CompletionHandlers, 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 a ContentInputStream that wraps this ReadableContentChannel.
      void write​(java.nio.ByteBuffer buf, CompletionHandler handler)
      Schedules the given ByteBuffer 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 java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • ReadableContentChannel

        public ReadableContentChannel()
    • Method Detail

      • write

        public void write​(java.nio.ByteBuffer buf,
                          CompletionHandler handler)
        Description copied from interface: ContentChannel
        Schedules the given 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.
        Specified by:
        write in interface ContentChannel
        Parameters:
        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.
      • iterator

        public java.util.Iterator<java.nio.ByteBuffer> iterator()
        Specified by:
        iterator in interface java.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 to read() 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 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.

        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.