src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageEncoder.java
author prappo
Fri, 06 Apr 2018 15:20:43 +0100
branchhttp-client-branch
changeset 56389 0ba90c4f1e3f
parent 56327 efd50952acd9
child 56437 f8b3f053cfbb
permissions -rw-r--r--
http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     1
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     4
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    10
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    15
 * accompanied this code).
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    16
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    20
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    23
 * questions.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    24
 */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    25
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    26
package jdk.internal.net.http.websocket;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    27
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    28
import jdk.internal.net.http.common.Utils;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    29
import jdk.internal.net.http.websocket.Frame.Opcode;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    30
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    31
import java.io.IOException;
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    32
import java.lang.System.Logger.Level;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    33
import java.nio.ByteBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    34
import java.nio.CharBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    35
import java.nio.charset.CharacterCodingException;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    36
import java.nio.charset.CharsetEncoder;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    37
import java.nio.charset.CoderResult;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    38
import java.nio.charset.CodingErrorAction;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    39
import java.nio.charset.StandardCharsets;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    40
import java.security.SecureRandom;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    41
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    42
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    43
 * A stateful producer of binary representations of WebSocket messages being
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    44
 * sent from the client to the server.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    45
 *
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    46
 * An encoding method is given an original message and a byte buffer to put the
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    47
 * resulting bytes to. The method is called until it returns true. Then the
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    48
 * reset method is called. The whole sequence repeats with next message.
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    49
 */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    50
