jdk/src/share/classes/sun/io/ByteToCharEUC_TW.java
changeset 10372 2f6d68f22eae
parent 10321 64f7ee2f31dd
parent 10371 7da2112e4236
child 10373 d4c5e59b82f8
equal deleted inserted replaced
10321:64f7ee2f31dd 10372:2f6d68f22eae
     1 /*
       
     2  * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.io;
       
    27 
       
    28 import sun.nio.cs.ext.EUC_TW;
       
    29 
       
    30 /*
       
    31  * @author Limin Shi
       
    32  */
       
    33 public class ByteToCharEUC_TW extends ByteToCharConverter
       
    34 {
       
    35     private final byte G0 = 0;
       
    36     private final byte G1 = 1;
       
    37     private final byte G2 = 2;
       
    38     private final byte G3 = 3;
       
    39     private final byte G4 = 4;
       
    40     private final byte MSB = (byte) 0x80;
       
    41     private final byte SS2 = (byte) 0x8E;
       
    42 
       
    43     private byte firstByte = 0, state = G0;
       
    44     private int cnsPlane = 0;
       
    45 
       
    46     private EUC_TW.Decoder dec = (EUC_TW.Decoder)(new EUC_TW().newDecoder());
       
    47 
       
    48     public ByteToCharEUC_TW() {
       
    49     }
       
    50 
       
    51     public int flush(char[] output, int outStart, int outEnd)
       
    52         throws MalformedInputException
       
    53     {
       
    54         if (state != G0) {
       
    55             state = G0;
       
    56             firstByte = 0;
       
    57             badInputLength = 0;
       
    58             throw new MalformedInputException();
       
    59         }
       
    60         reset();
       
    61         return 0;
       
    62     }
       
    63 
       
    64     public void reset() {
       
    65         dec.reset();
       
    66         state = G0;
       
    67         firstByte = 0;
       
    68         byteOff = charOff = 0;
       
    69     }
       
    70 
       
    71     /**
       
    72      * Character conversion
       
    73      */
       
    74     public int convert(byte[] input, int inOff, int inEnd,
       
    75                        char[] output, int outOff, int outEnd)
       
    76         throws UnknownCharacterException, MalformedInputException,
       
    77                ConversionBufferFullException
       
    78     {
       
    79         int inputSize = 0;
       
    80         char[] c1 = new char[1];
       
    81 
       
    82         byteOff = inOff;
       
    83         charOff = outOff;
       
    84 
       
    85         cnsPlane = 3;
       
    86         while (byteOff < inEnd) {
       
    87             if (charOff >= outEnd)
       
    88                 throw new ConversionBufferFullException();
       
    89             char[] outputChar = null;
       
    90             switch (state) {
       
    91             case G0:
       
    92                 if ( (input[byteOff] & MSB) == 0) {     // ASCII
       
    93                     outputChar = c1;
       
    94                     outputChar[0] = (char) input[byteOff];
       
    95                 } else if (input[byteOff] == SS2) {     // Codeset 2
       
    96                     state = G2;
       
    97                 } else {                                // Codeset 1
       
    98                     firstByte = input[byteOff];
       
    99                     state = G1;
       
   100                 }
       
   101                 break;
       
   102             case G1:
       
   103                 inputSize = 2;
       
   104                 if ( (input[byteOff] & MSB) != 0) {     // 2nd byte
       
   105                     cnsPlane = 0;
       
   106                     outputChar = dec.toUnicode(firstByte & 0xff,
       
   107                                                input[byteOff] & 0xff,
       
   108                                                cnsPlane);
       
   109                 } else {                                // Error
       
   110                     badInputLength = 1;
       
   111                     throw new MalformedInputException();
       
   112                 }
       
   113                 firstByte = 0;
       
   114                 state = G0;
       
   115                 break;
       
   116             case G2:
       
   117                 cnsPlane = (input[byteOff] & (byte)0x0f);
       
   118                 // Adjust String array index for plan 15
       
   119                 cnsPlane = (cnsPlane == 15)? 8 : cnsPlane;
       
   120 
       
   121                 if (cnsPlane < 15) {
       
   122                      state = G3;
       
   123                 } else {
       
   124                     badInputLength = 2;
       
   125                     throw new MalformedInputException();
       
   126                 }
       
   127 
       
   128                 break;
       
   129             case G3:
       
   130                 if ( (input[byteOff] & MSB) != 0) {     // 1st byte
       
   131                     firstByte = input[byteOff];
       
   132                     state = G4;
       
   133                 } else {                                // Error
       
   134                     state = G0;
       
   135                     badInputLength = 2;
       
   136                     throw new MalformedInputException();
       
   137                 }
       
   138                 break;
       
   139             case G4:
       
   140                 if ( (input[byteOff] & MSB) != 0) {     // 2nd byte
       
   141                     outputChar = dec.toUnicode(firstByte & 0xff,
       
   142                                                input[byteOff] & 0xff,
       
   143                                                cnsPlane - 1);
       
   144                 } else {                                // Error
       
   145                     badInputLength = 3;
       
   146                     throw new MalformedInputException();
       
   147                 }
       
   148                 firstByte = 0;
       
   149                 state = G0;
       
   150                 break;
       
   151             }
       
   152             byteOff++;
       
   153             if (state == G0) {
       
   154                 if (outputChar == null) {
       
   155                     if (subMode) {               // substitution enabled
       
   156                         outputChar = c1;
       
   157                         outputChar[0] = subChars[0];
       
   158                     } else {
       
   159                         badInputLength = inputSize;
       
   160                         throw new UnknownCharacterException();
       
   161                     }
       
   162                 }
       
   163                 output[charOff++] = outputChar[0];
       
   164             }
       
   165         }
       
   166         return charOff - outOff;
       
   167     }
       
   168 
       
   169 
       
   170     /**
       
   171      * Return the character set ID
       
   172      */
       
   173     public String getCharacterEncoding() {
       
   174         return "EUC_TW";
       
   175     }
       
   176 }