public class InputStreamResponseListener extends Response.Listener.Adapter
Response.Listener
that produces an InputStream
that allows applications to read the response content.
Typical usage is:
InputStreamResponseListener listener = new InputStreamResponseListener(); client.newRequest(...).send(listener); // Wait for the response headers to arrive Response response = listener.get(5, TimeUnit.SECONDS); if (response.getStatus() == 200) { // Obtain the input stream on the response content try (InputStream input = listener.getInputStream()) { // Read the response content } }
The HttpClient
implementation (the producer) will feed the input stream
asynchronously while the application (the consumer) is reading from it.
If the consumer is faster than the producer, then the consumer will block
with the typical InputStream.read()
semantic.
If the consumer is slower than the producer, then the producer will block
until the client consumes.
Modifier and Type | Class and Description |
---|---|
private class |
InputStreamResponseListener.Input |
Response.Listener.Adapter
Modifier and Type | Field and Description |
---|---|
private java.util.Queue<DeferredContentProvider.Chunk> |
chunks |
private boolean |
closed |
private static DeferredContentProvider.Chunk |
EOF |
private java.lang.Throwable |
failure |
private java.lang.Object |
lock |
private static Logger |
LOG |
private Response |
response |
private java.util.concurrent.CountDownLatch |
responseLatch |
private Result |
result |
private java.util.concurrent.CountDownLatch |
resultLatch |
private java.util.concurrent.atomic.AtomicReference<java.io.InputStream> |
stream |
Constructor and Description |
---|
InputStreamResponseListener() |
Modifier and Type | Method and Description |
---|---|
Result |
await(long timeout,
java.util.concurrent.TimeUnit unit)
Waits for the given timeout for the whole request/response cycle to be finished,
then returns the corresponding result.
|
private java.util.List<Callback> |
drain() |
Response |
get(long timeout,
java.util.concurrent.TimeUnit unit)
Waits for the given timeout for the response to be available, then returns it.
|
java.io.InputStream |
getInputStream()
Returns an
InputStream providing the response content bytes. |
void |
onComplete(Result result)
Callback method invoked when the request and the response have been processed,
either successfully or not.
|
void |
onContent(Response response,
java.nio.ByteBuffer content,
Callback callback)
Callback method invoked when the response content has been received, parsed and there is demand.
|
void |
onFailure(Response response,
java.lang.Throwable failure)
Callback method invoked when the response has failed in the process of being received
|
void |
onHeaders(Response response)
Callback method invoked when all the response headers have been received and parsed.
|
void |
onSuccess(Response response)
Callback method invoked when the whole response has been successfully received.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
onBegin, onContent, onHeader
onContent
onBeforeContent
private static final Logger LOG
private static final DeferredContentProvider.Chunk EOF
private final java.lang.Object lock
private final java.util.concurrent.CountDownLatch responseLatch
private final java.util.concurrent.CountDownLatch resultLatch
private final java.util.concurrent.atomic.AtomicReference<java.io.InputStream> stream
private final java.util.Queue<DeferredContentProvider.Chunk> chunks
private Response response
private Result result
private java.lang.Throwable failure
private boolean closed
public void onHeaders(Response response)
Response.HeadersListener
response
- the response containing the response line data and the headerspublic void onContent(Response response, java.nio.ByteBuffer content, Callback callback)
Response.AsyncContentListener
callback
object should be succeeded to signal that the
content
buffer has been consumed and to demand more content.response
- the response containing the response line data and the headerscontent
- the content bytes receivedcallback
- the callback to call when the content is consumed and to demand more contentpublic void onSuccess(Response response)
Response.SuccessListener
response
- the response containing the response line data and the headerspublic void onFailure(Response response, java.lang.Throwable failure)
Response.FailureListener
response
- the response containing data up to the point the failure happenedfailure
- the failure happenedpublic void onComplete(Result result)
Response.CompleteListener
The result
parameter contains the request, the response, and eventual failures.
Requests may complete after response, for example in case of big uploads that are
discarded or read asynchronously by the server.
This method is always invoked after Response.SuccessListener.onSuccess(Response)
or
Response.FailureListener.onFailure(Response, Throwable)
, and only when request indicates that
it is completed.
result
- the result of the request / response exchangepublic Response get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException
The wait ends as soon as all the HTTP headers have been received, without waiting for the content.
To wait for the whole content, see await(long, TimeUnit)
.
timeout
- the time to waitunit
- the timeout unitjava.lang.InterruptedException
- if the thread is interruptedjava.util.concurrent.TimeoutException
- if the timeout expiresjava.util.concurrent.ExecutionException
- if a failure happenedpublic Result await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
timeout
- the time to waitunit
- the timeout unitjava.lang.InterruptedException
- if the thread is interruptedjava.util.concurrent.TimeoutException
- if the timeout expiresget(long, TimeUnit)
public java.io.InputStream getInputStream()
InputStream
providing the response content bytes.
The method may be invoked only once; subsequent invocations will return a closed InputStream
.
private java.util.List<Callback> drain()