public class OutputStreamContentProvider extends Object implements AsyncContentProvider, Callback, Closeable
ContentProvider that provides content asynchronously through an OutputStream
similar to DeferredContentProvider.
OutputStreamContentProvider can only be used in conjunction with
Request.send(Response.CompleteListener) (and not with its blocking counterpart Request.send())
because it provides content asynchronously.
The deferred content is provided once by writing to the output stream
and then fully consumed.
Invocations to the iterator() method after the first will return an "empty" iterator
because the stream has been consumed on the first invocation.
However, it is possible for subclasses to support multiple invocations of iterator()
by overriding write(ByteBuffer) and close(), copying the bytes and making them
available for subsequent invocations.
Content must be provided by writing to the output stream, that must be
closed when all content has been provided.
Example usage:
HttpClient httpClient = ...;
// Use try-with-resources to autoclose the output stream
OutputStreamContentProvider content = new OutputStreamContentProvider();
try (OutputStream output = content.getOutputStream())
{
httpClient.newRequest("localhost", 8080)
.content(content)
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
// Your logic here
}
});
// At a later time...
output.write("some content".getBytes());
}
AsyncContentProvider.ListenerContentProvider.TypedCallback.Completable, Callback.NestedInvocable.InvocationType__nonBlocking| Constructor and Description |
|---|
OutputStreamContentProvider() |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
failed(Throwable failure) |
Invocable.InvocationType |
getInvocationType() |
long |
getLength() |
OutputStream |
getOutputStream() |
Iterator<ByteBuffer> |
iterator() |
void |
setListener(AsyncContentProvider.Listener listener) |
void |
succeeded() |
protected void |
write(ByteBuffer buffer) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEach, spliteratorasPreferred, getInvocationType, invokeNonBlocking, invokePreferNonBlocking, invokePreferred, isNonBlockingInvocationpublic Invocable.InvocationType getInvocationType()
getInvocationType in interface Invocablepublic long getLength()
getLength in interface ContentProviderpublic Iterator<ByteBuffer> iterator()
iterator in interface Iterable<ByteBuffer>public void setListener(AsyncContentProvider.Listener listener)
setListener in interface AsyncContentProviderlistener - the listener to be notified of content availabilitypublic OutputStream getOutputStream()
protected void write(ByteBuffer buffer)
public void close()
close in interface Closeableclose in interface AutoCloseableCopyright © 1995–2017 Webtide. All rights reserved.