src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/RequestPublishers.java
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56088 38fac6d0521d
child 56090 5c7fb702948a
equal deleted inserted replaced
56088:38fac6d0521d 56089:42208b2f224e
     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.incubator.http.internal;
       
    27 
       
    28 import java.io.File;
       
    29 import java.io.FileInputStream;
       
    30 import java.io.IOException;
       
    31 import java.io.InputStream;
       
    32 import java.io.UncheckedIOException;
       
    33 import java.nio.ByteBuffer;
       
    34 import java.nio.charset.Charset;
       
    35 import java.nio.file.Path;
       
    36 import java.security.AccessControlContext;
       
    37 import java.security.AccessController;
       
    38 import java.security.PrivilegedAction;
       
    39 import java.security.PrivilegedActionException;
       
    40 import java.security.PrivilegedExceptionAction;
       
    41 import java.util.ArrayList;
       
    42 import java.util.Collections;
       
    43 import java.util.Iterator;
       
    44 import java.util.List;
       
    45 import java.util.NoSuchElementException;
       
    46 import java.util.Objects;
       
    47 import java.util.concurrent.ConcurrentLinkedQueue;
       
    48 import java.util.concurrent.Flow;
       
    49 import java.util.concurrent.Flow.Publisher;
       
    50 import java.util.function.Supplier;
       
    51 import jdk.incubator.http.HttpRequest.BodyPublisher;
       
    52 import jdk.incubator.http.internal.common.Utils;
       
    53 
       
    54 public final class RequestPublishers {
       
    55 
       
    56     private RequestPublishers() { }
       
    57 
       
    58     public static class ByteArrayPublisher implements BodyPublisher {
       
    59         private volatile Flow.Publisher<ByteBuffer> delegate;
       
    60         private final int length;
       
    61         private final byte[] content;
       
    62         private final int offset;
       
    63         private final int bufSize;
       
    64 
       
    65         public ByteArrayPublisher(byte[] content) {
       
    66             this(content, 0, content.length);
       
    67         }
       
    68 
       
    69         public ByteArrayPublisher(byte[] content, int offset, int length) {
       
    70             this(content, offset, length, Utils.BUFSIZE);
       
    71         }
       
    72 
       
    73         /* bufSize exposed for testing purposes */
       
    74         ByteArrayPublisher(byte[] content, int offset, int length, int bufSize) {
       
    75             this.content = content;
       
    76             this.offset = offset;
       
    77             this.length = length;
       
    78             this.bufSize = bufSize;
       
    79         }
       
    80 
       
    81         List<ByteBuffer> copy(byte[] content, int offset, int length) {
       
    82             List<ByteBuffer> bufs = new ArrayList<>();
       
    83             while (length > 0) {
       
    84                 ByteBuffer b = ByteBuffer.allocate(Math.min(bufSize, length));
       
    85                 int max = b.capacity();
       
    86                 int tocopy = Math.min(max, length);
       
    87                 b.put(content, offset, tocopy);
       
    88                 offset += tocopy;
       
    89                 length -= tocopy;
       
    90                 b.flip();
       
    91                 bufs.add(b);
       
    92             }
       
    93             return bufs;
       
    94         }
       
    95 
       
    96         @Override
       
    97         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
    98             List<ByteBuffer> copy = copy(content, offset, length);
       
    99             this.delegate = new PullPublisher<>(copy);
       
   100             delegate.subscribe(subscriber);
       
   101         }
       
   102 
       
   103         @Override
       
   104         public long contentLength() {
       
   105             return length;
       
   106         }
       
   107     }
       
   108 
       
   109     // This implementation has lots of room for improvement.
       
   110     public static class IterablePublisher implements BodyPublisher {
       
   111         private volatile Flow.Publisher<ByteBuffer> delegate;
       
   112         private final Iterable<byte[]> content;
       
   113         private volatile long contentLength;
       
   114 
       
   115         public IterablePublisher(Iterable<byte[]> content) {
       
   116             this.content = Objects.requireNonNull(content);
       
   117         }
       
   118 
       
   119         // The ByteBufferIterator will iterate over the byte[] arrays in
       
   120         // the content one at the time.
       
   121         //
       
   122         class ByteBufferIterator implements Iterator<ByteBuffer> {
       
   123             final ConcurrentLinkedQueue<ByteBuffer> buffers = new ConcurrentLinkedQueue<>();
       
   124             final Iterator<byte[]> iterator = content.iterator();
       
   125             @Override
       
   126             public boolean hasNext() {
       
   127                 return !buffers.isEmpty() || iterator.hasNext();
       
   128             }
       
   129 
       
   130             @Override
       
   131             public ByteBuffer next() {
       
   132                 ByteBuffer buffer = buffers.poll();
       
   133                 while (buffer == null) {
       
   134                     copy();
       
   135                     buffer = buffers.poll();
       
   136                 }
       
   137                 return buffer;
       
   138             }
       
   139 
       
   140             ByteBuffer getBuffer() {
       
   141                 return Utils.getBuffer();
       
   142             }
       
   143 
       
   144             void copy() {
       
   145                 byte[] bytes = iterator.next();
       
   146                 int length = bytes.length;
       
   147                 if (length == 0 && iterator.hasNext()) {
       
   148                     // avoid inserting empty buffers, except
       
   149                     // if that's the last.
       
   150                     return;
       
   151                 }
       
   152                 int offset = 0;
       
   153                 do {
       
   154                     ByteBuffer b = getBuffer();
       
   155                     int max = b.capacity();
       
   156 
       
   157                     int tocopy = Math.min(max, length);
       
   158                     b.put(bytes, offset, tocopy);
       
   159                     offset += tocopy;
       
   160                     length -= tocopy;
       
   161                     b.flip();
       
   162                     buffers.add(b);
       
   163                 } while (length > 0);
       
   164             }
       
   165         }
       
   166 
       
   167         public Iterator<ByteBuffer> iterator() {
       
   168             return new ByteBufferIterator();
       
   169         }
       
   170 
       
   171         @Override
       
   172         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   173             Iterable<ByteBuffer> iterable = this::iterator;
       
   174             this.delegate = new PullPublisher<>(iterable);
       
   175             delegate.subscribe(subscriber);
       
   176         }
       
   177 
       
   178         static long computeLength(Iterable<byte[]> bytes) {
       
   179             long len = 0;
       
   180             for (byte[] b : bytes) {
       
   181                 len = Math.addExact(len, (long)b.length);
       
   182             }
       
   183             return len;
       
   184         }
       
   185 
       
   186         @Override
       
   187         public long contentLength() {
       
   188             if (contentLength == 0) {
       
   189                 synchronized(this) {
       
   190                     if (contentLength == 0) {
       
   191                         contentLength = computeLength(content);
       
   192                     }
       
   193                 }
       
   194             }
       
   195             return contentLength;
       
   196         }
       
   197     }
       
   198 
       
   199     public static class StringPublisher extends ByteArrayPublisher {
       
   200         public StringPublisher(String content, Charset charset) {
       
   201             super(content.getBytes(charset));
       
   202         }
       
   203     }
       
   204 
       
   205     public static class EmptyPublisher implements BodyPublisher {
       
   206         private final Flow.Publisher<ByteBuffer> delegate =
       
   207                 new PullPublisher<ByteBuffer>(Collections.emptyList(), null);
       
   208 
       
   209         @Override
       
   210         public long contentLength() {
       
   211             return 0;
       
   212         }
       
   213 
       
   214         @Override
       
   215         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   216             delegate.subscribe(subscriber);
       
   217         }
       
   218     }
       
   219 
       
   220     public static class FilePublisher implements BodyPublisher  {
       
   221         private final File file;
       
   222         private volatile AccessControlContext acc;
       
   223 
       
   224         public FilePublisher(Path name) {
       
   225             file = name.toFile();
       
   226         }
       
   227 
       
   228         void setAccessControlContext(AccessControlContext acc) {
       
   229             this.acc = acc;
       
   230         }
       
   231 
       
   232         @Override
       
   233         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   234             if (System.getSecurityManager() != null && acc == null)
       
   235                 throw new InternalError(
       
   236                         "Unexpected null acc when security manager has been installed");
       
   237 
       
   238             InputStream is;
       
   239             try {
       
   240                 PrivilegedExceptionAction<FileInputStream> pa =
       
   241                         () -> new FileInputStream(file);
       
   242                 is = AccessController.doPrivileged(pa, acc);
       
   243             } catch (PrivilegedActionException pae) {
       
   244                 throw new UncheckedIOException((IOException)pae.getCause());
       
   245             }
       
   246             PullPublisher<ByteBuffer> publisher =
       
   247                     new PullPublisher<>(() -> new StreamIterator(is));
       
   248             publisher.subscribe(subscriber);
       
   249         }
       
   250 
       
   251         @Override
       
   252         public long contentLength() {
       
   253             assert System.getSecurityManager() != null ? acc != null: true;
       
   254             PrivilegedAction<Long> pa = () -> file.length();
       
   255             return AccessController.doPrivileged(pa, acc);
       
   256         }
       
   257     }
       
   258 
       
   259     /**
       
   260      * Reads one buffer ahead all the time, blocking in hasNext()
       
   261      */
       
   262     public static class StreamIterator implements Iterator<ByteBuffer> {
       
   263         final InputStream is;
       
   264         final Supplier<? extends ByteBuffer> bufSupplier;
       
   265         volatile ByteBuffer nextBuffer;
       
   266         volatile boolean need2Read = true;
       
   267         volatile boolean haveNext;
       
   268 
       
   269         StreamIterator(InputStream is) {
       
   270             this(is, Utils::getBuffer);
       
   271         }
       
   272 
       
   273         StreamIterator(InputStream is, Supplier<? extends ByteBuffer> bufSupplier) {
       
   274             this.is = is;
       
   275             this.bufSupplier = bufSupplier;
       
   276         }
       
   277 
       
   278 //        Throwable error() {
       
   279 //            return error;
       
   280 //        }
       
   281 
       
   282         private int read() {
       
   283             nextBuffer = bufSupplier.get();
       
   284             nextBuffer.clear();
       
   285             byte[] buf = nextBuffer.array();
       
   286             int offset = nextBuffer.arrayOffset();
       
   287             int cap = nextBuffer.capacity();
       
   288             try {
       
   289                 int n = is.read(buf, offset, cap);
       
   290                 if (n == -1) {
       
   291                     is.close();
       
   292                     return -1;
       
   293                 }
       
   294                 //flip
       
   295                 nextBuffer.limit(n);
       
   296                 nextBuffer.position(0);
       
   297                 return n;
       
   298             } catch (IOException ex) {
       
   299                 return -1;
       
   300             }
       
   301         }
       
   302 
       
   303         @Override
       
   304         public synchronized boolean hasNext() {
       
   305             if (need2Read) {
       
   306                 haveNext = read() != -1;
       
   307                 if (haveNext) {
       
   308                     need2Read = false;
       
   309                 }
       
   310                 return haveNext;
       
   311             }
       
   312             return haveNext;
       
   313         }
       
   314 
       
   315         @Override
       
   316         public synchronized ByteBuffer next() {
       
   317             if (!hasNext()) {
       
   318                 throw new NoSuchElementException();
       
   319             }
       
   320             need2Read = true;
       
   321             return nextBuffer;
       
   322         }
       
   323 
       
   324     }
       
   325 
       
   326     public static class InputStreamPublisher implements BodyPublisher {
       
   327         private final Supplier<? extends InputStream> streamSupplier;
       
   328 
       
   329         public InputStreamPublisher(Supplier<? extends InputStream> streamSupplier) {
       
   330             this.streamSupplier = Objects.requireNonNull(streamSupplier);
       
   331         }
       
   332 
       
   333         @Override
       
   334         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   335             PullPublisher<ByteBuffer> publisher;
       
   336             InputStream is = streamSupplier.get();
       
   337             if (is == null) {
       
   338                 Throwable t = new IOException("streamSupplier returned null");
       
   339                 publisher = new PullPublisher<>(null, t);
       
   340             } else  {
       
   341                 publisher = new PullPublisher<>(iterableOf(is), null);
       
   342             }
       
   343             publisher.subscribe(subscriber);
       
   344         }
       
   345 
       
   346         protected Iterable<ByteBuffer> iterableOf(InputStream is) {
       
   347             return () -> new StreamIterator(is);
       
   348         }
       
   349 
       
   350         @Override
       
   351         public long contentLength() {
       
   352             return -1;
       
   353         }
       
   354     }
       
   355 
       
   356     public static final class PublisherAdapter implements BodyPublisher {
       
   357 
       
   358         private final Publisher<? extends ByteBuffer> publisher;
       
   359         private final long contentLength;
       
   360 
       
   361         public PublisherAdapter(Publisher<? extends ByteBuffer> publisher,
       
   362                          long contentLength) {
       
   363             this.publisher = Objects.requireNonNull(publisher);
       
   364             this.contentLength = contentLength;
       
   365         }
       
   366 
       
   367         @Override
       
   368         public final long contentLength() {
       
   369             return contentLength;
       
   370         }
       
   371 
       
   372         @Override
       
   373         public final void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
       
   374             publisher.subscribe(subscriber);
       
   375         }
       
   376     }
       
   377 }