jdk/src/share/classes/sun/io/CharToByteCp949.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2921 d9d491a5a169
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 1997-2003 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
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import sun.nio.cs.ext.IBM949;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
* @author Malcolm Ayres
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class CharToByteCp949 extends CharToByteConverter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static final char SBase = '\uAC00';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static final char LBase = '\u1100';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static final char VBase = '\u1161';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final char TBase = '\u11A7';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final int  VCount = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final int  TCount = 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final byte G0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final byte G1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final byte G2 = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final byte G3 = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private byte   charState = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private char   l, v, t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private byte[] outputByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private char highHalfZoneCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private int  mask1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int  mask2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int  shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private short[] index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String index2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private String index2a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static IBM949 nioCoder = new IBM949();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public CharToByteCp949() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
       super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
       highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
       outputByte = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
       mask1 = 0xFFF8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
       mask2 = 0x0007;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
       shift = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
       index1 = nioCoder.getEncoderIndex1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
       index2 = nioCoder.getEncoderIndex2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
       index2a = nioCoder.getEncoderIndex2a();
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
      * flush out any residual data and reset the buffer state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public int flush(byte[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throws MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       int bytesOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
       byteOff = outStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
       if (highHalfZoneCode != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
           badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
           throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
       if (charState != G0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
              unicodeToBuffer(composeHangul() ,output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
           catch(UnknownCharacterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
              reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
              badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
              throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
           charState = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
       bytesOut = byteOff - outStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
       reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
       return bytesOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Resets converter to its initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
       highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
       charState = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
       charOff = byteOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns true if the given character can be converted to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * target character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public boolean canConvert(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
       int  index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
       int  theBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
       index = index1[((ch & mask1) >> shift)] + (ch & mask2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
       if (index < 15000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         theBytes = (int)(index2.charAt(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
       else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         theBytes = (int)(index2a.charAt(index-15000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
       if (theBytes != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
          return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
       // only return true if input char was unicode null - all others are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
       //    undefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
       return( ch == '\u0000');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public int convert(char[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                       byte[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws UnknownCharacterException, MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
       char    inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
       int     inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
       charOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
       byteOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
       while (charOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
          if (highHalfZoneCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
             inputChar = input[charOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
             inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
             inputChar = highHalfZoneCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
             inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
          switch (charState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
          case G0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             l = LBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             v = VBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
             t = TBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
             if ( isLeadingC(inputChar) ) {     // Leading Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                l = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                charState = G1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             if ( isVowel(inputChar) ) {        // Vowel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                v = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                charState = G2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             if ( isTrailingC(inputChar) ) {    // Trailing Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                t = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                charState = G3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
          case G1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
             if ( isLeadingC(inputChar) ) {     // Leading Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                l = composeLL(l, inputChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
             if ( isVowel(inputChar) ) {        // Vowel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                v = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                charState = G2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
             if ( isTrailingC(inputChar) ) {    // Trailing Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                t = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                charState = G3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
             unicodeToBuffer(composeHangul(), output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
             charState = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
          case G2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
             if ( isLeadingC(inputChar) ) {     // Leading Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                unicodeToBuffer(composeHangul(), output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                l = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                v = VBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                t = TBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                charState = G1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
             if ( isVowel(inputChar) ) {        // Vowel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                v = composeVV(l, inputChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                charState = G2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
             if ( isTrailingC(inputChar) ) {    // Trailing Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                t = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                charState = G3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             unicodeToBuffer(composeHangul(), output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
             charState = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
          case G3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
             if ( isTrailingC(inputChar) ) {    // Trailing Consonant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                t = composeTT(t, inputChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                charState = G3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
             unicodeToBuffer(composeHangul(), output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             charState = G0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
          if (charState != G0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
             charOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
          else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
             // Is this a high surrogate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
             if(inputChar >= '\ud800' && inputChar <= '\udbff') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                // Is this the last character of the input?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (charOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                   highHalfZoneCode = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                   charOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                   break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                // Is there a low surrogate following?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                inputChar = input[charOff + inputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (inputChar >= '\udc00' && inputChar <= '\udfff') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                   // We have a valid surrogate pair.  Too bad we don't do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                   // surrogates.  Is substitution enabled?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                   if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                      if (subBytes.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                         outputByte[0] = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                         outputByte[1] = subBytes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                         outputByte[0] = subBytes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                         outputByte[1] = subBytes[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                      bytesToBuffer(outputByte, output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                      inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                      badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                      throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                   // We have a malformed surrogate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                   badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                   throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
               // Is this an unaccompanied low surrogate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                   badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                   throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                   unicodeToBuffer(inputChar, output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
             charOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
       return byteOff - outOff;
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
    private char composeHangul() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
       int lIndex, vIndex, tIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
       lIndex = l - LBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
       vIndex = v - VBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
       tIndex = t - TBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
       return (char)((lIndex * VCount + vIndex) * TCount + tIndex + SBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private char composeLL(char l1, char l2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
       return l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private char composeVV(char v1, char v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
       return v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private char composeTT(char t1, char t2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
       return t2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private boolean isLeadingC(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
       return (c >= LBase && c <= '\u1159');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private boolean isVowel(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
       return (c >= VBase && c <= '\u11a2');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private boolean isTrailingC(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
       return (c >= TBase && c <= '\u11f9');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * returns the maximum number of bytes needed to convert a char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public int getMaxBytesPerChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
       return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Return the character set ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
       return "Cp949";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * private function to add the bytes to the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private void bytesToBuffer(byte[] theBytes, byte[] output, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        throws ConversionBufferFullException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
               UnknownCharacterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
       int spaceNeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
       // ensure sufficient space for the bytes(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
       if (theBytes[0] == 0x00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
          spaceNeeded = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
       else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
          spaceNeeded = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
       if (byteOff + spaceNeeded > outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
          throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
       // move the data into the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
       if (spaceNeeded == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
          output[byteOff++] = theBytes[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
       else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
          output[byteOff++] = theBytes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
          output[byteOff++] = theBytes[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * private function to add a unicode character to the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    private void unicodeToBuffer(char unicode, byte[] output, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        throws ConversionBufferFullException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
               UnknownCharacterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
       int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
       int theBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
       // first we convert the unicode to its byte representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
       index = index1[((unicode & mask1) >> shift)] + (unicode & mask2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
       if (index < 15000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         theBytes = (int)(index2.charAt(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
       else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         theBytes = (int)(index2a.charAt(index-15000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
       outputByte[0] = (byte)((theBytes & 0x0000ff00)>>8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
       outputByte[1] = (byte)(theBytes & 0x000000ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
       // if the unicode was not mappable - look for the substitution bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
       if (outputByte[0] == 0x00 && outputByte[1] == 0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                          && unicode != '\u0000') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
          if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
             if (subBytes.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                outputByte[0] = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                outputByte[1] = subBytes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                outputByte[0] = subBytes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                outputByte[1] = subBytes[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
             badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
             throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
       // now put the bytes in the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
       bytesToBuffer(outputByte, output, outEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}