public class FastContentWriter extends Object implements com.google.common.util.concurrent.ListenableFuture<Boolean>, 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.
Modifier and Type | Class and Description |
---|---|
private class |
FastContentWriter.SimpleCompletionHandler |
Modifier and Type | Field and Description |
---|---|
private AtomicBoolean |
closed |
private CompletionHandler |
completionHandler |
private com.google.common.util.concurrent.SettableFuture<Boolean> |
future |
private AtomicInteger |
numPendingCompletions |
private ContentChannel |
out |
Constructor and Description |
---|
FastContentWriter(ContentChannel out)
Creates a new FastContentWriter that encapsulates a given
ContentChannel . |
Modifier and Type | Method and Description |
---|---|
void |
addListener(Runnable listener,
Executor executor) |
boolean |
cancel(boolean mayInterruptIfRunning) |
void |
close()
Closes the underlying
ContentChannel . |
Boolean |
get() |
Boolean |
get(long timeout,
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(ByteBuffer buf)
Writes to the underlying
ContentChannel . |
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) . |
private final AtomicBoolean closed
private final AtomicInteger numPendingCompletions
private final CompletionHandler completionHandler
private final ContentChannel out
private final com.google.common.util.concurrent.SettableFuture<Boolean> future
public FastContentWriter(ContentChannel out)
Creates a new FastContentWriter that encapsulates a given ContentChannel
.
out
- The ContentChannel to encapsulate.NullPointerException
- If the content argument is null.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)
.
str
- The string to 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)
.
buf
- The bytes to 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)
.
buf
- The bytes to write.offset
- The offset of the subarray to be used.length
- The length of the subarray to be used.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.
buf
- The ByteBuffer to write.public void close()
Closes the underlying ContentChannel
. If CompletionHandler.failed(Throwable)
is called,
either of the get() methods will rethrow that Throwable.
close
in interface AutoCloseable
public void addListener(Runnable listener, Executor executor)
addListener
in interface com.google.common.util.concurrent.ListenableFuture<Boolean>
public boolean cancel(boolean mayInterruptIfRunning)
public boolean isCancelled()
isCancelled
in interface Future<Boolean>
public Boolean get() throws InterruptedException, ExecutionException
get
in interface Future<Boolean>
InterruptedException
ExecutionException
public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get
in interface Future<Boolean>
InterruptedException
ExecutionException
TimeoutException
Copyright © 2017. All rights reserved.