jdk/src/java.httpclient/share/classes/java/net/http/WSUtils.java
author michaelm
Mon, 16 May 2016 16:04:14 +0100
changeset 38322 f6f9d3ec14ba
parent 37874 02589df0999a
child 39730 196f4e25d9f5
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.net.URI;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.net.URISyntaxException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.nio.Buffer;
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.Arrays;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
final class WSUtils {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
    private WSUtils() { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
    static final System.Logger logger = System.getLogger("java.net.http.WebSocket");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
    static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocate(0);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
    // Helps to trim long names (packages, nested/inner types) in logs/toString
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
    static String toStringSimple(Object o) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
        return o.getClass().getSimpleName() + "@" +
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
                Integer.toHexString(System.identityHashCode(o));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
    // 1. It adds a number of remaining bytes;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
    // 2. Standard Buffer-type toString for CharBuffer (since it adheres to the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
    // contract of java.lang.CharSequence.toString() which is both not too
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
    // useful and not too private)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
    static String toString(Buffer b) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
        return toStringSimple(b)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
                + "[pos=" + b.position()
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
                + " lim=" + b.limit()
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
                + " cap=" + b.capacity()
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
                + " rem=" + b.remaining() + "]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
    static String toString(CharSequence s) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
        return s == null
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
                ? "null"
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
                : toStringSimple(s) + "[len=" + s.length() + "]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
    static String dump(Object... objects) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
        return Arrays.toString(objects);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
    static String webSocketSpecViolation(String section, String detail) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
        return "RFC 6455 " + section + " " + detail;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
}