Module methanol

Class MoreBodySubscribers


  • public class MoreBodySubscribers
    extends java.lang.Object
    Static factories for additional HttpResponse.BodySubscriber implementations.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​S extends java.util.concurrent.Flow.Subscriber<? super java.util.List<java.nio.ByteBuffer>>>
      java.net.http.HttpResponse.BodySubscriber<T>
      fromAsyncSubscriber​(S downstream, java.util.function.Function<? super S,​? extends java.util.concurrent.CompletionStage<T>> asyncFinisher)
      Returns a BodySubscriber that forwards the response body to the given downstream.
      static java.net.http.HttpResponse.BodySubscriber<java.nio.channels.ReadableByteChannel> ofByteChannel()
      Returns a completed BodySubscriber of ReadableByteChannel that reads the response body.
      static <T> java.net.http.HttpResponse.BodySubscriber<java.util.function.Supplier<T>> ofDeferredObject​(TypeRef<T> type, @Nullable MediaType mediaType)
      Returns a BodySubscriber that decodes the response body into an object of the given type using an installed decoder.
      static <T> java.net.http.HttpResponse.BodySubscriber<T> ofObject​(TypeRef<T> type, @Nullable MediaType mediaType)
      Returns a BodySubscriber that decodes the response body into an object of the given type using an installed decoder.
      static java.net.http.HttpResponse.BodySubscriber<java.io.Reader> ofReader​(java.nio.charset.Charset charset)
      Returns a completed BodySubscriber of Reader that reads the response body as a stream of characters decoded using the given charset.
      static <T> java.net.http.HttpResponse.BodySubscriber<T> withReadTimeout​(java.net.http.HttpResponse.BodySubscriber<T> downstream, java.time.Duration timeout)
      Returns a BodySubscriber that completes the given downstream with HttpReadTimeoutException if a requested signal is not received within the given timeout.
      static <T> java.net.http.HttpResponse.BodySubscriber<T> withReadTimeout​(java.net.http.HttpResponse.BodySubscriber<T> downstream, java.time.Duration timeout, java.util.concurrent.ScheduledExecutorService scheduler)
      Returns a BodySubscriber that completes the given downstream with HttpReadTimeoutException if a requested signal is not received within the given timeout.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • fromAsyncSubscriber

        public static <T,​S extends java.util.concurrent.Flow.Subscriber<? super java.util.List<java.nio.ByteBuffer>>> java.net.http.HttpResponse.BodySubscriber<T> fromAsyncSubscriber​(S downstream,
                                                                                                                                                                                             java.util.function.Function<? super S,​? extends java.util.concurrent.CompletionStage<T>> asyncFinisher)
        Returns a BodySubscriber that forwards the response body to the given downstream. The response body's completion depends on the completion of the CompletionStage returned by the given function. Unlike HttpResponse.BodySubscribers.fromSubscriber(Subscriber, Function), the given subscriber's onComplete or onError need not be called for the body to complete.
      • withReadTimeout

        public static <T> java.net.http.HttpResponse.BodySubscriber<T> withReadTimeout​(java.net.http.HttpResponse.BodySubscriber<T> downstream,
                                                                                       java.time.Duration timeout)
        Returns a BodySubscriber that completes the given downstream with HttpReadTimeoutException if a requested signal is not received within the given timeout. A system-wide scheduler is used to schedule timeout events.
        Throws:
        java.lang.IllegalArgumentException - if the timeout is non-positive
      • withReadTimeout

        public static <T> java.net.http.HttpResponse.BodySubscriber<T> withReadTimeout​(java.net.http.HttpResponse.BodySubscriber<T> downstream,
                                                                                       java.time.Duration timeout,
                                                                                       java.util.concurrent.ScheduledExecutorService scheduler)
        Returns a BodySubscriber that completes the given downstream with HttpReadTimeoutException if a requested signal is not received within the given timeout. The given ScheduledExecutorService is used to schedule timeout events.
        Throws:
        java.lang.IllegalArgumentException - if the timeout is non-positive
      • ofByteChannel

        public static java.net.http.HttpResponse.BodySubscriber<java.nio.channels.ReadableByteChannel> ofByteChannel()
        Returns a completed BodySubscriber of ReadableByteChannel that reads the response body. The channel returned by the subscriber is interruptible.

        To ensure proper release of resources, the channel should be fully consumed until EOF is reached. If such consumption cannot be guaranteed, either the channel should be eventually closed or the thread which is blocked on reading the channel should be interrupted. Note, however, that doing so will render the underlying connection unusable for subsequent requests.

      • ofReader

        public static java.net.http.HttpResponse.BodySubscriber<java.io.Reader> ofReader​(java.nio.charset.Charset charset)
        Returns a completed BodySubscriber of Reader that reads the response body as a stream of characters decoded using the given charset.

        To ensure proper release of resources, the reader should be fully consumed until EOF is reached. If such consumption cannot be guaranteed, the reader should be eventually closed. Note, however, that doing so will render the underlying connection unusable for subsequent requests.

      • ofObject

        public static <T> java.net.http.HttpResponse.BodySubscriber<T> ofObject​(TypeRef<T> type,
                                                                                @Nullable MediaType mediaType)
        Returns a BodySubscriber that decodes the response body into an object of the given type using an installed decoder.
        Throws:
        java.lang.UnsupportedOperationException - if no BodyAdapter.Decoder that supports the given object type or media type is installed
      • ofDeferredObject

        public static <T> java.net.http.HttpResponse.BodySubscriber<java.util.function.Supplier<T>> ofDeferredObject​(TypeRef<T> type,
                                                                                                                     @Nullable MediaType mediaType)
        Returns a BodySubscriber that decodes the response body into an object of the given type using an installed decoder.
        Throws:
        java.lang.UnsupportedOperationException - if no BodyAdapter.Decoder that supports the given object type or media type is installed