jdk/src/share/classes/sun/nio/cs/Surrogate.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3324 02cc89024ea2
permissions -rw-r--r--
Initial load
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Utility class for dealing with surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // UTF-16 surrogate-character ranges
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static final char MIN_HIGH = '\uD800';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static final char MAX_HIGH = '\uDBFF';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static final char MIN_LOW  = '\uDC00';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final char MAX_LOW  = '\uDFFF';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static final char MIN = MIN_HIGH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final char MAX = MAX_LOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Range of UCS-4 values that need surrogates in UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int UCS4_MIN = 0x10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final int UCS4_MAX = (1 << 20) + UCS4_MIN - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Tells whether or not the given UTF-16 value is a high surrogate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static boolean isHigh(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return (MIN_HIGH <= c) && (c <= MAX_HIGH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Tells whether or not the given UTF-16 value is a low surrogate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static boolean isLow(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return (MIN_LOW <= c) && (c <= MAX_LOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Tells whether or not the given UTF-16 value is a surrogate character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static boolean is(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return (MIN <= c) && (c <= MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Tells whether or not the given UCS-4 character must be represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * surrogate pair in UTF-16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static boolean neededFor(int uc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return (uc >= UCS4_MIN) && (uc <= UCS4_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Returns the high UTF-16 surrogate for the given UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static char high(int uc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        assert neededFor(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return (char)(0xd800 | (((uc - UCS4_MIN) >> 10) & 0x3ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns the low UTF-16 surrogate for the given UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static char low(int uc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        assert neededFor(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return (char)(0xdc00 | ((uc - UCS4_MIN) & 0x3ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Converts the given surrogate pair into a 32-bit UCS-4 character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static int toUCS4(char c, char d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        assert isHigh(c) && isLow(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return (((c & 0x3ff) << 10) | (d & 0x3ff)) + 0x10000;
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
     * Surrogate parsing support.  Charset implementations may use instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * this class to handle the details of parsing UTF-16 surrogate pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static class Parser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public Parser() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        private int character;          // UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        private CoderResult error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        private boolean isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * Returns the UCS-4 character previously parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        public int character() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * Tells whether or not the previously-parsed UCS-4 character was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * originally represented by a surrogate pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        public boolean isPair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return isPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * Returns the number of UTF-16 characters consumed by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * parse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public int increment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return isPair ? 2 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * If the previous parse operation detected an error, return the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            assert (error != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Returns an unmappable-input result object, with the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * input length, for the previously-parsed character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public CoderResult unmappableResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            assert (error == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return CoderResult.unmappableForLength(isPair ? 2 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * @param  in   The source buffer, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        public int parse(char c, CharBuffer in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (Surrogate.isHigh(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (!in.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                char d = in.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (Surrogate.isLow(d)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    character = toUCS4(c, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (Surrogate.isLow(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * Parses a UCS-4 character from the given source buffer, handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
         * @param  c    The first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * @param  ia   The input array, from which one more character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         *              will be consumed if c is a high surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * @param  ip   The input index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * @param  il   The input limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * @returns  Either a parsed UCS-4 character, in which case the isPair()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         *           and increment() methods will return meaningful values, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         *           -1, in which case error() will return a descriptive result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         *           object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public int parse(char c, char[] ia, int ip, int il) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            assert (ia[ip] == c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (Surrogate.isHigh(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (il - ip < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    error = CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                char d = ia[ip + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (Surrogate.isLow(d)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    character = toUCS4(c, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    isPair = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (Surrogate.isLow(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                error = CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            character = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            isPair = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Surrogate generation support.  Charset implementations may use instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * of this class to handle the details of generating UTF-16 surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public static class Generator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        public Generator() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        private CoderResult error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * If the previous generation operation detected an error, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * object describing that error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        public CoderResult error() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            assert error != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * Generates one or two UTF-16 characters to represent the given UCS-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * @param  uc   The UCS-4 character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * @param  len  The number of input bytes from which the UCS-4 value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         *              was constructed (used when creating result objects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * @param  dst  The destination buffer, to which one or two UTF-16
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         *              characters will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * @returns  Either a positive count of the number of UTF-16 characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         *           written to the destination buffer, or -1, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         *           error() will return a descriptive result object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        public int generate(int uc, int len, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (uc <= 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (Surrogate.is(uc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (dst.remaining() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    error = CoderResult.OVERFLOW;
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
                dst.put((char)uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                error = null;
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 (uc < Surrogate.UCS4_MIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                error = CoderResult.malformedForLength(len);
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
            if (uc <= Surrogate.UCS4_MAX) {
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            error = CoderResult.unmappableForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return -1;
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (uc <= 0xffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (Surrogate.is(uc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (dl - dp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    error = CoderResult.OVERFLOW;
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
                da[dp] = (char)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                error = null;
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
            if (uc < Surrogate.UCS4_MIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                error = CoderResult.malformedForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (uc <= Surrogate.UCS4_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (dl - dp < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    error = CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                da[dp] = Surrogate.high(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                da[dp + 1] = Surrogate.low(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                error = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            error = CoderResult.unmappableForLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return -1;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
}