jdk/src/share/classes/sun/nio/cs/Surrogate.java
author martin
Fri, 24 Jul 2009 18:24:02 -0700
changeset 3324 02cc89024ea2
parent 2 90ce3da70b43
child 3714 6a4eb8f53f91
permissions -rw-r--r--
6639458: Improvements to Surrogate.java Summary: Optimize Surrogate.java Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.MalformedInputException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.UnmappableCharacterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Utility class for dealing with surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Surrogate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private Surrogate() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    43
    // TODO: Deprecate/remove the following redundant definitions
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    44
    public static final char MIN_HIGH = Character.MIN_HIGH_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    45
    public static final char MAX_HIGH = Character.MAX_HIGH_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    46
    public static final char MIN_LOW  = Character.MIN_LOW_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    47
    public static final char MAX_LOW  = Character.MAX_LOW_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    48
    public static final char MIN      = Character.MIN_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    49
    public static final char MAX      = Character.MAX_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    50
    public static final int UCS4_MIN  = Character.MIN_SUPPLEMENTARY_CODE_POINT;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    51
    public static final int UCS4_MAX  = Character.MAX_CODE_POINT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Tells whether or not the given UTF-16 value is a high surrogate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static boolean isHigh(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return (MIN_HIGH <= c) && (c <= MAX_HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Tells whether or not the given UTF-16 value is a low surrogate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static boolean isLow(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return (MIN_LOW <= c) && (c <= MAX_LOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Tells whether or not the given UTF-16 value is a surrogate character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static boolean is(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return (MIN <= c) && (c <= MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    75
     * Tells whether or not the given UCS-4 character is in the Basic
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    76
     * Multilingual Plane, and can be represented using a single char.
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    77
     */
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    78
    public static boolean isBMP(int uc) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    79
        return (int) (char) uc == uc;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    80
    }
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    81
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    82
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Tells whether or not the given UCS-4 character must be represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * surrogate pair in UTF-16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static boolean neededFor(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    87
        return Character.isSupplementaryCodePoint(uc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the high UTF-16 surrogate for the given UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static char high(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    94
        assert Character.isSupplementaryCodePoint(uc);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    95
        return (char)((uc >> 10)
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    96
                      + (Character.MIN_HIGH_SURROGATE
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    97
                         - (Character.MIN_SUPPLEMENTARY_CODE_POINT >> 10)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Returns the low UTF-16 surrogate for the given UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static char low(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   104
        assert Character.isSupplementaryCodePoint(uc);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   105
        return (char)((uc & 0x3ff) + Character.MIN_LOW_SURROGATE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Converts the given surrogate pair into a 32-bit UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static int toUCS4(char c, char d) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   112
        assert Character.isHighSurrogate(c) && Character.isLowSurrogate(d);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   113
        return Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Surrogate parsing support.  Charset implementations may use instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * this class to handle the details of parsing UTF-16 surrogate pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public Parser() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        private int character;          // UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        private CoderResult error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private boolean isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * Returns the UCS-4 character previously parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        public int character() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * Tells whether or not the previously-parsed UCS-4 character was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * originally represented by a surrogate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public boolean isPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * Returns the number of UTF-16 characters consumed by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * parse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public int increment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return isPair ? 2 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * If the previous parse operation detected an error, return the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            assert (error != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * Returns an unmappable-input result object, with the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * input length, for the previously-parsed character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        public CoderResult unmappableResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return CoderResult.unmappableForLength(isPair ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * @param  in   The source buffer, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        public int parse(char c, CharBuffer in) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   186
            if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (!in.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                char d = in.get();
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   192
                if (Character.isLowSurrogate(d)) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   193
                    character = Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   201
            if (Character.isLowSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * @param  ia   The input array, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * @param  ip   The input index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * @param  il   The input limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public int parse(char c, char[] ia, int ip, int il) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            assert (ia[ip] == c);
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   228
            if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (il - ip < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                char d = ia[ip + 1];
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   234
                if (Character.isLowSurrogate(d)) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   235
                    character = Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   243
            if (Character.isLowSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Surrogate generation support.  Charset implementations may use instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * of this class to handle the details of generating UTF-16 surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public static class Generator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        public Generator() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        private CoderResult error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         * If the previous generation operation detected an error, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * object describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            assert error != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * @param  dst  The destination buffer, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        public int generate(int uc, int len, CharBuffer dst) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   290
            if (Surrogate.isBMP(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (Surrogate.is(uc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                if (dst.remaining() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                dst.put((char)uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return 1;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   302
            } else if (Character.isSupplementaryCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                if (dst.remaining() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                dst.put(Surrogate.high(uc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                dst.put(Surrogate.low(uc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return 2;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   311
            } else {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   312
                error = CoderResult.unmappableForLength(len);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   313
                return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * @param  da   The destination array, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * @param  dp   The destination position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * @param  dl   The destination limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        public int generate(int uc, int len, char[] da, int dp, int dl) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   334
            if (Surrogate.isBMP(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (Surrogate.is(uc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (dl - dp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                da[dp] = (char)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return 1;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   346
            } else if (Character.isSupplementaryCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                if (dl - dp < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                da[dp] = Surrogate.high(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                da[dp + 1] = Surrogate.low(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return 2;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   355
            } else {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   356
                error = CoderResult.unmappableForLength(len);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   357
                return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
}