Class FastContentWriter
- All Implemented Interfaces:
AutoCloseable
,CompletionStage<Boolean>
,Future<Boolean>
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
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture
CompletableFuture.AsynchronousCompletionTask
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(Runnable listener, Executor executor) boolean
cancel
(boolean mayInterruptIfRunning) void
close()
Closes the underlyingContentChannel
.boolean
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
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, exceptionallyAsync, exceptionallyAsync, exceptionallyCompose, exceptionallyComposeAsync, exceptionallyComposeAsync, 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 Details
-
FastContentWriter
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
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
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
-
addListener
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) -
isCancelled
public boolean isCancelled()- Specified by:
isCancelled
in interfaceFuture<Boolean>
- Overrides:
isCancelled
in classCompletableFuture<Boolean>
-