Interface HttpResponse.BodyProcessor<T>

  • 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 type T.

    The getBody() method returns a CompletionStage<T> that provides the response body object. The CompletionStage must be obtainable at any time. When it completes depends on the nature of type T. In many cases, when T represents the entire body after being read then it completes after the body has been read. If T is a streaming type such as InputStream then it completes before the body has been read, because the calling code uses it to consume the data.

    • Method Detail

      • getBody

        CompletionStage<T> getBody​()
        Returns a CompletionStage 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 a String converted using the given Charset.

        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 a BodyProcessor 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 a BodyProcessor which stores the response body in a file opened with the given options and name. The file will be opened with the given options using FileChannel.open just before the body is read. Any exception thrown will be returned or thrown from HttpClient::send or HttpClient::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 in
        openOptions - the list of options to open the file with
        Returns:
        a body processor
      • asByteArrayConsumer

        static HttpResponse.BodyProcessor<Void> asByteArrayConsumer​(Consumer<Optional<byte[]>> consumer)
        Returns a BodyProcessor which provides the incoming body data to the provided Consumer of Optional<byte[]>. Each call to Consumer.accept() will contain a non empty Optional, except for the final invocation after all body data has been read, when the Optional 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 a BodyProcessor which stores the response body in a file opened with the given name. Has the same effect as calling asFile with the standard open options CREATE and WRITE

        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 from HttpResponse.body().
        Type Parameters:
        U - The type of the response body
        Parameters:
        value - the value to return from HttpResponse.body()
        Returns:
        a BodyProcessor