< prev index next >

src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpResponse.java

Print this page

        

@@ -37,10 +37,11 @@
 import java.nio.charset.StandardCharsets;
 import java.nio.file.OpenOption;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
 import java.util.concurrent.Flow;

@@ -268,10 +269,28 @@
                     return BodyProcessor.asString(charset);
                 }
                 return BodyProcessor.asString(charsetFrom(headers));
             };
         }
+        /**
+         * Returns a {@code BodyHandler} which returns {@linkplain 
+         * BodyProcessor#buffering(BodyProcessor,long) buffering BodyProcessors} that 
+         * buffer data before delivering it to the processors created by the given 
+         * {@code downstream} handler. These {@code BodyProcessor} 
+         * instances are created by calling
+         * {@link BodyProcessor#buffering(BodyProcessor,long) BodyProcessor.buffering()} with
+         * a processor obtained from the {@code downstream} handler and
+         * the {@code buffersize} parameter supplied to this call.
+         *
+         * @param downstream the downstream handler from the returned handler
+         * @param buffersize the buffersize parameter passed to {@link BodyProcessor#buffering(BodyProcessor,long)
+         *        BodyProcessor.buffering()}
+         * @return a body handler
+         */
+        public static <T> BodyHandler<T> buffering(BodyHandler<T> downstream, long buffersize) {
+            return new BufferingHandler<>(downstream, buffersize);
+        }
 
 
         /**
          * Returns a {@code BodyHandler<Path>} that returns a
          * {@link BodyProcessor BodyProcessor}{@code <Path>} obtained from

@@ -401,13 +420,13 @@
 
     /**
      * A processor for response bodies.
      * {@Incubating}
      * <p>
-     * The object acts as a {@link Flow.Subscriber}&lt;{@link ByteBuffer}&gt; to
-     * the HTTP client implementation which publishes ByteBuffers containing the
-     * response body. The processor converts the incoming buffers of data to
+     * The object acts as a {@link Flow.Subscriber}&lt;{@link List}&lt;{@link 
+     * ByteBuffer}&gt;&gt; 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 {@code T}.
      * <p>
      * The {@link #getBody()} method returns a {@link CompletionStage}{@code <T>}
      * that provides the response body object. The {@code CompletionStage} must
      * be obtainable at any time. When it completes depends on the nature

@@ -417,11 +436,11 @@
      * body has been read, because the calling code uses it to consume the data.
      *
      * @param <T> the response body type
      */
     public interface BodyProcessor<T>
-            extends Flow.Subscriber<ByteBuffer> {
+            extends Flow.Subscriber<List<ByteBuffer>> {
 
         /**
          * Returns a {@code CompletionStage} which when completed will return the
          * response body object.
          *

@@ -444,10 +463,32 @@
                     bytes -> new String(bytes, charset)
             );
         }
 
         /**
+         * Returns a {@code BodyProcessor} which buffers data before delivering
+         * it to the given downstream processor. The processor guarantees to deliver
+         * {@code buffersize} bytes of data in each call to {@link #onNext(Object) onNext}
+         * except for the final call, just before {@link #onComplete() onComplete} 
+         * is called. The final call of {@code onNext} may contain fewer than 
+         * {@code buffersize}
+         * bytes.
+         * <p>
+         * The returned processor delegates its {@link #getBody()} call to the 
+         * down stream processor.
+         *
+         * @param downstream the downstream processor for the returned handler
+         * @param buffersize the number of bytes guaranteed to be returned in each
+         *        call to onNext() except for the last call.
+         *
+         * @return a body processor
+         */
+        public static <T> BodyProcessor<T> buffering(BodyProcessor<T> downstream, long buffersize) {
+            return new BufferingProcessor<>(downstream, buffersize);
+        }
+
+        /**
          * Returns a {@code BodyProcessor} which stores the response body as a
          * byte array.
          * <p>
          * The {@link HttpResponse} using this processor is available after the
          * entire response has been read.
< prev index next >