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