src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56097 15dc43936d39
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
       
     1 /*
       
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.internal.net.http;
       
    27 
       
    28 import java.io.BufferedReader;
       
    29 import java.io.IOException;
       
    30 import java.io.InputStream;
       
    31 import java.io.InputStreamReader;
       
    32 import java.lang.System.Logger.Level;
       
    33 import java.nio.ByteBuffer;
       
    34 import java.nio.channels.FileChannel;
       
    35 import java.nio.charset.Charset;
       
    36 import java.nio.file.OpenOption;
       
    37 import java.nio.file.Path;
       
    38 import java.security.AccessControlContext;
       
    39 import java.security.AccessController;
       
    40 import java.security.PrivilegedActionException;
       
    41 import java.security.PrivilegedExceptionAction;
       
    42 import java.util.ArrayList;
       
    43 import java.util.Iterator;
       
    44 import java.util.List;
       
    45 import java.util.Objects;
       
    46 import java.util.Optional;
       
    47 import java.util.concurrent.ArrayBlockingQueue;
       
    48 import java.util.concurrent.BlockingQueue;
       
    49 import java.util.concurrent.CompletableFuture;
       
    50 import java.util.concurrent.CompletionStage;
       
    51 import java.util.concurrent.ConcurrentHashMap;
       
    52 import java.util.concurrent.Executor;
       
    53 import java.util.concurrent.Flow;
       
    54 import java.util.concurrent.Flow.Subscriber;
       
    55 import java.util.concurrent.Flow.Subscription;
       
    56 import java.util.concurrent.atomic.AtomicBoolean;
       
    57 import java.util.function.Consumer;
       
    58 import java.util.function.Function;
       
    59 import java.util.stream.Stream;
       
    60 import java.net.http.HttpResponse.BodySubscriber;
       
    61 import jdk.internal.net.http.common.MinimalFuture;
       
    62 import jdk.internal.net.http.common.Utils;
       
    63 import static java.nio.charset.StandardCharsets.UTF_8;
       
    64 
       
    65 public class ResponseSubscribers {
       
    66 
       
    67     public static class ConsumerSubscriber implements BodySubscriber<Void> {
       
    68         private final Consumer<Optional<byte[]>> consumer;
       
    69         private Flow.Subscription subscription;
       
    70         private final CompletableFuture<Void> result = new MinimalFuture<>();
       
    71         private final AtomicBoolean subscribed = new AtomicBoolean();
       
    72 
       
    73         public ConsumerSubscriber(Consumer<Optional<byte[]>> consumer) {
       
    74             this.consumer = Objects.requireNonNull(consumer);
       
    75         }
       
    76 
       
    77         @Override
       
    78         public CompletionStage<Void> getBody() {
       
    79             return result;
       
    80         }
       
    81 
       
    82         @Override
       
    83         public void onSubscribe(Flow.Subscription subscription) {
       
    84             if (!subscribed.compareAndSet(false, true)) {
       
    85                 subscription.cancel();
       
    86             } else {
       
    87                 this.subscription = subscription;
       
    88                 subscription.request(1);
       
    89             }
       
    90         }
       
    91 
       
    92         @Override
       
    93         public void onNext(List<ByteBuffer> items) {
       
    94             for (ByteBuffer item : items) {
       
    95                 byte[] buf = new byte[item.remaining()];
       
    96                 item.get(buf);
       
    97                 consumer.accept(Optional.of(buf));
       
    98             }
       
    99             subscription.request(1);
       
   100         }
       
   101 
       
   102         @Override
       
   103         public void onError(Throwable throwable) {
       
   104             result.completeExceptionally(throwable);
       
   105         }
       
   106 
       
   107         @Override
       
   108         public void onComplete() {
       
   109             consumer.accept(Optional.empty());
       
   110             result.complete(null);
       
   111         }
       
   112 
       
   113     }
       
   114 
       
   115     public static class PathSubscriber implements BodySubscriber<Path> {
       
   116 
       
   117         private final Path file;
       
   118         private final CompletableFuture<Path> result = new MinimalFuture<>();
       
   119 
       
   120         private volatile Flow.Subscription subscription;
       
   121         private volatile FileChannel out;
       
   122         private volatile AccessControlContext acc;
       
   123         private final OpenOption[] options;
       
   124 
       
   125         public PathSubscriber(Path file, OpenOption... options) {
       
   126             this.file = file;
       
   127             this.options = options;
       
   128         }
       
   129 
       
   130         public void setAccessControlContext(AccessControlContext acc) {
       
   131             this.acc = acc;
       
   132         }
       
   133 
       
   134         @Override
       
   135         public void onSubscribe(Flow.Subscription subscription) {
       
   136             if (System.getSecurityManager() != null && acc == null)
       
   137                 throw new InternalError(
       
   138                         "Unexpected null acc when security manager has been installed");
       
   139 
       
   140             this.subscription = subscription;
       
   141             try {
       
   142                 PrivilegedExceptionAction<FileChannel> pa =
       
   143                         () -> FileChannel.open(file, options);
       
   144                 out = AccessController.doPrivileged(pa, acc);
       
   145             } catch (PrivilegedActionException pae) {
       
   146                 Throwable t = pae.getCause() != null ? pae.getCause() : pae;
       
   147                 result.completeExceptionally(t);
       
   148                 subscription.cancel();
       
   149                 return;
       
   150             }
       
   151             subscription.request(1);
       
   152         }
       
   153 
       
   154         @Override
       
   155         public void onNext(List<ByteBuffer> items) {
       
   156             try {
       
   157                 out.write(items.toArray(Utils.EMPTY_BB_ARRAY));
       
   158             } catch (IOException ex) {
       
   159                 Utils.close(out);
       
   160                 subscription.cancel();
       
   161                 result.completeExceptionally(ex);
       
   162             }
       
   163             subscription.request(1);
       
   164         }
       
   165 
       
   166         @Override
       
   167         public void onError(Throwable e) {
       
   168             result.completeExceptionally(e);
       
   169             Utils.close(out);
       
   170         }
       
   171 
       
   172         @Override
       
   173         public void onComplete() {
       
   174             Utils.close(out);
       
   175             result.complete(file);
       
   176         }
       
   177 
       
   178         @Override
       
   179         public CompletionStage<Path> getBody() {
       
   180             return result;
       
   181         }
       
   182     }
       
   183 
       
   184     public static class ByteArraySubscriber<T> implements BodySubscriber<T> {
       
   185         private final Function<byte[], T> finisher;
       
   186         private final CompletableFuture<T> result = new MinimalFuture<>();
       
   187         private final List<ByteBuffer> received = new ArrayList<>();
       
   188 
       
   189         private volatile Flow.Subscription subscription;
       
   190 
       
   191         public ByteArraySubscriber(Function<byte[],T> finisher) {
       
   192             this.finisher = finisher;
       
   193         }
       
   194 
       
   195         @Override
       
   196         public void onSubscribe(Flow.Subscription subscription) {
       
   197             if (this.subscription != null) {
       
   198                 subscription.cancel();
       
   199                 return;
       
   200             }
       
   201             this.subscription = subscription;
       
   202             // We can handle whatever you've got
       
   203             subscription.request(Long.MAX_VALUE);
       
   204         }
       
   205 
       
   206         @Override
       
   207         public void onNext(List<ByteBuffer> items) {
       
   208             // incoming buffers are allocated by http client internally,
       
   209             // and won't be used anywhere except this place.
       
   210             // So it's free simply to store them for further processing.
       
   211             assert Utils.hasRemaining(items);
       
   212             received.addAll(items);
       
   213         }
       
   214 
       
   215         @Override
       
   216         public void onError(Throwable throwable) {
       
   217             received.clear();
       
   218             result.completeExceptionally(throwable);
       
   219         }
       
   220 
       
   221         static private byte[] join(List<ByteBuffer> bytes) {
       
   222             int size = Utils.remaining(bytes, Integer.MAX_VALUE);
       
   223             byte[] res = new byte[size];
       
   224             int from = 0;
       
   225             for (ByteBuffer b : bytes) {
       
   226                 int l = b.remaining();
       
   227                 b.get(res, from, l);
       
   228                 from += l;
       
   229             }
       
   230             return res;
       
   231         }
       
   232 
       
   233         @Override
       
   234         public void onComplete() {
       
   235             try {
       
   236                 result.complete(finisher.apply(join(received)));
       
   237                 received.clear();
       
   238             } catch (IllegalArgumentException e) {
       
   239                 result.completeExceptionally(e);
       
   240             }
       
   241         }
       
   242 
       
   243         @Override
       
   244         public CompletionStage<T> getBody() {
       
   245             return result;
       
   246         }
       
   247     }
       
   248 
       
   249     /**
       
   250      * An InputStream built on top of the Flow API.
       
   251      */
       
   252     public static class HttpResponseInputStream extends InputStream
       
   253         implements BodySubscriber<InputStream>
       
   254     {
       
   255         final static boolean DEBUG = Utils.DEBUG;
       
   256         final static int MAX_BUFFERS_IN_QUEUE = 1;  // lock-step with the producer
       
   257 
       
   258         // An immutable ByteBuffer sentinel to mark that the last byte was received.
       
   259         private static final ByteBuffer LAST_BUFFER = ByteBuffer.wrap(new byte[0]);
       
   260         private static final List<ByteBuffer> LAST_LIST = List.of(LAST_BUFFER);
       
   261         private static final System.Logger DEBUG_LOGGER =
       
   262                 Utils.getDebugLogger("HttpResponseInputStream"::toString, DEBUG);
       
   263 
       
   264         // A queue of yet unprocessed ByteBuffers received from the flow API.
       
   265         private final BlockingQueue<List<ByteBuffer>> buffers;
       
   266         private volatile Flow.Subscription subscription;
       
   267         private volatile boolean closed;
       
   268         private volatile Throwable failed;
       
   269         private volatile Iterator<ByteBuffer> currentListItr;
       
   270         private volatile ByteBuffer currentBuffer;
       
   271         private final AtomicBoolean subscribed = new AtomicBoolean();
       
   272 
       
   273         public HttpResponseInputStream() {
       
   274             this(MAX_BUFFERS_IN_QUEUE);
       
   275         }
       
   276 
       
   277         HttpResponseInputStream(int maxBuffers) {
       
   278             int capacity = (maxBuffers <= 0 ? MAX_BUFFERS_IN_QUEUE : maxBuffers);
       
   279             // 1 additional slot needed for LAST_LIST added by onComplete
       
   280             this.buffers = new ArrayBlockingQueue<>(capacity + 1);
       
   281         }
       
   282 
       
   283         @Override
       
   284         public CompletionStage<InputStream> getBody() {
       
   285             // Returns the stream immediately, before the
       
   286             // response body is received.
       
   287             // This makes it possible for sendAsync().get().body()
       
   288             // to complete before the response body is received.
       
   289             return CompletableFuture.completedStage(this);
       
   290         }
       
   291 
       
   292         // Returns the current byte buffer to read from.
       
   293         // If the current buffer has no remaining data, this method will take the
       
   294         // next buffer from the buffers queue, possibly blocking until
       
   295         // a new buffer is made available through the Flow API, or the
       
   296         // end of the flow has been reached.
       
   297         private ByteBuffer current() throws IOException {
       
   298             while (currentBuffer == null || !currentBuffer.hasRemaining()) {
       
   299                 // Check whether the stream is closed or exhausted
       
   300                 if (closed || failed != null) {
       
   301                     throw new IOException("closed", failed);
       
   302                 }
       
   303                 if (currentBuffer == LAST_BUFFER) break;
       
   304 
       
   305                 try {
       
   306                     if (currentListItr == null || !currentListItr.hasNext()) {
       
   307                         // Take a new list of buffers from the queue, blocking
       
   308                         // if none is available yet...
       
   309 
       
   310                         DEBUG_LOGGER.log(Level.DEBUG, "Taking list of Buffers");
       
   311                         List<ByteBuffer> lb = buffers.take();
       
   312                         currentListItr = lb.iterator();
       
   313                         DEBUG_LOGGER.log(Level.DEBUG, "List of Buffers Taken");
       
   314 
       
   315                         // Check whether an exception was encountered upstream
       
   316                         if (closed || failed != null)
       
   317                             throw new IOException("closed", failed);
       
   318 
       
   319                         // Check whether we're done.
       
   320                         if (lb == LAST_LIST) {
       
   321                             currentListItr = null;
       
   322                             currentBuffer = LAST_BUFFER;
       
   323                             break;
       
   324                         }
       
   325 
       
   326                         // Request another upstream item ( list of buffers )
       
   327                         Flow.Subscription s = subscription;
       
   328                         if (s != null) {
       
   329                             DEBUG_LOGGER.log(Level.DEBUG, "Increased demand by 1");
       
   330                             s.request(1);
       
   331                         }
       
   332                         assert currentListItr != null;
       
   333                         if (lb.isEmpty()) continue;
       
   334                     }
       
   335                     assert currentListItr != null;
       
   336                     assert currentListItr.hasNext();
       
   337                     DEBUG_LOGGER.log(Level.DEBUG, "Next Buffer");
       
   338                     currentBuffer = currentListItr.next();
       
   339                 } catch (InterruptedException ex) {
       
   340                     // continue
       
   341                 }
       
   342             }
       
   343             assert currentBuffer == LAST_BUFFER || currentBuffer.hasRemaining();
       
   344             return currentBuffer;
       
   345         }
       
   346 
       
   347         @Override
       
   348         public int read(byte[] bytes, int off, int len) throws IOException {
       
   349             // get the buffer to read from, possibly blocking if
       
   350             // none is available
       
   351             ByteBuffer buffer;
       
   352             if ((buffer = current()) == LAST_BUFFER) return -1;
       
   353 
       
   354             // don't attempt to read more than what is available
       
   355             // in the current buffer.
       
   356             int read = Math.min(buffer.remaining(), len);
       
   357             assert read > 0 && read <= buffer.remaining();
       
   358 
       
   359             // buffer.get() will do the boundary check for us.
       
   360             buffer.get(bytes, off, read);
       
   361             return read;
       
   362         }
       
   363 
       
   364         @Override
       
   365         public int read() throws IOException {
       
   366             ByteBuffer buffer;
       
   367             if ((buffer = current()) == LAST_BUFFER) return -1;
       
   368             return buffer.get() & 0xFF;
       
   369         }
       
   370 
       
   371         @Override
       
   372         public void onSubscribe(Flow.Subscription s) {
       
   373             try {
       
   374                 if (!subscribed.compareAndSet(false, true)) {
       
   375                     s.cancel();
       
   376                 } else {
       
   377                     // check whether the stream is already closed.
       
   378                     // if so, we should cancel the subscription
       
   379                     // immediately.
       
   380                     boolean closed;
       
   381                     synchronized (this) {
       
   382                         closed = this.closed;
       
   383                         if (!closed) {
       
   384                             this.subscription = s;
       
   385                         }
       
   386                     }
       
   387                     if (closed) {
       
   388                         s.cancel();
       
   389                         return;
       
   390                     }
       
   391                     assert buffers.remainingCapacity() > 1; // should contain at least 2
       
   392                     DEBUG_LOGGER.log(Level.DEBUG, () -> "onSubscribe: requesting "
       
   393                             + Math.max(1, buffers.remainingCapacity() - 1));
       
   394                     s.request(Math.max(1, buffers.remainingCapacity() - 1));
       
   395                 }
       
   396             } catch (Throwable t) {
       
   397                 failed = t;
       
   398                 try {
       
   399                     close();
       
   400                 } catch (IOException x) {
       
   401                     // OK
       
   402                 } finally {
       
   403                     onError(t);
       
   404                 }
       
   405             }
       
   406         }
       
   407 
       
   408         @Override
       
   409         public void onNext(List<ByteBuffer> t) {
       
   410             Objects.requireNonNull(t);
       
   411             try {
       
   412                 DEBUG_LOGGER.log(Level.DEBUG, "next item received");
       
   413                 if (!buffers.offer(t)) {
       
   414                     throw new IllegalStateException("queue is full");
       
   415                 }
       
   416                 DEBUG_LOGGER.log(Level.DEBUG, "item offered");
       
   417             } catch (Throwable ex) {
       
   418                 failed = ex;
       
   419                 try {
       
   420                     close();
       
   421                 } catch (IOException ex1) {
       
   422                     // OK
       
   423                 } finally {
       
   424                     onError(ex);
       
   425                 }
       
   426             }
       
   427         }
       
   428 
       
   429         @Override
       
   430         public void onError(Throwable thrwbl) {
       
   431             subscription = null;
       
   432             failed = Objects.requireNonNull(thrwbl);
       
   433             // The client process that reads the input stream might
       
   434             // be blocked in queue.take().
       
   435             // Tries to offer LAST_LIST to the queue. If the queue is
       
   436             // full we don't care if we can't insert this buffer, as
       
   437             // the client can't be blocked in queue.take() in that case.
       
   438             // Adding LAST_LIST to the queue is harmless, as the client
       
   439             // should find failed != null before handling LAST_LIST.
       
   440             buffers.offer(LAST_LIST);
       
   441         }
       
   442 
       
   443         @Override
       
   444         public void onComplete() {
       
   445             subscription = null;
       
   446             onNext(LAST_LIST);
       
   447         }
       
   448 
       
   449         @Override
       
   450         public void close() throws IOException {
       
   451             Flow.Subscription s;
       
   452             synchronized (this) {
       
   453                 if (closed) return;
       
   454                 closed = true;
       
   455                 s = subscription;
       
   456                 subscription = null;
       
   457             }
       
   458             // s will be null if already completed
       
   459             try {
       
   460                 if (s != null) {
       
   461                     s.cancel();
       
   462                 }
       
   463             } finally {
       
   464                 buffers.offer(LAST_LIST);
       
   465                 super.close();
       
   466             }
       
   467         }
       
   468 
       
   469     }
       
   470 
       
   471     public static BodySubscriber<Stream<String>> createLineStream() {
       
   472         return createLineStream(UTF_8);
       
   473     }
       
   474 
       
   475     public static BodySubscriber<Stream<String>> createLineStream(Charset charset) {
       
   476         Objects.requireNonNull(charset);
       
   477         BodySubscriber<InputStream> s = new HttpResponseInputStream();
       
   478         return new MappedSubscriber<InputStream,Stream<String>>(s,
       
   479             (InputStream stream) -> {
       
   480                 return new BufferedReader(new InputStreamReader(stream, charset))
       
   481                             .lines().onClose(() -> Utils.close(stream));
       
   482             });
       
   483     }
       
   484 
       
   485     /**
       
   486      * Currently this consumes all of the data and ignores it
       
   487      */
       
   488     public static class NullSubscriber<T> implements BodySubscriber<T> {
       
   489 
       
   490         private final CompletableFuture<T> cf = new MinimalFuture<>();
       
   491         private final Optional<T> result;
       
   492         private final AtomicBoolean subscribed = new AtomicBoolean();
       
   493 
       
   494         public NullSubscriber(Optional<T> result) {
       
   495             this.result = result;
       
   496         }
       
   497 
       
   498         @Override
       
   499         public void onSubscribe(Flow.Subscription subscription) {
       
   500             if (!subscribed.compareAndSet(false, true)) {
       
   501                 subscription.cancel();
       
   502             } else {
       
   503                 subscription.request(Long.MAX_VALUE);
       
   504             }
       
   505         }
       
   506 
       
   507         @Override
       
   508         public void onNext(List<ByteBuffer> items) {
       
   509             Objects.requireNonNull(items);
       
   510         }
       
   511 
       
   512         @Override
       
   513         public void onError(Throwable throwable) {
       
   514             cf.completeExceptionally(throwable);
       
   515         }
       
   516 
       
   517         @Override
       
   518         public void onComplete() {
       
   519             if (result.isPresent()) {
       
   520                 cf.complete(result.get());
       
   521             } else {
       
   522                 cf.complete(null);
       
   523             }
       
   524         }
       
   525 
       
   526         @Override
       
   527         public CompletionStage<T> getBody() {
       
   528             return cf;
       
   529         }
       
   530     }
       
   531 
       
   532     /** An adapter between {@code BodySubscriber} and {@code Flow.Subscriber}. */
       
   533     public static final class SubscriberAdapter<S extends Subscriber<? super List<ByteBuffer>>,R>
       
   534         implements BodySubscriber<R>
       
   535     {
       
   536         private final CompletableFuture<R> cf = new MinimalFuture<>();
       
   537         private final S subscriber;
       
   538         private final Function<S,R> finisher;
       
   539         private volatile Subscription subscription;
       
   540 
       
   541         public SubscriberAdapter(S subscriber, Function<S,R> finisher) {
       
   542             this.subscriber = Objects.requireNonNull(subscriber);
       
   543             this.finisher = Objects.requireNonNull(finisher);
       
   544         }
       
   545 
       
   546         @Override
       
   547         public void onSubscribe(Subscription subscription) {
       
   548             Objects.requireNonNull(subscription);
       
   549             if (this.subscription != null) {
       
   550                 subscription.cancel();
       
   551             } else {
       
   552                 this.subscription = subscription;
       
   553                 subscriber.onSubscribe(subscription);
       
   554             }
       
   555         }
       
   556 
       
   557         @Override
       
   558         public void onNext(List<ByteBuffer> item) {
       
   559             Objects.requireNonNull(item);
       
   560             try {
       
   561                 subscriber.onNext(item);
       
   562             } catch (Throwable throwable) {
       
   563                 subscription.cancel();
       
   564                 onError(throwable);
       
   565             }
       
   566         }
       
   567 
       
   568         @Override
       
   569         public void onError(Throwable throwable) {
       
   570             Objects.requireNonNull(throwable);
       
   571             try {
       
   572                 subscriber.onError(throwable);
       
   573             } finally {
       
   574                 cf.completeExceptionally(throwable);
       
   575             }
       
   576         }
       
   577 
       
   578         @Override
       
   579         public void onComplete() {
       
   580             try {
       
   581                 subscriber.onComplete();
       
   582             } finally {
       
   583                 try {
       
   584                     cf.complete(finisher.apply(subscriber));
       
   585                 } catch (Throwable throwable) {
       
   586                     cf.completeExceptionally(throwable);
       
   587                 }
       
   588             }
       
   589         }
       
   590 
       
   591         @Override
       
   592         public CompletionStage<R> getBody() {
       
   593             return cf;
       
   594         }
       
   595     }
       
   596 
       
   597     /**
       
   598      * A body subscriber which receives input from an upstream subscriber
       
   599      * and maps that subscriber's body type to a new type. The upstream subscriber
       
   600      * delegates all flow operations directly to this object. The
       
   601      * {@link CompletionStage} returned by {@link #getBody()}} takes the output
       
   602      * of the upstream {@code getBody()} and applies the mapper function to
       
   603      * obtain the new {@code CompletionStage} type.
       
   604      *
       
   605      * Uses an Executor that must be set externally.
       
   606      *
       
   607      * @param <T> the upstream body type
       
   608      * @param <U> this subscriber's body type
       
   609      */
       
   610     public static class MappedSubscriber<T,U> implements BodySubscriber<U> {
       
   611         final BodySubscriber<T> upstream;
       
   612         final Function<T,U> mapper;
       
   613 
       
   614         /**
       
   615          *
       
   616          * @param upstream
       
   617          * @param mapper
       
   618          */
       
   619         public MappedSubscriber(BodySubscriber<T> upstream, Function<T,U> mapper) {
       
   620             this.upstream = upstream;
       
   621             this.mapper = mapper;
       
   622         }
       
   623 
       
   624         @Override
       
   625         public CompletionStage<U> getBody() {
       
   626             return upstream.getBody()
       
   627                     .thenApply(mapper);
       
   628         }
       
   629 
       
   630         @Override
       
   631         public void onSubscribe(Flow.Subscription subscription) {
       
   632             upstream.onSubscribe(subscription);
       
   633         }
       
   634 
       
   635         @Override
       
   636         public void onNext(List<ByteBuffer> item) {
       
   637             upstream.onNext(item);
       
   638         }
       
   639 
       
   640         @Override
       
   641         public void onError(Throwable throwable) {
       
   642             upstream.onError(throwable);
       
   643         }
       
   644 
       
   645         @Override
       
   646         public void onComplete() {
       
   647             upstream.onComplete();
       
   648         }
       
   649     }
       
   650 }