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 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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture
CompletableFuture.AsynchronousCompletionTask
-
-
Constructor Summary
Constructors Constructor Description FastContentWriter(ContentChannel out)
Creates a new FastContentWriter that encapsulates a givenContentChannel
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(Runnable listener, Executor executor)
boolean
cancel(boolean mayInterruptIfRunning)
void
close()
Closes the underlyingContentChannel
.boolean
isCancelled()
void
write(byte[] buf)
This is a convenience method to convert the given byte array into a ByteBuffer object, and then passing that towrite(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 towrite(java.nio.ByteBuffer)
.void
write(String str)
This is a convenience method to convert the given string to a ByteBuffer of UTF8 bytes, and then passing that towrite(ByteBuffer)
.void
write(ByteBuffer buf)
Writes to the underlyingContentChannel
.-
Methods inherited from class java.util.concurrent.CompletableFuture
acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, complete, completeAsync, completeAsync, completedFuture, completedStage, completeExceptionally, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, failedFuture, failedStage, get, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCompletedExceptionally, isDone, join, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
-
-
-
-
Constructor Detail
-
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 Detail
-
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
. IfCompletionHandler.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
. IfCompletionHandler.failed(Throwable)
is called, either of the get() methods will rethrow that Throwable.- Specified by:
close
in interfaceAutoCloseable
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in interfaceFuture<Boolean>
- Overrides:
isCancelled
in classCompletableFuture<Boolean>
-
-