src/java.net.http/share/classes/jdk/internal/net/http/LineSubscriberAdapter.java
author dfuchs
Mon, 23 Apr 2018 15:45:40 +0100
branchhttp-client-branch
changeset 56474 fe2bf7b369b8
parent 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
package jdk.internal.net.http;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.nio.CharBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.nio.charset.CharacterCodingException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.nio.charset.Charset;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.nio.charset.CharsetDecoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import java.nio.charset.CoderResult;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import java.nio.charset.CodingErrorAction;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.util.Objects;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.util.concurrent.ConcurrentLinkedDeque;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.concurrent.Flow;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.util.concurrent.Flow.Subscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.util.concurrent.Flow.Subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.util.concurrent.atomic.AtomicReference;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import java.util.function.Function;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import jdk.internal.net.http.common.Demand;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import java.net.http.HttpResponse.BodySubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import jdk.internal.net.http.common.SequentialScheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
/** An adapter between {@code BodySubscriber} and {@code Flow.Subscriber<String>}. */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
public final class LineSubscriberAdapter<S extends Subscriber<? super String>,R>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
        implements BodySubscriber<R> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
    private final CompletableFuture<R> cf = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
    private final S subscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
    private final Function<? super S, ? extends R> finisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
    private final Charset charset;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
    private final String eol;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    private volatile LineSubscription downstream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
    private LineSubscriberAdapter(S subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
                                  Function<? super S, ? extends R> finisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
                                  Charset charset,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
                                  String eol) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
        if (eol != null && eol.isEmpty())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
            throw new IllegalArgumentException("empty line separator");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
        this.subscriber = Objects.requireNonNull(subscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
        this.finisher = Objects.requireNonNull(finisher);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
        this.charset = Objects.requireNonNull(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
        this.eol = eol;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
    public void onSubscribe(Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
        downstream = LineSubscription.create(subscription,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
                                             charset,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
                                             eol,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
                                             subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
                                             cf);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
        subscriber.onSubscribe(downstream);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
    public void onNext(List<ByteBuffer> item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
            downstream.submit(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
        } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
            onError(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    public void onError(Throwable throwable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
            downstream.signalError(throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
            cf.completeExceptionally(throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    public void onComplete() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
            downstream.signalComplete();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
            cf.complete(finisher.apply(subscriber));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
    public CompletionStage<R> getBody() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
        return cf;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
    public static <S extends Subscriber<? super String>, R> LineSubscriberAdapter<S, R>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
    create(S subscriber, Function<? super S, ? extends R> finisher, Charset charset, String eol)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        if (eol != null && eol.isEmpty())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
            throw new IllegalArgumentException("empty line separator");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
        return new LineSubscriberAdapter<>(Objects.requireNonNull(subscriber),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
                Objects.requireNonNull(finisher),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                Objects.requireNonNull(charset),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
                eol);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
    static final class LineSubscription implements Flow.Subscription {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
        final Flow.Subscription upstreamSubscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
        final CharsetDecoder decoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
        final String newline;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        final Demand downstreamDemand;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        final ConcurrentLinkedDeque<ByteBuffer> queue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        final SequentialScheduler scheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        final Flow.Subscriber<? super String> upstream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        final CompletableFuture<?> cf;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
        private final AtomicReference<Throwable> errorRef = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
        private final AtomicLong demanded = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
        private volatile boolean completed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
        private volatile boolean cancelled;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
        private final char[] chars = new char[1024];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
        private final ByteBuffer leftover = ByteBuffer.wrap(new byte[64]);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
        private final CharBuffer buffer = CharBuffer.wrap(chars);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
        private final StringBuilder builder = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
        private String nextLine;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
        private LineSubscription(Flow.Subscription s,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
                                 CharsetDecoder dec,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
                                 String separator,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
                                 Flow.Subscriber<? super String> subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
                                 CompletableFuture<?> completion) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
            downstreamDemand = new Demand();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
            queue = new ConcurrentLinkedDeque<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
            upstreamSubscription = Objects.requireNonNull(s);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
            decoder = Objects.requireNonNull(dec);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
            newline = separator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
            upstream = Objects.requireNonNull(subscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
            cf = Objects.requireNonNull(completion);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
            scheduler = SequentialScheduler.synchronizedScheduler(this::loop);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
        public void request(long n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
            if (cancelled) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
            if (downstreamDemand.increase(n)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
                scheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
        public void cancel() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
            cancelled = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
            upstreamSubscription.cancel();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        public void submit(List<ByteBuffer> list) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
            queue.addAll(list);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
            demanded.decrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
            scheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
        public void signalComplete() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
            completed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            scheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
        public void signalError(Throwable error) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
            if (errorRef.compareAndSet(null,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
                    Objects.requireNonNull(error))) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
                scheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        // This method looks at whether some bytes where left over (in leftover)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        // from decoding the previous buffer when the previous buffer was in
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        // underflow. If so, it takes bytes one by one from the new buffer 'in'
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        // and combines them with the leftover bytes until 'in' is exhausted or a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        // character was produced in 'out', resolving the previous underflow.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        // Returns true if the buffer is still in underflow, false otherwise.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        // However, in both situation some chars might have been produced in 'out'.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        private boolean isUnderFlow(ByteBuffer in, CharBuffer out, boolean endOfInput)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
                throws CharacterCodingException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
            int limit = leftover.position();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
            if (limit == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
                // no leftover
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
                CoderResult res = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
                while (in.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
                    leftover.position(limit);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
                    leftover.limit(++limit);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
                    leftover.put(in.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
                    leftover.position(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
                    res = decoder.decode(leftover, out,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
                            endOfInput && !in.hasRemaining());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                    int remaining = leftover.remaining();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
                    if (remaining > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
                        assert leftover.position() == 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
                        leftover.position(remaining);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
                    } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
                        leftover.position(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
                    leftover.limit(leftover.capacity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
                    if (res.isUnderflow() && remaining > 0 && in.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
                        continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
                    if (res.isError()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
                        res.throwException();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
                    assert !res.isOverflow();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
                    return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
                return !endOfInput;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
        // extract characters from start to end and remove them from
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        // the StringBuilder
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
        private static String take(StringBuilder b, int start, int end) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
            assert start == 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
            String line;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
            if (end == start) return "";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
            line = b.substring(start, end);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
            b.delete(start, end);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
            return line;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
        // finds end of line, returns -1 if not found, or the position after
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
        // the line delimiter if found, removing the delimiter in the process.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
        private static int endOfLine(StringBuilder b, String eol, boolean endOfInput) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
            int len = b.length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
            if (eol != null) { // delimiter explicitly specified
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
                int i = b.indexOf(eol);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
                if (i >= 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
                    // remove the delimiter and returns the position
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
                    // of the char after it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
                    b.delete(i, i + eol.length());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
                    return i;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
            } else { // no delimiter specified, behaves as BufferedReader::readLine
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
                boolean crfound = false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
                for (int i = 0; i < len; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
                    char c = b.charAt(i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
                    if (c == '\n') {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
                        // '\n' or '\r\n' found.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
                        // remove the delimiter and returns the position
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                        // of the char after it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
                        b.delete(crfound ? i - 1 : i, i + 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
                        return crfound ? i - 1 : i;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
                    } else if (crfound) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
                        // previous char was '\r', c != '\n'
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
                        assert i != 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
                        // remove the delimiter and returns the position
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
                        // of the char after it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
                        b.delete(i - 1, i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
                        return i - 1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                    crfound = c == '\r';
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                if (crfound && endOfInput) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                    // remove the delimiter and returns the position
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
                    // of the char after it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
                    b.delete(len - 1, len);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                    return len - 1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
            return endOfInput && len > 0 ? len : -1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        // Looks at whether the StringBuilder contains a line.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
        // Returns null if more character are needed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
        private static String nextLine(StringBuilder b, String eol, boolean endOfInput) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
            int next = endOfLine(b, eol, endOfInput);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
            return (next > -1) ? take(b, 0, next) : null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
        // Attempts to read the next line. Returns the next line if
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        // the delimiter was found, null otherwise. The delimiters are
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        // consumed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
        private String nextLine()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
                throws CharacterCodingException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
            assert nextLine == null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
            LINES:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
            while (nextLine == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                boolean endOfInput = completed && queue.isEmpty();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
                nextLine = nextLine(builder, newline,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
                        endOfInput && leftover.position() == 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
                if (nextLine != null) return nextLine;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                ByteBuffer b;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                BUFFERS:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
                while ((b = queue.peek()) != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                    if (!b.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
                        queue.poll();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                        continue BUFFERS;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
                    BYTES:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
                    while (b.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
                        buffer.position(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
                        buffer.limit(buffer.capacity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
                        boolean endofInput = completed && queue.size() <= 1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
                        if (isUnderFlow(b, buffer, endofInput)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
                            assert !b.hasRemaining();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
                            if (buffer.position() > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
                                buffer.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
                                builder.append(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
                            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
                            continue BUFFERS;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
                        CoderResult res = decoder.decode(b, buffer, endofInput);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
                        if (res.isError()) res.throwException();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
                        if (buffer.position() > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
                            buffer.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                            builder.append(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                            continue LINES;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                        if (res.isUnderflow() && b.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
                            //System.out.println("underflow: adding " + b.remaining() + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                            leftover.put(b);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                            assert !b.hasRemaining();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
                            continue BUFFERS;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
                assert queue.isEmpty();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
                if (endOfInput) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
                    // Time to cleanup: there may be some undecoded leftover bytes
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
                    // We need to flush them out.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
                    // The decoder has been configured to replace malformed/unmappable
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
                    // chars with some replacement, in order to behave like
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
                    // InputStreamReader.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
                    leftover.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
                    buffer.position(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
                    buffer.limit(buffer.capacity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
                    // decode() must be called just before flush, even if there
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
                    // is nothing to decode. We must do this even if leftover
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
                    // has no remaining bytes.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
                    CoderResult res = decoder.decode(leftover, buffer, endOfInput);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
                    if (buffer.position() > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
                        buffer.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
                        builder.append(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
                    if (res.isError()) res.throwException();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
                    // Now call decoder.flush()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
                    buffer.position(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
                    buffer.limit(buffer.capacity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
                    res = decoder.flush(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
                    if (buffer.position() > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
                        buffer.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
                        builder.append(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
                    if (res.isError()) res.throwException();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
                    // It's possible that we reach here twice - just for the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
                    // purpose of checking that no bytes were left over, so
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
                    // we reset leftover/decoder to make the function reentrant.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
                    leftover.position(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
                    leftover.limit(leftover.capacity());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
                    decoder.reset();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
                    // if some chars were produced then this call will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
                    // return them.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
                    return nextLine = nextLine(builder, newline, endOfInput);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
            return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
        // The main sequential scheduler loop.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
        private void loop() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
                while (!cancelled) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
                    Throwable error = errorRef.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
                    if (error != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
                        cancelled = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
                        scheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
                        upstream.onError(error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
                        cf.completeExceptionally(error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
                        return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
                    if (nextLine == null) nextLine = nextLine();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
                    if (nextLine == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
                        if (completed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
                            scheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   407
                            if (leftover.position() != 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
                                // Underflow: not all bytes could be
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
                                // decoded, but no more bytes will be coming.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
                                // This should not happen as we should already
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   411
                                // have got a MalformedInputException, or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   412
                                // replaced the unmappable chars.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
                                errorRef.compareAndSet(null,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
                                        new IllegalStateException(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
                                                "premature end of input ("
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
                                                        + leftover.position()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   417
                                                        + " undecoded bytes)"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   418
                                continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   419
                            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   420
                                upstream.onComplete();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   421
                            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   422
                            return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   423
                        } else if (demanded.get() == 0
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   424
                                && !downstreamDemand.isFulfilled()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   425
                            long incr = Math.max(1, downstreamDemand.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   426
                            demanded.addAndGet(incr);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   427
                            upstreamSubscription.request(incr);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   428
                            continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   429
                        } else return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   430
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   431
                    assert nextLine != null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   432
                    assert newline != null && !nextLine.endsWith(newline)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   433
                            || !nextLine.endsWith("\n") || !nextLine.endsWith("\r");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   434
                    if (downstreamDemand.tryDecrement()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   435
                        String forward = nextLine;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   436
                        nextLine = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   437
                        upstream.onNext(forward);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   438
                    } else return; // no demand: come back later
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   439
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   440
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   441
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   442
                    upstreamSubscription.cancel();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
                } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
                    signalError(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   448
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   449
        static LineSubscription create(Flow.Subscription s,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   450
                                       Charset charset,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   451
                                       String lineSeparator,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   452
                                       Flow.Subscriber<? super String> upstream,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   453
                                       CompletableFuture<?> cf) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   454
            return new LineSubscription(Objects.requireNonNull(s),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   455
                    Objects.requireNonNull(charset).newDecoder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   456
                            // use the same decoder configuration than
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   457
                            // java.io.InputStreamReader
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   458
                            .onMalformedInput(CodingErrorAction.REPLACE)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   459
                            .onUnmappableCharacter(CodingErrorAction.REPLACE),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   460
                    lineSeparator,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   461
                    Objects.requireNonNull(upstream),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   462
                    Objects.requireNonNull(cf));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   463
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   464
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   465
}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   466