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.
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.Adapter
Constructor and Description |
---|
InputStreamResponseListener() |
InputStreamResponseListener(long maxBufferSize) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
await() |
Result |
await(long timeout,
TimeUnit unit)
Waits for the given timeout for the whole request/response cycle to be finished,
then returns the corresponding result.
|
Response |
get(long timeout,
TimeUnit unit)
Waits for the given timeout for the response to be available, then returns it.
|
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,
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.
|
protected void |
signal() |
onBegin, onContent, onHeader
public InputStreamResponseListener()
public InputStreamResponseListener(long maxBufferSize)
public void onHeaders(Response response)
Response.HeadersListener
onHeaders
in interface Response.HeadersListener
onHeaders
in class Response.Listener.Adapter
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.Adapter
response
- the response containing the response line data and the headerscontent
- the content bytes receivedpublic void onSuccess(Response response)
Response.SuccessListener
onSuccess
in interface Response.SuccessListener
onSuccess
in class Response.Listener.Adapter
response
- the response containing the response line data and the headerspublic void onFailure(Response response, Throwable failure)
Response.FailureListener
onFailure
in interface Response.FailureListener
onFailure
in class Response.Listener.Adapter
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.
onComplete
in interface Response.CompleteListener
onComplete
in class Response.Listener.Adapter
result
- the result of the request / response exchangeprotected boolean await()
protected void signal()
public Response get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, 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 unitInterruptedException
- if the thread is interruptedTimeoutException
- if the timeout expiresExecutionException
- if a failure happenedpublic Result await(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
timeout
- the time to waitunit
- the timeout unitInterruptedException
- if the thread is interruptedTimeoutException
- if the timeout expiresget(long, TimeUnit)
public InputStream getInputStream()
InputStream
providing the response content bytes.
The method may be invoked only once; subsequent invocations will return a closed InputStream
.
Copyright © 1995-2015 Webtide. All Rights Reserved.