Class FastContentWriter

java.lang.Object
java.util.concurrent.CompletableFuture<Boolean>
com.yahoo.jdisc.handler.FastContentWriter
All Implemented Interfaces:
AutoCloseable, CompletionStage<Boolean>, Future<Boolean>

public class FastContentWriter extends CompletableFuture<Boolean> implements AutoCloseable

This class provides a non-blocking, awaitable write-interface to a ContentChannel. The CompletableFuture<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
  • Constructor Details

    • FastContentWriter

      public FastContentWriter(ContentChannel out)

      Creates a new FastContentWriter that encapsulates a given ContentChannel.

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

    • write

      public void write(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(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.
    • close

      public void close()

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

      Specified by:
      close in interface AutoCloseable
    • addListener

      public void addListener(Runnable listener, Executor executor)
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      Specified by:
      cancel in interface Future<Boolean>
      Overrides:
      cancel in class CompletableFuture<Boolean>
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface Future<Boolean>
      Overrides:
      isCancelled in class CompletableFuture<Boolean>