java.lang.Object
com.github.mizosoft.methanol.MoreBodySubscribers
Factory for additional
HttpResponse.BodySubscriber
implementations.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
S extends Flow.Subscriber<? super List<ByteBuffer>>>
HttpResponse.BodySubscriber<T> fromAsyncSubscriber
(S downstream, Function<? super S, ? extends CompletionStage<T>> asyncFinisher) Returns aBodySubscriber
that forwards the response body to the given downstream.Returns a completedBodySubscriber
ofReadableByteChannel
that reads the response body.static <T> HttpResponse.BodySubscriber
<Supplier<T>> ofDeferredObject
(TypeRef<T> type, @Nullable MediaType mediaType) Returns aBodySubscriber
that decodes the response body into an object of the given type using an installeddecoder
.static <T> HttpResponse.BodySubscriber
<T> Returns aBodySubscriber
that decodes the response body into an object of the given type using an installeddecoder
.static HttpResponse.BodySubscriber
<Reader> Returns a completedBodySubscriber
ofReader
that reads the response body as a stream of characters decoded using the given charset.static <T> HttpResponse.BodySubscriber
<T> withReadTimeout
(HttpResponse.BodySubscriber<T> downstream, Duration timeout) Returns aBodySubscriber
that completes the given downstream withHttpReadTimeoutException
if a requested signal is not received within the given timeout.static <T> HttpResponse.BodySubscriber
<T> withReadTimeout
(HttpResponse.BodySubscriber<T> downstream, Duration timeout, ScheduledExecutorService scheduler) Returns aBodySubscriber
that completes the given downstream withHttpReadTimeoutException
if a requested signal is not received within the given timeout.
-
Method Details
-
fromAsyncSubscriber
public static <T,S extends Flow.Subscriber<? super List<ByteBuffer>>> HttpResponse.BodySubscriber<T> fromAsyncSubscriber(S downstream, Function<? super S, ? extends CompletionStage<T>> asyncFinisher) Returns aBodySubscriber
that forwards the response body to the given downstream. The response body's completion depends on the completion of theCompletionStage
returned by the given function. UnlikeHttpResponse.BodySubscribers.fromSubscriber(Subscriber, Function)
, the given subscriber'sonComplete
oronError
need not be called for the body to complete. -
withReadTimeout
public static <T> HttpResponse.BodySubscriber<T> withReadTimeout(HttpResponse.BodySubscriber<T> downstream, Duration timeout) Returns aBodySubscriber
that completes the given downstream withHttpReadTimeoutException
if a requested signal is not received within the given timeout. A system-wide scheduler is used to schedule timeout events.- Throws:
IllegalArgumentException
- if the timeout is non-positive
-
withReadTimeout
public static <T> HttpResponse.BodySubscriber<T> withReadTimeout(HttpResponse.BodySubscriber<T> downstream, Duration timeout, ScheduledExecutorService scheduler) Returns aBodySubscriber
that completes the given downstream withHttpReadTimeoutException
if a requested signal is not received within the given timeout. The givenScheduledExecutorService
is used to schedule timeout events.- Throws:
IllegalArgumentException
- if the timeout is non-positive
-
ofByteChannel
Returns a completedBodySubscriber
ofReadableByteChannel
that reads the response body. The channel returned by the subscriber isinterruptible
.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
Returns a completedBodySubscriber
ofReader
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> HttpResponse.BodySubscriber<T> ofObject(TypeRef<T> type, @Nullable MediaType mediaType) Returns aBodySubscriber
that decodes the response body into an object of the given type using an installeddecoder
.- Throws:
UnsupportedOperationException
- if noBodyAdapter.Decoder
that supports the given object type or media type is installed
-
ofDeferredObject
public static <T> HttpResponse.BodySubscriber<Supplier<T>> ofDeferredObject(TypeRef<T> type, @Nullable MediaType mediaType) Returns aBodySubscriber
that decodes the response body into an object of the given type using an installeddecoder
.- Throws:
UnsupportedOperationException
- if noBodyAdapter.Decoder
that supports the given object type or media type is installed
-