public class InputStreamResponseListener extends Response.Listener.Empty
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.
Chunks of content are maintained in a queue, and it is possible to specify a
maximum buffer size for the bytes held in the queue, by default 16384 bytes.
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.Response.Listener.Empty
Constructor and Description |
---|
InputStreamResponseListener() |
InputStreamResponseListener(long maxBufferSize) |
Modifier and Type | Method and Description |
---|---|
Result |
await(long timeout,
TimeUnit unit) |
Response |
get(long timeout,
TimeUnit unit) |
InputStream |
getInputStream() |
void |
onComplete(Result result)
Callback method invoked when the request and the response have been processed,
either successfully or not.
|
void |
onContent(Response response,
ByteBuffer content)
Callback method invoked when the response content has been received.
|
void |
onFailure(Response response,
Throwable failure)
Callback method invoked when the response has failed in the process of being received
|
void |
onHeaders(Response response)
Callback method invoked when the response headers have been received and parsed.
|
void |
onSuccess(Response response)
Callback method invoked when the whole response has been successfully received.
|
onBegin, onHeader
public InputStreamResponseListener()
public InputStreamResponseListener(long maxBufferSize)
public void onHeaders(Response response)
Response.HeadersListener
onHeaders
in interface Response.HeadersListener
onHeaders
in class Response.Listener.Empty
response
- the response containing the response line data and the headerspublic void onContent(Response response, ByteBuffer content)
Response.ContentListener
content
buffer must be consumed
before returning from this method.onContent
in interface Response.ContentListener
onContent
in class Response.Listener.Empty
response
- the response containing the response line data and the headerscontent
- the content bytes receivedpublic void onFailure(Response response, Throwable failure)
Response.FailureListener
onFailure
in interface Response.FailureListener
onFailure
in class Response.Listener.Empty
response
- the response containing data up to the point the failure happenedfailure
- the failure happenedpublic void onSuccess(Response response)
Response.SuccessListener
onSuccess
in interface Response.SuccessListener
onSuccess
in class Response.Listener.Empty
response
- the response containing the response line data and the headerspublic void onComplete(Result result)
Response.CompleteListener
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.onComplete
in interface Response.CompleteListener
onComplete
in class Response.Listener.Empty
result
- the result of the request / response exchangepublic Response get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, ExecutionException
public Result await(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
InterruptedException
TimeoutException
public InputStream getInputStream()
Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.