public class MessageEncoder {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    51
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    52
    private static final boolean DEBUG = Utils.DEBUG_WS;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    53
    private static final System.Logger debug =
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56295
diff changeset
    54
            Utils.getWebSocketLogger("[Output]"::toString, DEBUG);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    55
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    56
    private final SecureRandom maskingKeySource = new SecureRandom();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    57
    private final Frame.HeaderWriter headerWriter = new Frame.HeaderWriter();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    58
    private final Frame.Masker payloadMasker = new Frame.Masker();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    59
    private final CharsetEncoder charsetEncoder
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    60
            = StandardCharsets.UTF_8.newEncoder()
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    61
                                    .onMalformedInput(CodingErrorAction.REPORT)
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    62
                                    .onUnmappableCharacter(CodingErrorAction.REPORT);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    63
    /*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    64
     * This buffer is used both to encode characters to UTF-8 and to calculate
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    65
     * the length of the resulting frame's payload. The length of the payload
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    66
     * must be known before the frame's header can be written.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    67
     * For implementation reasons, this buffer must have a capacity of at least
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    68
     * the maximum size of a Close frame payload, which is 125 bytes
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    69
     * (or Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH).
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    70
     */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    71
    private final ByteBuffer intermediateBuffer = createIntermediateBuffer(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    72
            Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    73
    private final ByteBuffer headerBuffer = ByteBuffer.allocate(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    74
            Frame.MAX_HEADER_SIZE_BYTES);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    75
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    76
    private boolean started;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    77
    private boolean flushing;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    78
    private boolean moreText = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    79
    private long headerCount;
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    80
    /* Has the previous frame got its fin flag set? */
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    81
    private boolean previousFin = true;
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
    82
    /* Was the previous frame TEXT or a CONTINUATION thereof? */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    83
    private boolean previousText;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56304
diff changeset
    84
    private boolean closed;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    85
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    86
    /*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    87
     * How many bytes of the current message have been already encoded.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    88
     *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    89
     * Even though the user hands their buffers over to us, they still can
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    90
     * manipulate these buffers while we are getting data out of them.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    91
     * The number of produced bytes guards us from such behaviour in the
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    92
     * case of messages that must be restricted in size (Ping, Pong and Close).
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    93
     * For other messages this measure provides a best-effort attempt to detect
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    94
     * concurrent changes to buffer.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    95
     *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    96
     * Making a shallow copy (duplicate/slice) and then checking the size
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    97
     * precondition on it would also solve the problem, but at the cost of this
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    98
     * extra copy.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    99
     */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   100
    private int actualLen;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   101
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   102
    /*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   103
     * How many bytes were originally there in the message, before the encoding
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   104
     * started.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   105
     */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   106
    private int expectedLen;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   107
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   108
    /* Exposed for testing purposes */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   109
    protected ByteBuffer createIntermediateBuffer(int minSize) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   110
        int capacity = Utils.getIntegerNetProperty(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   111
                "jdk.httpclient.websocket.intermediateBufferSize", 16384);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   112
        return ByteBuffer.allocate(Math.max(minSize, capacity));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   113
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   114
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   115
    public void reset() {
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   116
        // Do not reset the message stream state fields, e.g. previousFin,
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   117
        // previousText. Just an individual message state:
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   118
        started = false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   119
        flushing = false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   120
        moreText = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   121
        headerCount = 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   122
        actualLen = 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   123
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   124
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   125
    /*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   126
     * Encodes text messages by cutting them into fragments of maximum size of
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   127
     * intermediateBuffer.capacity()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   128
     */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   129
    public boolean encodeText(CharBuffer src, boolean last, ByteBuffer dst)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   130
            throws IOException
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   131
    {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   132
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   133
            debug.log(Level.DEBUG, "encode text src=[pos=%s lim=%s cap=%s] last=%s dst=%s",
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   134
                      src.position(), src.limit(), src.capacity(), last, dst);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   135
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   136
        if (closed) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   137
            throw new IOException("Output closed");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   138
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   139
        if (!started) {
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   140
            if (!previousText && !previousFin) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   141
                // Previous data message was a partial binary message
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   142
                throw new IllegalStateException("Unexpected text message");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   143
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   144
            started = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   145
            headerBuffer.position(0).limit(0);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   146
            intermediateBuffer.position(0).limit(0);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   147
            charsetEncoder.reset();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   148
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   149
        while (true) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   150
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   151
                debug.log(Level.DEBUG, "put");
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   152
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   153
            if (!putAvailable(headerBuffer, dst)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   154
                return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   155
            }
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   156
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   157
                debug.log(Level.DEBUG, "mask");
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   158
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   159
            if (maskAvailable(intermediateBuffer, dst) < 0) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   160
                return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   161
            }
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   162
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   163
                debug.log(Level.DEBUG, "moreText");
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   164
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   165
            if (!moreText) {
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   166
                previousFin = last;
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   167
                previousText = true;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   168
                return true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   169
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   170
            intermediateBuffer.clear();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   171
            CoderResult r = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   172
            if (!flushing) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   173
                r = charsetEncoder.encode(src, intermediateBuffer, true);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   174
                if (r.isUnderflow()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   175
                    flushing = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   176
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   177
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   178
            if (flushing) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   179
                r = charsetEncoder.flush(intermediateBuffer);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   180
                if (r.isUnderflow()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   181
                    moreText = false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   182
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   183
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   184
            if (r.isError()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   185
                try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   186
                    r.throwException();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   187
                } catch (CharacterCodingException e) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   188
                    throw new IOException("Malformed text message", e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   189
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   190
            }
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   191
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   192
                debug.log(Level.DEBUG, "frame #%s", headerCount);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   193
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   194
            intermediateBuffer.flip();
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   195
            Opcode opcode = previousFin && headerCount == 0
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   196
                    ? Opcode.TEXT : Opcode.CONTINUATION;
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   197
            boolean fin = last && !moreText;
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   198
            setupHeader(opcode, fin, intermediateBuffer.remaining());
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   199
            headerCount++;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   200
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   201
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   202
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   203
    private boolean putAvailable(ByteBuffer src, ByteBuffer dst) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   204
        int available = dst.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   205
        if (available >= src.remaining()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   206
            dst.put(src);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   207
            return true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   208
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   209
            int lim = src.limit();                   // save the limit
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   210
            src.limit(src.position() + available);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   211
            dst.put(src);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   212
            src.limit(lim);                          // restore the limit
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   213
            return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   214
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   215
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   216
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   217
    public boolean encodeBinary(ByteBuffer src, boolean last, ByteBuffer dst)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   218
            throws IOException
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   219
    {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   220
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   221
            debug.log(Level.DEBUG, "encode binary src=%s last=%s dst=%s",
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   222
                      src, last, dst);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   223
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   224
        if (closed) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   225
            throw new IOException("Output closed");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   226
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   227
        if (!started) {
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   228
            if (previousText && !previousFin) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   229
                // Previous data message was a partial text message
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   230
                throw new IllegalStateException("Unexpected binary message");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   231
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   232
            expectedLen = src.remaining();
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   233
            Opcode opcode = previousFin ? Opcode.BINARY : Opcode.CONTINUATION;
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   234
            setupHeader(opcode, last, expectedLen);
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   235
            previousFin = last;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   236
            previousText = false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   237
            started = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   238
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   239
        if (!putAvailable(headerBuffer, dst)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   240
            return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   241
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   242
        int count = maskAvailable(src, dst);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   243
        actualLen += Math.abs(count);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   244
        if (count >= 0 && actualLen != expectedLen) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   245
            throw new IOException("Concurrent message modification");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   246
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   247
        return count >= 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   248
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   249
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   250
    private int maskAvailable(ByteBuffer src, ByteBuffer dst) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   251
        int r0 = dst.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   252
        payloadMasker.transferMasking(src, dst);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   253
        int masked = r0 - dst.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   254
        return src.hasRemaining() ? -masked : masked;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   255
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   256
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   257
    public boolean encodePing(ByteBuffer src, ByteBuffer dst)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   258
            throws IOException
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   259
    {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   260
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   261
            debug.log(Level.DEBUG, "encode ping src=%s dst=%s", src, dst);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   262
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   263
        if (closed) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   264
            throw new IOException("Output closed");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   265
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   266
        if (!started) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   267
            expectedLen = src.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   268
            if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   269
                throw new IllegalArgumentException("Long message: " + expectedLen);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   270
            }
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   271
            setupHeader(Opcode.PING, true, expectedLen);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   272
            started = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   273
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   274
        if (!putAvailable(headerBuffer, dst)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   275
            return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   276
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   277
        int count = maskAvailable(src, dst);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   278
        actualLen += Math.abs(count);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   279
        if (count >= 0 && actualLen != expectedLen) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   280
            throw new IOException("Concurrent message modification");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   281
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   282
        return count >= 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   283
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   284
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   285
    public boolean encodePong(ByteBuffer src, ByteBuffer dst)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   286
            throws IOException
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   287
    {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   288
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   289
            debug.log(Level.DEBUG, "encode pong src=%s dst=%s",
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   290
                      src, dst);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   291
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   292
        if (closed) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   293
            throw new IOException("Output closed");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   294
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   295
        if (!started) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   296
            expectedLen = src.remaining();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   297
            if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   298
                throw new IllegalArgumentException("Long message: " + expectedLen);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   299
            }
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   300
            setupHeader(Opcode.PONG, true, expectedLen);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   301
            started = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   302
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   303
        if (!putAvailable(headerBuffer, dst)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   304
            return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   305
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   306
        int count = maskAvailable(src, dst);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   307
        actualLen += Math.abs(count);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   308
        if (count >= 0 && actualLen != expectedLen) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   309
            throw new IOException("Concurrent message modification");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   310
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   311
        return count >= 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   312
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   313
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   314
    public boolean encodeClose(int statusCode, CharBuffer reason, ByteBuffer dst)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   315
            throws IOException
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   316
    {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   317
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   318
            debug.log(Level.DEBUG, "encode close statusCode=%s reason=[pos=%s lim=%s cap=%s] dst=%s",
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   319
                      statusCode, reason.position(), reason.limit(), reason.capacity(), dst);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   320
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   321
        if (closed) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   322
            throw new IOException("Output closed");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   323
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   324
        if (!started) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   325
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   326
                debug.log(Level.DEBUG, "reason [pos=%s lim=%s cap=%s]",
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   327
                          reason.position(), reason.limit(), reason.capacity());
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   328
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   329
            intermediateBuffer.position(0).limit(Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   330
            intermediateBuffer.putChar((char) statusCode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   331
            CoderResult r = charsetEncoder.reset().encode(reason, intermediateBuffer, true);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   332
            if (r.isUnderflow()) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   333
                if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   334
                    debug.log(Level.DEBUG, "flushing");
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   335
                }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   336
                r = charsetEncoder.flush(intermediateBuffer);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   337
            }
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   338
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   339
                debug.log(Level.DEBUG, "encoding result: %s", r);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   340
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   341
            if (r.isError()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   342
                try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   343
                    r.throwException();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   344
                } catch (CharacterCodingException e) {
56327
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56314
diff changeset
   345
                    throw new IOException("Malformed reason", e);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   346
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   347
            } else if (r.isOverflow()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   348
                // Here the 125 bytes size is ensured by the check for overflow
56327
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56314
diff changeset
   349
                throw new IOException("Long reason");
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   350
            } else if (!r.isUnderflow()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   351
                throw new InternalError(); // assertion
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   352
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   353
            intermediateBuffer.flip();
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   354
            setupHeader(Opcode.CLOSE, true, intermediateBuffer.remaining());
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   355
            started = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   356
            closed = true;
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   357
            if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   358
                debug.log(Level.DEBUG, "intermediateBuffer=%s", intermediateBuffer);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   359
            }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   360
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   361
        if (!putAvailable(headerBuffer, dst)) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   362
            return false;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   363
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   364
        return maskAvailable(intermediateBuffer, dst) >= 0;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   365
    }
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   366
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   367
    private void setupHeader(Opcode opcode, boolean fin, long payloadLen) {
56389
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   368
        if (debug.isLoggable(Level.DEBUG)) {
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   369
            debug.log(Level.DEBUG, "frame opcode=%s fin=%s len=%s",
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   370
                      opcode, fin, payloadLen);
0ba90c4f1e3f http-client-branch: review comment - check for logging level being enabled before logging (for performance reasons)
prappo
parents: 56327
diff changeset
   371
        }
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   372
        headerBuffer.clear();
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   373
        int mask = maskingKeySource.nextInt();
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   374
        headerWriter.fin(fin)
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   375
                    .opcode(opcode)
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   376
                    .payloadLen(payloadLen)
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   377
                    .mask(mask)
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   378
                    .write(headerBuffer);
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   379
        headerBuffer.flip();
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   380
        payloadMasker.mask(mask);
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   381
    }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   382
}