jdk/src/share/classes/sun/nio/cs/Surrogate.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 5144 9fab813ff16c
child 5986 04eb44085c00
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5144
diff changeset
     2
 * Copyright (c) 2000, 2001, 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
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
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    54
     * Tells whether or not the given value is in the high surrogate range.
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    55
     * Use of {@link Character#isHighSurrogate} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static boolean isHigh(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return (MIN_HIGH <= c) && (c <= MAX_HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    62
     * Tells whether or not the given value is in the low surrogate range.
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    63
     * Use of {@link Character#isLowSurrogate} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static boolean isLow(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (MIN_LOW <= c) && (c <= MAX_LOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    70
     * Tells whether or not the given value is in the surrogate range.
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    71
     * Use of {@link Character#isSurrogate} is generally preferred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static boolean is(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return (MIN <= c) && (c <= MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    78
     * Tells whether or not the given UCS-4 character is in the Basic
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    79
     * Multilingual Plane, and can be represented using a single char.
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    80
     */
5144
9fab813ff16c 6931812: A better implementation of sun.nio.cs.Surrogate.isBMP(int)
martin
parents: 3714
diff changeset
    81
    public static boolean isBMPCodePoint(int uc) {
9fab813ff16c 6931812: A better implementation of sun.nio.cs.Surrogate.isBMP(int)
martin
parents: 3714
diff changeset
    82
        return uc >> 16 == 0;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    83
    }
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    84
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    85
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Tells whether or not the given UCS-4 character must be represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * surrogate pair in UTF-16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static boolean neededFor(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    90
        return Character.isSupplementaryCodePoint(uc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
    94
     * Returns the high UTF-16 surrogate for the given supplementary UCS-4 character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static char high(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    97
        assert Character.isSupplementaryCodePoint(uc);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    98
        return (char)((uc >> 10)
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
    99
                      + (Character.MIN_HIGH_SURROGATE
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   100
                         - (Character.MIN_SUPPLEMENTARY_CODE_POINT >> 10)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 3324
diff changeset
   104
     * Returns the low UTF-16 surrogate for the given supplementary UCS-4 character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static char low(int uc) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   107
        assert Character.isSupplementaryCodePoint(uc);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   108
        return (char)((uc & 0x3ff) + Character.MIN_LOW_SURROGATE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Converts the given surrogate pair into a 32-bit UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static int toUCS4(char c, char d) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   115
        assert Character.isHighSurrogate(c) && Character.isLowSurrogate(d);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   116
        return Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Surrogate parsing support.  Charset implementations may use instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * this class to handle the details of parsing UTF-16 surrogate pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public Parser() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        private int character;          // UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        private CoderResult error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        private boolean isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * Returns the UCS-4 character previously parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public int character() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * Tells whether or not the previously-parsed UCS-4 character was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * originally represented by a surrogate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public boolean isPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * Returns the number of UTF-16 characters consumed by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * parse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public int increment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return isPair ? 2 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * If the previous parse operation detected an error, return the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            assert (error != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * Returns an unmappable-input result object, with the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * input length, for the previously-parsed character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public CoderResult unmappableResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return CoderResult.unmappableForLength(isPair ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * @param  in   The source buffer, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        public int parse(char c, CharBuffer in) {
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   189
            if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (!in.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                char d = in.get();
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   195
                if (Character.isLowSurrogate(d)) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   196
                    character = Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   204
            if (Character.isLowSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * @param  ia   The input array, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * @param  ip   The input index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         * @param  il   The input limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        public int parse(char c, char[] ia, int ip, int il) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            assert (ia[ip] == c);
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   231
            if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                if (il - ip < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                char d = ia[ip + 1];
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   237
                if (Character.isLowSurrogate(d)) {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   238
                    character = Character.toCodePoint(c, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   246
            if (Character.isLowSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Surrogate generation support.  Charset implementations may use instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * of this class to handle the details of generating UTF-16 surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public static class Generator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        public Generator() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        private CoderResult error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         * If the previous generation operation detected an error, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * object describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            assert error != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * @param  dst  The destination buffer, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        public int generate(int uc, int len, CharBuffer dst) {
5144
9fab813ff16c 6931812: A better implementation of sun.nio.cs.Surrogate.isBMP(int)
martin
parents: 3714
diff changeset
   293
            if (Surrogate.isBMPCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (Surrogate.is(uc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (dst.remaining() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                dst.put((char)uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return 1;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   305
            } else if (Character.isSupplementaryCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (dst.remaining() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                dst.put(Surrogate.high(uc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                dst.put(Surrogate.low(uc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                return 2;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   314
            } else {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   315
                error = CoderResult.unmappableForLength(len);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   316
                return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * @param  da   The destination array, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * @param  dp   The destination position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * @param  dl   The destination limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        public int generate(int uc, int len, char[] da, int dp, int dl) {
5144
9fab813ff16c 6931812: A better implementation of sun.nio.cs.Surrogate.isBMP(int)
martin
parents: 3714
diff changeset
   337
            if (Surrogate.isBMPCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (Surrogate.is(uc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (dl - dp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                da[dp] = (char)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return 1;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   349
            } else if (Character.isSupplementaryCodePoint(uc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (dl - dp < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                da[dp] = Surrogate.high(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                da[dp + 1] = Surrogate.low(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return 2;
3324
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   358
            } else {
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   359
                error = CoderResult.unmappableForLength(len);
02cc89024ea2 6639458: Improvements to Surrogate.java
martin
parents: 2
diff changeset
   360
                return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}