jdk/src/java.httpclient/share/classes/java/net/http/WSWriter.java
author michaelm
Mon, 16 May 2016 16:04:14 +0100
changeset 38322 f6f9d3ec14ba
parent 37874 02589df0999a
child 39729 ef2b0635618f
permissions -rw-r--r--
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError 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) 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  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  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  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.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.nio.channels.SelectionKey;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.util.function.Consumer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import static java.util.Objects.requireNonNull;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
 * Writes ByteBuffer[] to the channel in a non-blocking, asynchronous fashion.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
 * A client attempts to write data by calling
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
 *     boolean tryWriteFully(ByteBuffer[] buffers)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
 * If the attempt was successful and all the data has been written, then the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
 * method returns `true`.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
 * If the data has been written partially, then the method returns `false`, and
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
 * the writer (this object) attempts to complete the write asynchronously by
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
 * calling, possibly more than once
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
 *     boolean tryCompleteWrite()
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
 * in its own threads.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
 * When the write has been completed asynchronously, the callback is signalled
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
 * with `null`.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
 * If an error occurs in any of these stages it will NOT be thrown from the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
 * method. Instead `false` will be returned and the exception will be signalled
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
 * to the callback. This is done in order to handle all exceptions in a single
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
 * place.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
final class WSWriter {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
    private final RawChannel channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
    private final RawChannel.NonBlockingEvent writeReadinessHandler;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
    private final Consumer<Throwable> completionCallback;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
    private ByteBuffer[] buffers;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
    private int offset;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
    WSWriter(RawChannel channel, Consumer<Throwable> completionCallback) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
        this.channel = channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
        this.completionCallback = completionCallback;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
        this.writeReadinessHandler = createHandler();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
    boolean tryWriteFully(ByteBuffer[] buffers) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
        synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
            this.buffers = requireNonNull(buffers);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
            this.offset = 0;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
        return tryCompleteWrite();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
    private final boolean tryCompleteWrite() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
            return writeNow();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
        } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
            completionCallback.accept(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
            return false;
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
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
    private boolean writeNow() throws IOException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
        synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
            for (; offset != -1; offset = nextUnwrittenIndex(buffers, offset)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                long bytesWritten = channel.write(buffers, offset, buffers.length - offset);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
                if (bytesWritten == 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                    channel.registerEvent(writeReadinessHandler);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
                    return false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
            return true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
    private static int nextUnwrittenIndex(ByteBuffer[] buffers, int offset) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
        for (int i = offset; i < buffers.length; i++) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
            if (buffers[i].hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
                return i;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
        return -1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
    private RawChannel.NonBlockingEvent createHandler() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
        return new RawChannel.NonBlockingEvent() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
            public int interestOps() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
                return SelectionKey.OP_WRITE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
            public void handle() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
                if (tryCompleteWrite()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
                    completionCallback.accept(null);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
            public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
                return "Write readiness event [" + channel + "]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
        };
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
}