jdk/src/java.httpclient/share/classes/java/net/http/CharsetToolkit.java
author michaelm
Sat, 30 Apr 2016 00:30:31 +0100
changeset 37720 45cd7cc65382
child 39730 196f4e25d9f5
permissions -rw-r--r--
8087124: HTTP/2 implementation Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    23
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
package java.net.http;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
import java.nio.ByteBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
import java.nio.CharBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
import java.nio.charset.CharacterCodingException;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import java.nio.charset.CharsetDecoder;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
import java.nio.charset.CharsetEncoder;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
import java.nio.charset.CoderResult;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
import static java.nio.charset.StandardCharsets.UTF_8;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
// The purpose of this class is to separate charset-related tasks from the main
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
// WebSocket logic, simplifying where possible.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
//
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
//     * Coders hide the differences between coding and flushing stages on the
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
//       API level
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
//     * Verifier abstracts the way the verification is performed
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
//       (spoiler: it's a decoding into a throw-away buffer)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
//
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
// Coding methods throw exceptions instead of returning coding result denoting
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
// errors, since any kind of handling and recovery is not expected.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
final class CharsetToolkit {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
    private CharsetToolkit() { }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
    static final class Verifier {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
        private final CharsetDecoder decoder = UTF_8.newDecoder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
        // A buffer used to check validity of UTF-8 byte stream by decoding it.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
        // The contents of this buffer are never used.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
        // The size is arbitrary, though it should probably be chosen from the
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
        // performance perspective since it affects the total number of calls to
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
        // decoder.decode() and amount of work in each of these calls
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
        private final CharBuffer blackHole = CharBuffer.allocate(1024);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
        void verify(ByteBuffer in, boolean endOfInput)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
                throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
            while (true) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
                // Since decoder.flush() cannot produce an error, it's not
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
                // helpful for verification. Therefore this step is skipped.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
                CoderResult r = decoder.decode(in, blackHole, endOfInput);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
                if (r.isOverflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
                    blackHole.clear();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
                } else if (r.isUnderflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
                    break;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
                } else if (r.isError()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
                    r.throwException();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
                } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
                    // Should not happen
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
                    throw new InternalError();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
        Verifier reset() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
            decoder.reset();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
            return this;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    84
    static final class Encoder {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
        private final CharsetEncoder encoder = UTF_8.newEncoder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
        private boolean coding = true;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
        CoderResult encode(CharBuffer in, ByteBuffer out, boolean endOfInput)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    90
                throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    91
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    92
            if (coding) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    93
                CoderResult r = encoder.encode(in, out, endOfInput);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    94
                if (r.isOverflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    95
                    return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    96
                } else if (r.isUnderflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    97
                    if (endOfInput) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    98
                        coding = false;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    99
                    } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   100
                        return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   101
                    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   102
                } else if (r.isError()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   103
                    r.throwException();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   104
                } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   105
                    // Should not happen
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   106
                    throw new InternalError();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   107
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   108
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   109
            assert !coding;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   110
            return encoder.flush(out);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   111
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   112
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   113
        Encoder reset() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   114
            coding = true;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   115
            encoder.reset();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   116
            return this;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   117
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   118
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   119
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   120
    static CharBuffer decode(ByteBuffer in) throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   121
        return UTF_8.newDecoder().decode(in);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   122
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   123
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   124
    static final class Decoder {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   125
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   126
        private final CharsetDecoder decoder = UTF_8.newDecoder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   127
        private boolean coding = true; // Either coding or flushing
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   128
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   129
        CoderResult decode(ByteBuffer in, CharBuffer out, boolean endOfInput)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   130
                throws CharacterCodingException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   131
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   132
            if (coding) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   133
                CoderResult r = decoder.decode(in, out, endOfInput);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   134
                if (r.isOverflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   135
                    return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   136
                } else if (r.isUnderflow()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   137
                    if (endOfInput) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   138
                        coding = false;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   139
                    } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   140
                        return r;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   141
                    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   142
                } else if (r.isError()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   143
                    r.throwException();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   144
                } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   145
                    // Should not happen
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   146
                    throw new InternalError();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   147
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   148
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   149
            assert !coding;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   150
            return decoder.flush(out);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   151
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   152
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   153
        Decoder reset() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   154
            coding = true;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   155
            decoder.reset();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   156
            return this;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   157
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   158
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   159
}