--- /dev/null 2017-10-28 22:49:55.551349757 -0700 +++ new/src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/common/FlowTube.java 2017-11-30 04:05:48.223601261 -0800 @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.incubator.http.internal.common; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.concurrent.Flow; + +/** + * FlowTube is an I/O abstraction that allows reading from and writing to a + * destination asynchronously. + * This is not a {@link Flow.Processor + * Flow.Processor<List<ByteBuffer>, List<ByteBuffer>>}, + * but rather models a publisher source and a subscriber sink in a bidirectional flow. + *
+ * The {@code connectFlows} method should be called to connect the bidirectional
+ * flow. A FlowTube supports handing over the same read subscription to different
+ * sequential read subscribers over time. When {@code connectFlows(writePublisher,
+ * readSubscriber} is called, the FlowTube will call {@code dropSubscription} on
+ * its former readSubscriber, and {@code onSubscribe} on its new readSubscriber.
+ */
+public interface FlowTube extends
+ Flow.Publisher>,
+ Flow.Subscriber
> {
+
+ /**
+ * A subscriber for reading from the bidirectional flow.
+ * A TubeSubscriber is a {@code Flow.Subscriber} that can be canceled
+ * by calling {@code dropSubscription()}.
+ * Once {@code dropSubscription()} is called, the {@code TubeSubscriber}
+ * should stop calling any method on its subscription.
+ */
+ static interface TubeSubscriber extends Flow.Subscriber
> {
+
+ /**
+ * Called when the flow is connected again, and the subscription
+ * is handed over to a new subscriber.
+ * Once {@code dropSubscription()} is called, the {@code TubeSubscriber}
+ * should stop calling any method on its subscription.
+ */
+ default void dropSubscription() { }
+
+ }
+
+ /**
+ * A publisher for writing to the bidirectional flow.
+ */
+ static interface TubePublisher extends Flow.Publisher
> {
+
+ }
+
+ /**
+ * Connects the bidirectional flows to a write {@code Publisher} and a
+ * read {@code Subscriber}. This method can be called sequentially
+ * several times to switch existing publishers and subscribers to a new
+ * pair of write subscriber and read publisher.
+ * @param writePublisher A new publisher for writing to the bidirectional flow.
+ * @param readSubscriber A new subscriber for reading from the bidirectional
+ * flow.
+ */
+ default void connectFlows(TubePublisher writePublisher,
+ TubeSubscriber readSubscriber) {
+
+ this.subscribe(readSubscriber);
+ writePublisher.subscribe(this);
+ }
+
+ /**
+ * Returns true if this flow was completed, either exceptionally
+ * or normally (EOF reached).
+ * @return true if the flow is finished
+ */
+ boolean isFinished();
+
+
+ /**
+ * Returns {@code s} if {@code s} is a {@code TubeSubscriber}, otherwise
+ * wraps it in a {@code TubeSubscriber}.
+ * Using the wrapper is only appropriate in the case where
+ * {@code dropSubscription} doesn't need to be implemented, and the
+ * {@code TubeSubscriber} is subscribed only once.
+ *
+ * @param s a subscriber for reading.
+ * @return a {@code TubeSubscriber}: either {@code s} if {@code s} is a
+ * {@code TubeSubscriber}, otherwise a {@code TubeSubscriber}
+ * wrapper that delegates to {@code s}
+ */
+ static TubeSubscriber asTubeSubscriber(Flow.Subscriber super List
> p) {
+ if (p instanceof TubePublisher) {
+ return (TubePublisher) p;
+ }
+ return new AbstractTubePublisher.TubePublisherWrapper(p);
+ }
+
+ /**
+ * Convenience abstract class for {@code TubePublisher} implementations.
+ * It is not required that a {@code TubePublisher} implementation extends
+ * this class.
+ */
+ static abstract class AbstractTubePublisher implements TubePublisher {
+ static final class TubePublisherWrapper extends AbstractTubePublisher {
+ final Flow.Publisher
> delegate;
+ public TubePublisherWrapper(Flow.Publisher
> delegate) {
+ this.delegate = delegate;
+ }
+ @Override
+ public void subscribe(Flow.Subscriber super List