-
- Type Parameters:
T
- the response body type
- All Superinterfaces:
Flow.Subscriber<ByteBuffer>
- Enclosing class:
- HttpResponse<T>
public static interface HttpResponse.BodyProcessor<T> extends Flow.Subscriber<ByteBuffer>
A processor for response bodies.
Incubating Feature. Will be removed in a future release.The object acts as a
Flow.Subscriber
<ByteBuffer
> to the HTTP client implementation which publishes ByteBuffers containing the response body. The processor converts the incoming buffers of data to some user-defined object typeT
.The
getBody()
method returns aCompletionStage
<T>
that provides the response body object. TheCompletionStage
must be obtainable at any time. When it completes depends on the nature of typeT
. In many cases, whenT
represents the entire body after being read then it completes after the body has been read. IfT
is a streaming type such asInputStream
then it completes before the body has been read, because the calling code uses it to consume the data.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static HttpResponse.BodyProcessor<byte[]>
asByteArray()
Returns aBodyProcessor
which stores the response body as a byte array.static HttpResponse.BodyProcessor<Void>
asByteArrayConsumer(Consumer<Optional<byte[]>> consumer)
Returns aBodyProcessor
which provides the incoming body data to the provided Consumer ofOptional<byte[]>
.static HttpResponse.BodyProcessor<Path>
asFile(Path file)
Returns aBodyProcessor
which stores the response body in a file opened with the given name.static HttpResponse.BodyProcessor<Path>
asFile(Path file, OpenOption... openOptions)
Returns aBodyProcessor
which stores the response body in a file opened with the given options and name.static HttpResponse.BodyProcessor<String>
asString(Charset charset)
Returns a body processor which stores the response body as aString
converted using the givenCharset
.static <U> HttpResponse.BodyProcessor<U>
discard(U value)
Returns a response processor which discards the response body.CompletionStage<T>
getBody()
Returns aCompletionStage
which when completed will return the response body object.-
Methods inherited from interface java.util.concurrent.Flow.Subscriber
onComplete, onError, onNext, onSubscribe
-
-
-
-
Method Detail
-
getBody
CompletionStage<T> getBody()
Returns aCompletionStage
which when completed will return the response body object.- Returns:
- a CompletionStage for the response body
-
asString
static HttpResponse.BodyProcessor<String> asString(Charset charset)
Returns a body processor which stores the response body as aString
converted using the givenCharset
.The
HttpResponse
using this processor is available after the entire response has been read.- Parameters:
charset
- the character set to convert the String with- Returns:
- a body processor
-
asByteArray
static HttpResponse.BodyProcessor<byte[]> asByteArray()
Returns aBodyProcessor
which stores the response body as a byte array.The
HttpResponse
using this processor is available after the entire response has been read.- Returns:
- a body processor
-
asFile
static HttpResponse.BodyProcessor<Path> asFile(Path file, OpenOption... openOptions)
Returns aBodyProcessor
which stores the response body in a file opened with the given options and name. The file will be opened with the given options usingFileChannel.open
just before the body is read. Any exception thrown will be returned or thrown fromHttpClient::send
orHttpClient::sendAsync
as appropriate.The
HttpResponse
using this processor is available after the entire response has been read.- Parameters:
file
- the file to store the body inopenOptions
- the list of options to open the file with- Returns:
- a body processor
-
asByteArrayConsumer
static HttpResponse.BodyProcessor<Void> asByteArrayConsumer(Consumer<Optional<byte[]>> consumer)
Returns aBodyProcessor
which provides the incoming body data to the provided Consumer ofOptional<byte[]>
. Each call toConsumer.accept()
will contain a non emptyOptional
, except for the final invocation after all body data has been read, when theOptional
will be empty.The
HttpResponse
using this processor is available after the entire response has been read.- Parameters:
consumer
- a Consumer of byte arrays- Returns:
- a BodyProcessor
-
asFile
static HttpResponse.BodyProcessor<Path> asFile(Path file)
Returns aBodyProcessor
which stores the response body in a file opened with the given name. Has the same effect as callingasFile
with the standard open optionsCREATE
andWRITE
The
HttpResponse
using this processor is available after the entire response has been read.- Parameters:
file
- the file to store the body in- Returns:
- a body processor
-
discard
static <U> HttpResponse.BodyProcessor<U> discard(U value)
Returns a response processor which discards the response body. The supplied value is the value that will be returned fromHttpResponse.body()
.- Type Parameters:
U
- The type of the response body- Parameters:
value
- the value to return from HttpResponse.body()- Returns:
- a
BodyProcessor
-
-