Class OutputStreamContentProvider
- java.lang.Object
-
- org.eclipse.jetty.client.util.OutputStreamContentProvider
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<ByteBuffer>
,ContentProvider
,AsyncContentProvider
,org.eclipse.jetty.util.Callback
public class OutputStreamContentProvider extends Object implements AsyncContentProvider, org.eclipse.jetty.util.Callback, Closeable
AContentProvider
that provides content asynchronously through anOutputStream
similar toDeferredContentProvider
.OutputStreamContentProvider
can only be used in conjunction withRequest.send(Response.CompleteListener)
(and not with its blocking counterpartRequest.send()
) because it provides content asynchronously.The deferred content is provided once by writing to the
output stream
and then fully consumed. Invocations to theiterator()
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 ofiterator()
by overridingwrite(ByteBuffer)
andclose()
, copying the bytes and making them available for subsequent invocations.Content must be provided by writing to the
output stream
, that must beclosed
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()); }
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.AsyncContentProvider
AsyncContentProvider.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.Callback
org.eclipse.jetty.util.Callback.Adapter, org.eclipse.jetty.util.Callback.Nested, org.eclipse.jetty.util.Callback.NonBlocking
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.api.ContentProvider
ContentProvider.Typed
-
-
Constructor Summary
Constructors Constructor Description OutputStreamContentProvider()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
failed(Throwable failure)
long
getLength()
OutputStream
getOutputStream()
boolean
isNonBlocking()
Iterator<ByteBuffer>
iterator()
void
setListener(AsyncContentProvider.Listener listener)
void
succeeded()
protected void
write(ByteBuffer buffer)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
isNonBlocking
public boolean isNonBlocking()
- Specified by:
isNonBlocking
in interfaceorg.eclipse.jetty.util.Callback
-
getLength
public long getLength()
- Specified by:
getLength
in interfaceContentProvider
- Returns:
- the content length, if known, or -1 if the content length is unknown
-
iterator
public Iterator<ByteBuffer> iterator()
- Specified by:
iterator
in interfaceIterable<ByteBuffer>
-
setListener
public void setListener(AsyncContentProvider.Listener listener)
- Specified by:
setListener
in interfaceAsyncContentProvider
- Parameters:
listener
- the listener to be notified of content availability
-
getOutputStream
public OutputStream getOutputStream()
-
write
protected void write(ByteBuffer buffer)
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
succeeded
public void succeeded()
- Specified by:
succeeded
in interfaceorg.eclipse.jetty.util.Callback
-
failed
public void failed(Throwable failure)
- Specified by:
failed
in interfaceorg.eclipse.jetty.util.Callback
-
-