jdk/src/java.httpclient/share/classes/java/net/http/WSCharsetToolkit.java
author prappo
Mon, 09 May 2016 23:33:09 +0100
changeset 37874 02589df0999a
child 39730 196f4e25d9f5
permissions -rw-r--r--
8087113: Websocket API and implementation Reviewed-by: chegar
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 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
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.nio.CharBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.nio.charset.CharacterCodingException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.nio.charset.CharsetDecoder;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.nio.charset.CharsetEncoder;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.nio.charset.CoderResult;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.nio.charset.CodingErrorAction;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.nio.charset.StandardCharsets;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import static java.lang.System.Logger.Level.WARNING;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import static java.net.http.WSUtils.EMPTY_BYTE_BUFFER;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import static java.net.http.WSUtils.logger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
import static java.nio.charset.StandardCharsets.UTF_8;
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
 * A collection of tools for UTF-8 coding.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
final class WSCharsetToolkit {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
    private WSCharsetToolkit() { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
    static final class Encoder {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
        private final CharsetEncoder encoder = UTF_8.newEncoder();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
        ByteBuffer encode(CharBuffer in) throws CharacterCodingException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
            return encoder.encode(in);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
        // TODO:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
        // ByteBuffer[] encode(CharBuffer in) throws CharacterCodingException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
        //     return encoder.encode(in);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
        // }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
    static CharBuffer decode(ByteBuffer in) throws CharacterCodingException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
        return UTF_8.newDecoder().decode(in);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
    static final class Decoder {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
        private final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
        {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
            decoder.onMalformedInput(CodingErrorAction.REPORT);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
            decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
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
        private ByteBuffer leftovers = EMPTY_BYTE_BUFFER;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
        WSShared<CharBuffer> decode(WSShared<ByteBuffer> in, boolean endOfInput)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
                throws CharacterCodingException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
            ByteBuffer b;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
            int rem = leftovers.remaining();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
            if (rem != 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
                // TODO: We won't need this wasteful allocation & copying when
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
                // JDK-8155222 has been resolved
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
                b = ByteBuffer.allocate(rem + in.remaining());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
                b.put(leftovers).put(in.buffer()).flip();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
                b = in.buffer();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
            CharBuffer out = CharBuffer.allocate(b.remaining());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
            CoderResult r = decoder.decode(b, out, endOfInput);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
            if (r.isError()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
                r.throwException();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
            if (b.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                leftovers = ByteBuffer.allocate(b.remaining()).put(b).flip();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                leftovers = EMPTY_BYTE_BUFFER;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
            // Since it's UTF-8, the assumption is leftovers.remaining() < 4
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
            // (i.e. small). Otherwise a shared buffer should be used
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
            if (!(leftovers.remaining() < 4)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
                logger.log(WARNING,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
                        "The size of decoding leftovers is greater than expected: {0}",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
                        leftovers.remaining());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
            b.position(b.limit()); // As if we always read to the end
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
            in.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
            // Decoder promises that in the case of endOfInput == true:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
            // "...any remaining undecoded input will be treated as being
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
            // malformed"
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
            assert !(endOfInput && leftovers.hasRemaining()) : endOfInput + ", " + leftovers;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
            if (endOfInput) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
                r = decoder.flush(out);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
                decoder.reset();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
                if (r.isOverflow()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
                    // FIXME: for now I know flush() does nothing. But the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
                    // implementation of UTF8 decoder might change. And if now
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
                    // flush() is a no-op, it is not guaranteed to remain so in
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
                    // the future
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
                    throw new InternalError("Not yet implemented");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
            out.flip();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
            return WSShared.wrap(out);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
}