jdk/src/java.httpclient/share/classes/java/net/http/WebSocket.java
author prappo
Wed, 08 Jun 2016 15:19:58 +0100
changeset 38864 bf2b41533aed
parent 38856 cc3a0d1e96e0
child 39133 b5641ce64cf7
permissions -rw-r--r--
8156693: Improve usability of CompletableFuture use in WebSocket API Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     4
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.io.IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.net.ProtocolException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.net.URI;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.util.Map;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.util.Objects;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.util.Optional;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.util.concurrent.CompletableFuture;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.util.concurrent.CompletionStage;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.util.concurrent.TimeUnit;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.stream.Stream;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
/**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
 * A WebSocket client conforming to RFC 6455.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
 * <p> A {@code WebSocket} provides full-duplex communication over a TCP
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
 * connection.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
 * <p> To create a {@code WebSocket} use a {@linkplain #newBuilder(URI, Listener)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
 * builder}. Once a {@code WebSocket} is obtained, it's ready to send and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
 * receive messages. When the {@code WebSocket} is no longer
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
 * needed it must be closed: a Close message must both be {@linkplain
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
 * #sendClose() sent} and {@linkplain Listener#onClose(WebSocket, Optional,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
 * String) received}. Or to close abruptly, {@link #abort()} is called. Once
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
 * closed it remains closed, cannot be reopened.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
 * <p> Messages of type {@code X} are sent through the {@code WebSocket.sendX}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
 * methods and received through {@link WebSocket.Listener}{@code .onX} methods
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
    55
 * asynchronously. Each of the methods returns a {@link CompletionStage} which
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
    56
 * completes when the operation has completed.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
 * <p> Messages are received only if {@linkplain #request(long) requested}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
 * <p> One outstanding send operation is permitted: if another send operation is
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
 * initiated before the previous one has completed, an {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
 * IllegalStateException IllegalStateException} will be thrown. When sending, a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
 * message should not be modified until the returned {@code CompletableFuture}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
 * completes (either normally or exceptionally).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
 * <p> Messages can be sent and received as a whole or in parts. A whole message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
 * is a sequence of one or more messages in which the last message is marked
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
 * when it is sent or received.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
 * <p> If the message is contained in a {@link ByteBuffer}, bytes are considered
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
 * arranged from the {@code buffer}'s {@link ByteBuffer#position() position} to
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
 * the {@code buffer}'s {@link ByteBuffer#limit() limit}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
 * <p> All message exchange is run by the threads belonging to the {@linkplain
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
 * HttpClient#executorService() executor service} of {@code WebSocket}'s {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
 * HttpClient}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
 * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
 * or method of this type will cause a {@link NullPointerException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
 * NullPointerException} to be thrown.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
 *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
    82
 * @implNote The default implementation's methods do not block before returning
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
    83
 * a {@code CompletableFuture}.
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
    84
 *
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
 * @since 9
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
public interface WebSocket {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
     * Creates a builder of {@code WebSocket}s connected to the given URI and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
     * receiving events with the given {@code Listener}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
     * <p> Equivalent to:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
     * <pre>{@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
     *     WebSocket.newBuilder(uri, HttpClient.getDefault())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
     * }</pre>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
     * @param uri
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
     *         the WebSocket URI as defined in the WebSocket Protocol
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
     *         (with "ws" or "wss" scheme)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
     * @param listener
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
     *         the listener
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
     * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
     *         if the {@code uri} is not a WebSocket URI
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
     * @throws SecurityException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
     *         if running under a security manager and the caller does
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
     *         not have permission to access the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
     *         {@linkplain HttpClient#getDefault() default HttpClient}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
     * @return a builder
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
    static Builder newBuilder(URI uri, Listener listener) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
        return newBuilder(uri, HttpClient.getDefault(), listener);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
     * Creates a builder of {@code WebSocket}s connected to the given URI and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
     * receiving events with the given {@code Listener}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
     * <p> Providing a custom {@code client} allows for finer control over the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
     * opening handshake.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
     * <p> <b>Example</b>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
     * <pre>{@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
     *     HttpClient client = HttpClient.create()
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
     *             .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
     *             .build();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
     *     ...
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
     *     WebSocket.newBuilder(URI.create("ws://websocket.example.com"), client, listener)...
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
     * }</pre>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
     * @param uri
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
     *         the WebSocket URI as defined in the WebSocket Protocol
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
     *         (with "ws" or "wss" scheme)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
     * @param client
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
     *         the HttpClient
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
     * @param listener
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
     *         the listener
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
     * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
     *         if the uri is not a WebSocket URI
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
     * @return a builder
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
    static Builder newBuilder(URI uri, HttpClient client, Listener listener) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
        return new WSBuilder(uri, client, listener);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
     * A builder for creating {@code WebSocket} instances.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
     * <p> To build a {@code WebSocket}, instantiate a builder, configure it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
     * as required by calling intermediate methods (the ones that return the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
     * builder itself), then finally call {@link #buildAsync()} to get a {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
     * CompletableFuture} with resulting {@code WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
     * <p> If an intermediate method has not been called, an appropriate
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
     * default value (or behavior) will be used. Unless otherwise noted, a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
     * repeated call to an intermediate method overwrites the previous value (or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
     * overrides the previous behaviour), if no exception is thrown.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
     * <p> Instances of {@code Builder} may not be safe for use by multiple
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
     * threads.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   167
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   168
     * @since 9
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
    interface Builder {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
         * Adds the given name-value pair to the list of additional headers for
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
         * the opening handshake.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
         * <p> Headers defined in WebSocket Protocol are not allowed to be added.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
         * @param name
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
         *         the header name
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
         * @param value
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
         *         the header value
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
         * @return this builder
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
         * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
         *         if the {@code name} is a WebSocket defined header name
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
        Builder header(String name, String value);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
         * Includes a request for the given subprotocols during the opening
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
         * handshake.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
         * <p> Among the requested subprotocols at most one will be chosen by
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
         * the server. When the {@code WebSocket} is connected, the subprotocol
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
         * in use is available from {@link WebSocket#getSubprotocol}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
         * Subprotocols may be specified in the order of preference.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
         * <p> Each of the given subprotocols must conform to the relevant
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
         * rules defined in the WebSocket Protocol.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
         * @param mostPreferred
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
         *         the most preferred subprotocol
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
         * @param lesserPreferred
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
         *         the lesser preferred subprotocols, with the least preferred
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
         *         at the end
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
         * @return this builder
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
         * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
         *         if any of the WebSocket Protocol rules relevant to
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
         *         subprotocols are violated
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
        Builder subprotocols(String mostPreferred, String... lesserPreferred);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
         * Sets a timeout for the opening handshake.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
         * <p> If the opening handshake is not finished within the specified
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
         * timeout then {@link #buildAsync()} completes exceptionally with a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
         * {@code HttpTimeoutException}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
         * <p> If the timeout is not specified then it's deemed infinite.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
         * @param timeout
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
         *         the maximum time to wait
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
         * @param unit
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
         *         the time unit of the timeout argument
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
         * @return this builder
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
         * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
         *         if the {@code timeout} is negative
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
        Builder connectTimeout(long timeout, TimeUnit unit);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
         * Builds a {@code WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
         *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   240
         * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   241
         * normally with the {@code WebSocket} when it is connected or completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   242
         * exceptionally if an error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
         * <p> {@code CompletableFuture} may complete exceptionally with the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
         * following errors:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
         * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
         * <li> {@link IOException}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
         *          if an I/O error occurs
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
         * <li> {@link InterruptedException}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
         *          if the operation was interrupted
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
         * <li> {@link SecurityException}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
         *          if a security manager is set, and the caller does not
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
         *          have a {@link java.net.URLPermission} for the WebSocket URI
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
         * <li> {@link WebSocketHandshakeException}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
         *          if the opening handshake fails
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
         * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
         *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   258
         * @return a {@code CompletableFuture} with the {@code WebSocket}
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
        CompletableFuture<WebSocket> buildAsync();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
     * A listener for events and messages on a {@code WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
     * <p> Each method below corresponds to a type of event.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
     * <li> {@link #onOpen onOpen} <br>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
     * This method is always the first to be invoked.
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   270
     * <li> {@link #onText(WebSocket, CharSequence, WebSocket.MessagePart)
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
     * onText}, {@link #onBinary(WebSocket, ByteBuffer, WebSocket.MessagePart)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
     * onBinary}, {@link #onPing(WebSocket, ByteBuffer) onPing} and {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
     * #onPong(WebSocket, ByteBuffer) onPong} <br>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   274
     * These methods are invoked zero or more times after {@code onOpen}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   275
     * <li> {@link #onClose(WebSocket, Optional, String) onClose}, {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
     * #onError(WebSocket, Throwable) onError} <br>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   277
     * Only one of these methods is invoked, and that method is invoked last and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   278
     * at most once.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   279
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   280
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   281
     * <pre><code>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   282
     *     onOpen (onText|onBinary|onPing|onPong)* (onClose|onError)?
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   283
     * </code></pre>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   284
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   285
     * <p> Messages received by the {@code Listener} conform to the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   286
     * Protocol, otherwise {@code onError} with a {@link ProtocolException} is
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   287
     * invoked.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   288
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   289
     * <p> If a whole message is received, then the corresponding method
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   290
     * ({@code onText} or {@code onBinary}) will be invoked with {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   291
     * WebSocket.MessagePart#WHOLE WHOLE} marker. Otherwise the method will be
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   292
     * invoked with {@link WebSocket.MessagePart#FIRST FIRST}, zero or more
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   293
     * times with {@link WebSocket.MessagePart#FIRST PART} and, finally, with
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   294
     * {@link WebSocket.MessagePart#LAST LAST} markers.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   295
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   296
     * <pre><code>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   297
     *     WHOLE|(FIRST PART* LAST)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   298
     * </code></pre>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   299
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   300
     * <p> All methods are invoked in a sequential (and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   301
     * <a href="../../../java/util/concurrent/package-summary.html#MemoryVisibility">
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   302
     * happens-before</a>) order, one after another, possibly by different
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   303
     * threads. If any of the methods above throws an exception, {@code onError}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   304
     * is then invoked with that exception. Exceptions thrown from {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   305
     * onError} or {@code onClose} are ignored.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   306
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   307
     * <p> When the method returns, the message is deemed received. After this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   308
     * another messages may be received.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   309
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   310
     * <p> These invocations begin asynchronous processing which might not end
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   311
     * with the invocation. To provide coordination, methods of {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   312
     * Listener} return a {@link CompletionStage CompletionStage}. The {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   313
     * CompletionStage} signals the {@code WebSocket} that the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   314
     * processing of a message has ended. For
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   315
     * convenience, methods may return {@code null}, which means
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   316
     * the same as returning an already completed {@code CompletionStage}. If
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   317
     * the returned {@code CompletionStage} completes exceptionally, then {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   318
     * #onError(WebSocket, Throwable) onError} will be invoked with the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   319
     * exception.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   320
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   321
     * <p> Control of the message passes to the {@code Listener} with the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   322
     * invocation of the method. Control of the message returns to the {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   323
     * WebSocket} at the earliest of, either returning {@code null} from the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   324
     * method, or the completion of the {@code CompletionStage} returned from
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   325
     * the method. The {@code WebSocket} does not access the message while it's
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   326
     * not in its control. The {@code Listener} must not access the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   327
     * after its control has been returned to the {@code WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   328
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   329
     * <p> It is the responsibility of the listener to make additional
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   330
     * {@linkplain WebSocket#request(long) message requests}, when ready, so
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   331
     * that messages are received eventually.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   332
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   333
     * <p> Methods above are never invoked with {@code null}s as their
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   334
     * arguments.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   335
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   336
     * @since 9
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   337
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   338
    interface Listener {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   339
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   340
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   341
         * Notifies the {@code Listener} that it is connected to the provided
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   342
         * {@code WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   343
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   344
         * <p> The {@code onOpen} method does not correspond to any message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   345
         * from the WebSocket Protocol. It is a synthetic event. It is the first
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   346
         * {@code Listener}'s method to be invoked. No other {@code Listener}'s
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   347
         * methods are invoked before this one. The method is usually used to
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   348
         * make an initial {@linkplain WebSocket#request(long) request} for
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   349
         * messages.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   350
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   351
         * <p> If an exception is thrown from this method then {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   352
         * #onError(WebSocket, Throwable) onError} will be invoked with the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   353
         * exception.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   354
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   355
         * @implSpec The default implementation {@linkplain WebSocket#request(long)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   356
         * requests one message}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   357
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   358
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   359
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   360
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   361
        default void onOpen(WebSocket webSocket) { webSocket.request(1); }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   362
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   363
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   364
         * Receives a Text message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   365
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   366
         * <p> The {@code onText} method is invoked zero or more times between
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   367
         * {@code onOpen} and ({@code onClose} or {@code onError}).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   368
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   369
         * <p> This message may be a partial UTF-16 sequence. However, the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   370
         * concatenation of all messages through the last will be a whole UTF-16
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   371
         * sequence.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   372
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   373
         * <p> If an exception is thrown from this method or the returned {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   374
         * CompletionStage} completes exceptionally, then {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   375
         * #onError(WebSocket, Throwable) onError} will be invoked with the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   376
         * exception.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   377
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   378
         * @implSpec The default implementation {@linkplain WebSocket#request(long)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   379
         * requests one more message}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   380
         *
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   381
         * @implNote This implementation passes only complete UTF-16 sequences
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   382
         * to the {@code onText} method.
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   383
         *
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   384
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   385
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   386
         * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   387
         *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   388
         * @param part
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   389
         *         the part
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   390
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   391
         * @return a CompletionStage that completes when the message processing
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   392
         * is done; or {@code null} if already done
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   393
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   394
        default CompletionStage<?> onText(WebSocket webSocket,
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   395
                                          CharSequence message,
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   396
                                          MessagePart part) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   397
            webSocket.request(1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   398
            return null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   399
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   400
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   401
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   402
         * Receives a Binary message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   403
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   404
         * <p> The {@code onBinary} method is invoked zero or more times
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   405
         * between {@code onOpen} and ({@code onClose} or {@code onError}).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   406
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   407
         * <p> If an exception is thrown from this method or the returned {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   408
         * CompletionStage} completes exceptionally, then {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   409
         * #onError(WebSocket, Throwable) onError} will be invoked with this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   410
         * exception.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   411
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   412
         * @implSpec The default implementation {@linkplain WebSocket#request(long)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   413
         * requests one more message}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   414
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   415
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   416
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   417
         * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   418
         *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   419
         * @param part
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   420
         *         the part
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   421
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   422
         * @return a CompletionStage that completes when the message processing
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   423
         * is done; or {@code null} if already done
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   424
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   425
        default CompletionStage<?> onBinary(WebSocket webSocket,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   426
                                            ByteBuffer message,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   427
                                            MessagePart part) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   428
            webSocket.request(1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   429
            return null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   430
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   431
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   432
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   433
         * Receives a Ping message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   434
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   435
         * <p> A Ping message may be sent or received by either client or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   436
         * server. It may serve either as a keepalive or as a means to verify
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   437
         * that the remote endpoint is still responsive.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   438
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   439
         * <p> The message will consist of not more than {@code 125} bytes:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   440
         * {@code message.remaining() <= 125}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   441
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   442
         * <p> The {@code onPing} is invoked zero or more times in between
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   443
         * {@code onOpen} and ({@code onClose} or {@code onError}).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   444
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   445
         * <p> If an exception is thrown from this method or the returned {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   446
         * CompletionStage} completes exceptionally, then {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   447
         * #onError(WebSocket, Throwable) onError} will be invoked with this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   448
         * exception.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   449
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   450
         * @implNote
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   451
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   452
         * <p> Replies with a Pong message and requests one more message when
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   453
         * the Pong has been sent.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   454
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   455
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   456
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   457
         * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   458
         *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   459
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   460
         * @return a CompletionStage that completes when the message processing
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   461
         * is done; or {@code null} if already done
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   462
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   463
        default CompletionStage<?> onPing(WebSocket webSocket,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   464
                                          ByteBuffer message) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   465
            return webSocket.sendPong(message).thenRun(() -> webSocket.request(1));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   466
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   467
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   468
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   469
         * Receives a Pong message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   470
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   471
         * <p> A Pong message may be unsolicited or may be received in response
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   472
         * to a previously sent Ping. In the latter case, the contents of the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   473
         * Pong is identical to the originating Ping.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   474
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   475
         * <p> The message will consist of not more than {@code 125} bytes:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   476
         * {@code message.remaining() <= 125}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   477
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   478
         * <p> The {@code onPong} method is invoked zero or more times in
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   479
         * between {@code onOpen} and ({@code onClose} or {@code onError}).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   480
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   481
         * <p> If an exception is thrown from this method or the returned {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   482
         * CompletionStage} completes exceptionally, then {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   483
         * #onError(WebSocket, Throwable) onError} will be invoked with this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   484
         * exception.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   485
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   486
         * @implSpec The default implementation {@linkplain WebSocket#request(long)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   487
         * requests one more message}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   488
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   489
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   490
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   491
         * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   492
         *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   493
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   494
         * @return a CompletionStage that completes when the message processing
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   495
         * is done; or {@code null} if already done
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   496
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   497
        default CompletionStage<?> onPong(WebSocket webSocket,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   498
                                          ByteBuffer message) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   499
            webSocket.request(1);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   500
            return null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   501
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   502
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   503
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   504
         * Receives a Close message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   505
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   506
         * <p> Once a Close message is received, the server will not send any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   507
         * more messages.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   508
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   509
         * <p> A Close message may consist of a close code and a reason for
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   510
         * closing. The reason will have a UTF-8 representation not longer than
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   511
         * {@code 123} bytes. The reason may be useful for debugging or passing
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   512
         * information relevant to the connection but is not necessarily human
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   513
         * readable.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   514
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   515
         * <p> {@code onClose} is the last invocation on the {@code Listener}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   516
         * It is invoked at most once, but after {@code onOpen}. If an exception
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   517
         * is thrown from this method, it is ignored.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   518
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   519
         * @implSpec The default implementation does nothing.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   520
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   521
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   522
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   523
         * @param code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   524
         *         an {@code Optional} describing the close code, or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   525
         *         an empty {@code Optional} if the message doesn't contain it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   526
         * @param reason
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   527
         *         the reason of close; can be empty
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   528
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   529
        default void onClose(WebSocket webSocket, Optional<CloseCode> code,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   530
                             String reason) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   531
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   532
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   533
         * Notifies an I/O or protocol error has occurred on the {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   534
         * WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   535
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   536
         * <p> The {@code onError} method does not correspond to any message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   537
         * from the WebSocket Protocol. It is a synthetic event. {@code onError}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   538
         * is the last invocation on the {@code Listener}. It is invoked at most
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   539
         * once but after {@code onOpen}. If an exception is thrown from this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   540
         * method, it is ignored.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   541
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   542
         * <p> The WebSocket Protocol requires some errors occurs in the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   543
         * incoming destination must be fatal to the connection. In such cases
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   544
         * the implementation takes care of closing the {@code WebSocket}. By
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   545
         * the time {@code onError} is invoked, no more messages can be sent on
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   546
         * this {@code WebSocket}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   547
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   548
         * @apiNote Errors associated with send operations ({@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   549
         * WebSocket#sendText(CharSequence, boolean) sendText}, {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   550
         * #sendBinary(ByteBuffer, boolean) sendBinary}, {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   551
         * #sendPing(ByteBuffer) sendPing}, {@link #sendPong(ByteBuffer)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   552
         * sendPong} and {@link #sendClose(CloseCode, CharSequence) sendClose})
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   553
         * are reported to the {@code CompletionStage} operations return.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   554
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   555
         * @implSpec The default implementation does nothing.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   556
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   557
         * @param webSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   558
         *         the WebSocket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   559
         * @param error
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   560
         *         the error
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   561
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   562
        default void onError(WebSocket webSocket, Throwable error) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   563
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   564
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   565
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   566
     * A marker used by {@link WebSocket.Listener} for partial message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   567
     * receiving.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   568
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   569
     * @since 9
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   570
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   571
    enum MessagePart {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   572
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   573
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   574
         * The first part of a message in a sequence.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   575
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   576
        FIRST,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   577
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   578
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   579
         * A middle part of a message in a sequence.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   580
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   581
        PART,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   582
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   583
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   584
         * The last part of a message in a sequence.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   585
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   586
        LAST,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   587
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   588
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   589
         * A whole message. The message consists of a single part.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   590
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   591
        WHOLE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   592
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   593
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   594
         * Tells whether a part of a message received with this marker is the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   595
         * last part.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   596
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   597
         * @return {@code true} if LAST or WHOLE, {@code false} otherwise
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   598
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   599
        public boolean isLast() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   600
            return this == LAST || this == WHOLE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   601
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   602
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   603
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   604
    /**
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   605
     * Sends a Text message with characters from the given {@code CharSequence}.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   606
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   607
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   608
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   609
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   610
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   611
     * <p> The {@code CharSequence} should not be modified until the returned
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   612
     * {@code CompletableFuture} completes (either normally or exceptionally).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   613
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   614
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   615
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   616
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   617
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   618
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   619
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   620
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   621
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   622
     *          or if there is an outstanding send operation;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   623
     *          or if a previous Binary message was not sent with {@code isLast == true}
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   624
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   625
     *
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   626
     * @implNote This implementation does not accept partial UTF-16
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   627
     * sequences. In case such a sequence is passed, a returned {@code
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   628
     * CompletableFuture} completes exceptionally.
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   629
     *
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   630
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   631
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   632
     * @param isLast
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   633
     *         {@code true} if this is the final part of the message,
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   634
     *         {@code false} otherwise
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   635
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   636
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   637
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   638
     * @throws IllegalArgumentException
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   639
     *         if {@code message} is a malformed (or an incomplete) UTF-16 sequence
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   640
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   641
    CompletableFuture<WebSocket> sendText(CharSequence message, boolean isLast);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   642
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   643
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   644
     * Sends a whole Text message with characters from the given {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   645
     * CharSequence}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   646
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   647
     * <p> This is a convenience method. For the general case, use {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   648
     * #sendText(CharSequence, boolean)}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   649
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   650
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   651
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   652
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   653
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   654
     * <p> The {@code CharSequence} should not be modified until the returned
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   655
     * {@code CompletableFuture} completes (either normally or exceptionally).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   656
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   657
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   658
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   659
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   660
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   661
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   662
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   663
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   664
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   665
     *          or if there is an outstanding send operation;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   666
     *          or if a previous Binary message was not sent with {@code isLast == true}
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   667
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   668
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   669
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   670
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   671
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   672
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   673
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   674
     * @throws IllegalArgumentException
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   675
     *         if {@code message} is a malformed (or an incomplete) UTF-16 sequence
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   676
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   677
    default CompletableFuture<WebSocket> sendText(CharSequence message) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   678
        return sendText(message, true);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   679
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   680
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   681
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   682
     * Sends a whole Text message with characters from {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   683
     * CharacterSequence}s provided by the given {@code Stream}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   684
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   685
     * <p> This is a convenience method. For the general case use {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   686
     * #sendText(CharSequence, boolean)}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   687
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   688
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   689
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   690
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   691
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   692
     * <p> Streamed character sequences should not be modified until the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   693
     * returned {@code CompletableFuture} completes (either normally or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   694
     * exceptionally).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   695
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   696
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   697
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   698
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   699
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   700
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   701
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   702
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   703
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   704
     *          or if there is an outstanding send operation;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   705
     *          or if a previous Binary message was not sent with {@code isLast == true}
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   706
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   707
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   708
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   709
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   710
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   711
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   712
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   713
     * @throws IllegalArgumentException
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   714
     *         if {@code message} is a malformed (or an incomplete) UTF-16 sequence
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   715
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   716
    CompletableFuture<WebSocket> sendText(Stream<? extends CharSequence> message);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   717
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   718
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   719
     * Sends a Binary message with bytes from the given {@code ByteBuffer}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   720
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   721
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   722
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   723
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   724
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   725
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   726
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   727
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   728
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   729
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   730
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   731
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   732
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   733
     *          or if there is an outstanding send operation;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   734
     *          or if a previous Text message was not sent with {@code isLast == true}
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   735
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   736
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   737
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   738
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   739
     * @param isLast
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   740
     *         {@code true} if this is the final part of the message,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   741
     *         {@code false} otherwise
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   742
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   743
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   744
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   745
    CompletableFuture<WebSocket> sendBinary(ByteBuffer message, boolean isLast);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   746
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   747
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   748
     * Sends a Binary message with bytes from the given {@code byte[]}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   749
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   750
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   751
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   752
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   753
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   754
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   755
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   756
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   757
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   758
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   759
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   760
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   761
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   762
     *          or if there is an outstanding send operation;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   763
     *          or if a previous Text message was not sent with {@code isLast == true}
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   764
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   765
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   766
     * @implSpec This is equivalent to:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   767
     * <pre>{@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   768
     *     sendBinary(ByteBuffer.wrap(message), isLast)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   769
     * }</pre>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   770
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   771
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   772
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   773
     * @param isLast
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   774
     *         {@code true} if this is the final part of the message,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   775
     *         {@code false} otherwise
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   776
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   777
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   778
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   779
    default CompletableFuture<WebSocket> sendBinary(byte[] message, boolean isLast) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   780
        Objects.requireNonNull(message, "message");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   781
        return sendBinary(ByteBuffer.wrap(message), isLast);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   782
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   783
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   784
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   785
     * Sends a Ping message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   786
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   787
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   788
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   789
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   790
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   791
     * <p> A Ping message may be sent or received by either client or server.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   792
     * It may serve either as a keepalive or as a means to verify that the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   793
     * remote endpoint is still responsive.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   794
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   795
     * <p> The message must consist of not more than {@code 125} bytes: {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   796
     * message.remaining() <= 125}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   797
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   798
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   799
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   800
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   801
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   802
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   803
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   804
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   805
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   806
     *          or if there is an outstanding send operation
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   807
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   808
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   809
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   810
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   811
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   812
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   813
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   814
     * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   815
     *         if {@code message.remaining() > 125}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   816
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   817
    CompletableFuture<WebSocket> sendPing(ByteBuffer message);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   818
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   819
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   820
     * Sends a Pong message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   821
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   822
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   823
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   824
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   825
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   826
     * <p> A Pong message may be unsolicited or may be sent in response to a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   827
     * previously received Ping. In latter case the contents of the Pong is
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   828
     * identical to the originating Ping.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   829
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   830
     * <p> The message must consist of not more than {@code 125} bytes: {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   831
     * message.remaining() <= 125}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   832
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   833
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   834
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   835
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   836
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   837
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   838
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   839
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   840
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   841
     *          or if there is an outstanding send operation
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   842
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   843
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   844
     * @param message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   845
     *         the message
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   846
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   847
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   848
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   849
     * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   850
     *         if {@code message.remaining() > 125}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   851
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   852
    CompletableFuture<WebSocket> sendPong(ByteBuffer message);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   853
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   854
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   855
     * Sends a Close message with the given close code and the reason.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   856
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   857
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   858
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   859
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   860
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   861
     * <p> A Close message may consist of a close code and a reason for closing.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   862
     * The reason must have a valid UTF-8 representation not longer than {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   863
     * 123} bytes. The reason may be useful for debugging or passing information
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   864
     * relevant to the connection but is not necessarily human readable.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   865
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   866
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   867
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   868
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   869
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   870
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   871
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   872
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   873
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   874
     *          or if there is an outstanding send operation
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   875
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   876
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   877
     * @param code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   878
     *         the close code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   879
     * @param reason
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   880
     *         the reason; can be empty
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   881
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   882
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   883
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   884
     * @throws IllegalArgumentException
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   885
     *         if {@code reason} doesn't have an UTF-8 representation not longer
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   886
     *         than {@code 123} bytes
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   887
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   888
    CompletableFuture<WebSocket> sendClose(CloseCode code, CharSequence reason);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   889
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   890
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   891
     * Sends an empty Close message.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   892
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   893
     * <p> Returns a {@code CompletableFuture<WebSocket>} which completes
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   894
     * normally when the message has been sent or completes exceptionally if an
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   895
     * error occurs.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   896
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   897
     * <p> The returned {@code CompletableFuture} can complete exceptionally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   898
     * with:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   899
     * <ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   900
     * <li> {@link IOException}
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   901
     *          if an I/O error occurs during this operation
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   902
     * <li> {@link IllegalStateException}
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   903
     *          if the {@code WebSocket} closes while this operation is in progress;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   904
     *          or if a Close message has been sent already;
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   905
     *          or if there is an outstanding send operation
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   906
     * </ul>
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   907
     *
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   908
     * @return a CompletableFuture with this WebSocket
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   909
     */
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 38856
diff changeset
   910
    CompletableFuture<WebSocket> sendClose();
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   911
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   912
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   913
     * Requests {@code n} more messages to be received by the {@link Listener
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   914
     * Listener}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   915
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   916
     * <p> The actual number might be fewer if either of the endpoints decide to
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   917
     * close the connection before that or an error occurs.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   918
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   919
     * <p> A {@code WebSocket} that has just been created, hasn't requested
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   920
     * anything yet. Usually the initial request for messages is done in {@link
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   921
     * Listener#onOpen(java.net.http.WebSocket) Listener.onOpen}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   922
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   923
     * If all requested messages have been received, and the server sends more,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   924
     * then these messages are queued.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   925
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   926
     * @implNote This implementation does not distinguish between partial and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   927
     * whole messages, because it's not known beforehand how a message will be
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   928
     * received.
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   929
     *
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   930
     * <p> If a server sends more messages than requested, the implementation
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   931
     * queues up these messages on the TCP connection and may eventually force
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   932
     * the sender to stop sending through TCP flow control.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   933
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   934
     * @param n
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   935
     *         the number of messages
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   936
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   937
     * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   938
     *         if {@code n < 0}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   939
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   940
     * @return resulting unfulfilled demand with this request taken into account
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   941
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   942
    // TODO return void as it's breaking encapsulation (leaking info when exactly something deemed delivered)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   943
    // or demand behaves after LONG.MAX_VALUE
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   944
    long request(long n);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   945
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   946
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   947
     * Returns a {@linkplain Builder#subprotocols(String, String...) subprotocol}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   948
     * in use.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   949
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   950
     * @return a subprotocol, or {@code null} if there is none
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   951
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   952
    String getSubprotocol();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   953
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   954
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   955
     * Tells whether the {@code WebSocket} is closed.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   956
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   957
     * <p> A {@code WebSocket} deemed closed when either the underlying socket
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   958
     * is closed or the closing handshake is completed.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   959
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   960
     * @return {@code true} if the {@code WebSocket} is closed,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   961
     *         {@code false} otherwise
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   962
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   963
    boolean isClosed();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   964
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   965
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   966
     * Closes the {@code WebSocket} abruptly.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   967
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   968
     * <p> This method closes the underlying TCP connection. If the {@code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   969
     * WebSocket} is already closed then invoking this method has no effect.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   970
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   971
     * @throws IOException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   972
     *         if an I/O error occurs
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   973
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   974
    void abort() throws IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   975
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   976
    /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   977
     * A {@code WebSocket} close status code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   978
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   979
     * <p> Some codes <a href="https://tools.ietf.org/html/rfc6455#section-7.4">
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   980
     * specified</a> in the WebSocket Protocol are defined as named constants
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   981
     * here. Others can be {@linkplain #of(int) retrieved on demand}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   982
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   983
     * <p> This is a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   984
     * <a href="../../lang/doc-files/ValueBased.html">value-based</a> class;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   985
     * use of identity-sensitive operations (including reference equality
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   986
     * ({@code ==}), identity hash code, or synchronization) on instances of
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   987
     * {@code CloseCode} may have unpredictable results and should be avoided.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   988
     *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   989
     * @since 9
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   990
     */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   991
    final class CloseCode {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   992
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   993
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   994
         * Indicates a normal close, meaning that the purpose for which the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   995
         * connection was established has been fulfilled.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   996
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   997
         * <p> Numerical representation: {@code 1000}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   998
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   999
        public static final CloseCode NORMAL_CLOSURE
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1000
                = new CloseCode(1000, "NORMAL_CLOSURE");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1001
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1002
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1003
         * Indicates that an endpoint is "going away", such as a server going
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1004
         * down or a browser having navigated away from a page.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1005
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1006
         * <p> Numerical representation: {@code 1001}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1007
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1008
        public static final CloseCode GOING_AWAY
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1009
                = new CloseCode(1001, "GOING_AWAY");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1010
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1011
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1012
         * Indicates that an endpoint is terminating the connection due to a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1013
         * protocol error.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1014
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1015
         * <p> Numerical representation: {@code 1002}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1016
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1017
        public static final CloseCode PROTOCOL_ERROR
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1018
                = new CloseCode(1002, "PROTOCOL_ERROR");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1019
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1020
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1021
         * Indicates that an endpoint is terminating the connection because it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1022
         * has received a type of data it cannot accept (e.g., an endpoint that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1023
         * understands only text data MAY send this if it receives a binary
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1024
         * message).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1025
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1026
         * <p> Numerical representation: {@code 1003}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1027
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1028
        public static final CloseCode CANNOT_ACCEPT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1029
                = new CloseCode(1003, "CANNOT_ACCEPT");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1030
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1031
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1032
         * Indicates that an endpoint is terminating the connection because it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1033
         * has received data within a message that was not consistent with the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1034
         * type of the message (e.g., non-UTF-8 [RFC3629] data within a text
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1035
         * message).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1036
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1037
         * <p> Numerical representation: {@code 1007}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1038
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1039
        public static final CloseCode NOT_CONSISTENT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1040
                = new CloseCode(1007, "NOT_CONSISTENT");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1041
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1042
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1043
         * Indicates that an endpoint is terminating the connection because it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1044
         * has received a message that violates its policy. This is a generic
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1045
         * status code that can be returned when there is no other more suitable
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1046
         * status code (e.g., {@link #CANNOT_ACCEPT} or {@link #TOO_BIG}) or if
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1047
         * there is a need to hide specific details about the policy.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1048
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1049
         * <p> Numerical representation: {@code 1008}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1050
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1051
        public static final CloseCode VIOLATED_POLICY
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1052
                = new CloseCode(1008, "VIOLATED_POLICY");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1053
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1054
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1055
         * Indicates that an endpoint is terminating the connection because it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1056
         * has received a message that is too big for it to process.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1057
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1058
         * <p> Numerical representation: {@code 1009}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1059
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1060
        public static final CloseCode TOO_BIG
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1061
                = new CloseCode(1009, "TOO_BIG");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1062
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1063
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1064
         * Indicates that an endpoint is terminating the connection because it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1065
         * encountered an unexpected condition that prevented it from fulfilling
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1066
         * the request.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1067
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1068
         * <p> Numerical representation: {@code 1011}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1069
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1070
        public static final CloseCode UNEXPECTED_CONDITION
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1071
                = new CloseCode(1011, "UNEXPECTED_CONDITION");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1072
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1073
        private static final Map<Integer, CloseCode> cached = Map.ofEntries(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1074
                entry(NORMAL_CLOSURE),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1075
                entry(GOING_AWAY),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1076
                entry(PROTOCOL_ERROR),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1077
                entry(CANNOT_ACCEPT),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1078
                entry(NOT_CONSISTENT),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1079
                entry(VIOLATED_POLICY),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1080
                entry(TOO_BIG),
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1081
                entry(UNEXPECTED_CONDITION)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1082
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1083
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1084
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1085
         * Returns a {@code CloseCode} from its numerical representation.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1086
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1087
         * <p> The given {@code code} should be in the range {@code 1000 <= code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1088
         * <= 4999}, and should not be equal to any of the following codes:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1089
         * {@code 1004}, {@code 1005}, {@code 1006} and {@code 1015}.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1090
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1091
         * @param code
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1092
         *         numerical representation
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1093
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1094
         * @return a close code corresponding to the provided numerical value
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1095
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1096
         * @throws IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1097
         *         if {@code code} violates any of the requirements above
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1098
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1099
        public static CloseCode of(int code) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1100
            if (code < 1000 || code > 4999) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1101
                throw new IllegalArgumentException("Out of range: " + code);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1102
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1103
            if (code == 1004 || code == 1005 || code == 1006 || code == 1015) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1104
                throw new IllegalArgumentException("Reserved: " + code);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1105
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1106
            CloseCode closeCode = cached.get(code);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1107
            return closeCode != null ? closeCode : new CloseCode(code, "");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1108
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1109
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1110
        private final int code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1111
        private final String description;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1112
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1113
        private CloseCode(int code, String description) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1114
            assert description != null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1115
            this.code = code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1116
            this.description = description;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1117
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1118
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1119
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1120
         * Returns a numerical representation of this close code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1121
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1122
         * @return a numerical representation
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1123
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1124
        public int getCode() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1125
            return code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1126
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1127
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1128
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1129
         * Compares this close code to the specified object.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1130
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1131
         * @param o
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1132
         *         the object to compare this {@code CloseCode} against
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1133
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1134
         * @return {@code true} iff the argument is a close code with the same
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1135
         * {@linkplain #getCode() numerical representation} as this one
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1136
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1137
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1138
        public boolean equals(Object o) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1139
            if (this == o) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1140
                return true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1141
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1142
            if (!(o instanceof CloseCode)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1143
                return false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1144
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1145
            CloseCode that = (CloseCode) o;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1146
            return code == that.code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1147
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1148
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1149
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1150
        public int hashCode() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1151
            return code;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1152
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1153
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1154
        /**
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1155
         * Returns a human-readable representation of this close code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1156
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1157
         * @apiNote The representation is not designed to be parsed; the format
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1158
         * may change unexpectedly.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1159
         *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1160
         * @return a string representation
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1161
         */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1162
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1163
        public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1164
            return code + (description.isEmpty() ? "" : (": " + description));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1165
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1166
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1167
        private static Map.Entry<Integer, CloseCode> entry(CloseCode cc) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1168
            return Map.entry(cc.getCode(), cc);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1169
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1170
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
  1171
}