jdk/src/share/classes/sun/nio/cs/Surrogate.java
author martin
Wed, 30 Jun 2010 16:11:32 -0700
changeset 5986 04eb44085c00
parent 5506 202f599c92aa
child 5991 288afdbbca28
permissions -rw-r--r--
6934265: Add public method Character.isBmpCodePoint Summary: Move isBmpCodePoint from sun.nio.cs.Surrogate to Character Reviewed-by: sherman Contributed-by: Ulf Zibis <ulf.zibis@gmx.de>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5144
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5144
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5144
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5144
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5144
diff changeset
    23
 * questions.
2
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
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    37
 * @author Martin Buchholz
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    38
 * @author Ulf Zibis
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Surrogate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private Surrogate() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    44
    // TODO: Deprecate/remove the following redundant definitions
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    45
    public static final char MIN_HIGH = Character.MIN_HIGH_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    46
    public static final char MAX_HIGH = Character.MAX_HIGH_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    47
    public static final char MIN_LOW  = Character.MIN_LOW_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    48
    public static final char MAX_LOW  = Character.MAX_LOW_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    49
    public static final char MIN      = Character.MIN_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    50
    public static final char MAX      = Character.MAX_SURROGATE;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    51
    public static final int UCS4_MIN  = Character.MIN_SUPPLEMENTARY_CODE_POINT;
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    52
    public static final int UCS4_MAX  = Character.MAX_CODE_POINT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    55
     * Tells whether or not the given value is in the high surrogate range.
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    56
     * Use of {@link Character#isHighSurrogate} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static boolean isHigh(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return (MIN_HIGH <= c) && (c <= MAX_HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    63
     * Tells whether or not the given value is in the low surrogate range.
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    64
     * Use of {@link Character#isLowSurrogate} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static boolean isLow(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return (MIN_LOW <= c) && (c <= MAX_LOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    71
     * Tells whether or not the given value is in the surrogate range.
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    72
     * Use of {@link Character#isSurrogate} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static boolean is(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return (MIN <= c) && (c <= MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Tells whether or not the given UCS-4 character must be represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * surrogate pair in UTF-16.
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    81
     * Use of {@link Character#isSupplementaryCodePoint} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static boolean neededFor(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    84
        return Character.isSupplementaryCodePoint(uc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    88
     * Returns the high UTF-16 surrogate for the given supplementary UCS-4 character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static char high(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    91
        assert Character.isSupplementaryCodePoint(uc);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    92
        return (char)((uc >> 10)
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    93
                      + (Character.MIN_HIGH_SURROGATE
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    94
                         - (Character.MIN_SUPPLEMENTARY_CODE_POINT >> 10)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    98
     * Returns the low UTF-16 surrogate for the given supplementary UCS-4 character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static char low(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   101
        assert Character.isSupplementaryCodePoint(uc);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   102
        return (char)((uc & 0x3ff) + Character.MIN_LOW_SURROGATE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Converts the given surrogate pair into a 32-bit UCS-4 character.
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   107
     * Use of {@link Character#toCodePoint} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static int toUCS4(char c, char d) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   110
        assert Character.isHighSurrogate(c) && Character.isLowSurrogate(d);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   111
        return Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Surrogate parsing support.  Charset implementations may use instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * this class to handle the details of parsing UTF-16 surrogate pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public Parser() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        private int character;          // UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        private CoderResult error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        private boolean isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Returns the UCS-4 character previously parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public int character() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * Tells whether or not the previously-parsed UCS-4 character was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * originally represented by a surrogate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public boolean isPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * Returns the number of UTF-16 characters consumed by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * parse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public int increment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return isPair ? 2 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * If the previous parse operation detected an error, return the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            assert (error != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * Returns an unmappable-input result object, with the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * input length, for the previously-parsed character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public CoderResult unmappableResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return CoderResult.unmappableForLength(isPair ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * @param  in   The source buffer, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        public int parse(char c, CharBuffer in) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   184
            if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (!in.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                char d = in.get();
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   190
                if (Character.isLowSurrogate(d)) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   191
                    character = Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   199
            if (Character.isLowSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * @param  ia   The input array, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * @param  ip   The input index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * @param  il   The input limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public int parse(char c, char[] ia, int ip, int il) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            assert (ia[ip] == c);
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   226
            if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (il - ip < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                char d = ia[ip + 1];
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   232
                if (Character.isLowSurrogate(d)) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   233
                    character = Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   241
            if (Character.isLowSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
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
     * Surrogate generation support.  Charset implementations may use instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * of this class to handle the details of generating UTF-16 surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public static class Generator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        public Generator() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        private CoderResult error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * If the previous generation operation detected an error, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * object describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            assert error != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * @param  dst  The destination buffer, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public int generate(int uc, int len, CharBuffer dst) {
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   288
            if (Character.isBmpCodePoint(uc)) {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   289
                char c = (char) uc;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   290
                if (Character.isSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (dst.remaining() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   298
                dst.put(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return 1;
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   301
            } else if (Character.isValidCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (dst.remaining() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                dst.put(Surrogate.high(uc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                dst.put(Surrogate.low(uc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return 2;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   310
            } else {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   311
                error = CoderResult.unmappableForLength(len);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   312
                return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
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
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * @param  da   The destination array, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @param  dp   The destination position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * @param  dl   The destination limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        public int generate(int uc, int len, char[] da, int dp, int dl) {
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   333
            if (Character.isBmpCodePoint(uc)) {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   334
                char c = (char) uc;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   335
                if (Character.isSurrogate(c)) {
2
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
                }
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   343
                da[dp] = c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return 1;
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   346
            } else if (Character.isValidCodePoint(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
}