Class FastContentWriter

  • All Implemented Interfaces:
    com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>, java.lang.AutoCloseable, java.util.concurrent.Future<java.lang.Boolean>

    public class FastContentWriter
    extends java.lang.Object
    implements com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>, java.lang.AutoCloseable

    This class provides a non-blocking, awaitable write-interface to a ContentChannel. The ListenableFuture<Boolean> interface can be used to await the asynchronous completion of all pending operations. Any asynchronous failure will be rethrown when calling either of the get() methods on this class.

    Please notice that the Future implementation of this class will NEVER complete unless close() has been called; please use try-with-resources to ensure that close() is called.

    Author:
    Simon Thoresen Hult
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addListener​(java.lang.Runnable listener, java.util.concurrent.Executor executor)  
      boolean cancel​(boolean mayInterruptIfRunning)  
      void close()
      Closes the underlying ContentChannel.
      java.lang.Boolean get()  
      java.lang.Boolean get​(long timeout, java.util.concurrent.TimeUnit unit)  
      boolean isCancelled()  
      boolean isDone()  
      void write​(byte[] buf)
      This is a convenience method to convert the given byte array into a ByteBuffer object, and then passing that to write(java.nio.ByteBuffer).
      void write​(byte[] buf, int offset, int length)
      This is a convenience method to convert a subarray of the given byte array into a ByteBuffer object, and then passing that to write(java.nio.ByteBuffer).
      void write​(java.lang.String str)
      This is a convenience method to convert the given string to a ByteBuffer of UTF8 bytes, and then passing that to write(ByteBuffer).
      void write​(java.nio.ByteBuffer buf)
      Writes to the underlying ContentChannel.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FastContentWriter

        public FastContentWriter​(ContentChannel out)

        Creates a new FastContentWriter that encapsulates a given ContentChannel.

        Parameters:
        out - The ContentChannel to encapsulate.
        Throws:
        java.lang.NullPointerException - If the content argument is null.
    • Method Detail

      • write

        public void write​(java.lang.String str)

        This is a convenience method to convert the given string to a ByteBuffer of UTF8 bytes, and then passing that to write(ByteBuffer).

        Parameters:
        str - The string to write.
      • write

        public void write​(byte[] buf)

        This is a convenience method to convert the given byte array into a ByteBuffer object, and then passing that to write(java.nio.ByteBuffer).

        Parameters:
        buf - The bytes to write.
      • write

        public void write​(byte[] buf,
                          int offset,
                          int length)

        This is a convenience method to convert a subarray of the given byte array into a ByteBuffer object, and then passing that to write(java.nio.ByteBuffer).

        Parameters:
        buf - The bytes to write.
        offset - The offset of the subarray to be used.
        length - The length of the subarray to be used.
      • write

        public void write​(java.nio.ByteBuffer buf)

        Writes to the underlying ContentChannel. If CompletionHandler.failed(Throwable) is called, either of the get() methods will rethrow that Throwable.

        Parameters:
        buf - The ByteBuffer to write.
      • addListener

        public void addListener​(java.lang.Runnable listener,
                                java.util.concurrent.Executor executor)
        Specified by:
        addListener in interface com.google.common.util.concurrent.ListenableFuture<java.lang.Boolean>
      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<java.lang.Boolean>
      • isCancelled

        public boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<java.lang.Boolean>
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface java.util.concurrent.Future<java.lang.Boolean>
      • get

        public java.lang.Boolean get()
                              throws java.lang.InterruptedException,
                                     java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.util.concurrent.Future<java.lang.Boolean>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • get

        public java.lang.Boolean get​(long timeout,
                                     java.util.concurrent.TimeUnit unit)
                              throws java.lang.InterruptedException,
                                     java.util.concurrent.ExecutionException,
                                     java.util.concurrent.TimeoutException
        Specified by:
        get in interface java.util.concurrent.Future<java.lang.Boolean>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException