jdk/src/share/classes/sun/io/ByteToCharCp964.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) 1997, 2003, 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 package sun.io;
       
    26 
       
    27 import sun.nio.cs.ext.IBM964;
       
    28 
       
    29 /**
       
    30 * @author Malcolm Ayres
       
    31 */
       
    32 public class ByteToCharCp964 extends ByteToCharConverter
       
    33 {
       
    34     private final static IBM964 nioCoder = new IBM964();
       
    35 
       
    36     private final int G0 = 0;
       
    37     private final int G1 = 1;
       
    38     private final int G2 = 2;
       
    39     private final int G3 = 3;
       
    40     private final int G4 = 4;
       
    41     private final int SS2 =  0x8E;
       
    42     private final int SS3 =  0x8F;
       
    43 
       
    44     private int firstByte, state;
       
    45 
       
    46     private String byteToCharTable;
       
    47     private String mappingTableG1;
       
    48     private String mappingTableG2;
       
    49     private String mappingTableG2a2;
       
    50     private String mappingTableG2ac;
       
    51     private String mappingTableG2ad;
       
    52 
       
    53 
       
    54     public ByteToCharCp964() {
       
    55        super();
       
    56        state = G0;
       
    57        byteToCharTable = nioCoder.getDecoderSingleByteMappings();
       
    58        mappingTableG1 = nioCoder.getDecoderMappingTableG1();
       
    59        mappingTableG2a2 = nioCoder.getDecoderMappingTableG2a2();
       
    60        mappingTableG2ac = nioCoder.getDecoderMappingTableG2ac();
       
    61        mappingTableG2ad = nioCoder.getDecoderMappingTableG2ad();
       
    62     }
       
    63 
       
    64     /**
       
    65       * Return the character set id
       
    66       */
       
    67     public String getCharacterEncoding()
       
    68     {
       
    69        return "Cp964";
       
    70     }
       
    71 
       
    72     /**
       
    73       * flush out any residual data and reset the buffer state
       
    74       */
       
    75     public int flush(char[] output, int outStart, int outEnd)
       
    76        throws MalformedInputException
       
    77     {
       
    78        if (state != G0) {
       
    79           reset();
       
    80           badInputLength = 0;
       
    81           throw new MalformedInputException();
       
    82        }
       
    83 
       
    84        reset();
       
    85        return 0;
       
    86     }
       
    87 
       
    88     /**
       
    89      *  Resets the converter.
       
    90      */
       
    91     public void reset() {
       
    92        state = G0;
       
    93        charOff = byteOff = 0;
       
    94     }
       
    95 
       
    96     /**
       
    97      * Character conversion
       
    98      */
       
    99     public int convert(byte[] input, int inOff, int inEnd,
       
   100                        char[] output, int outOff, int outEnd)
       
   101         throws UnknownCharacterException, MalformedInputException,
       
   102                ConversionBufferFullException
       
   103     {
       
   104 
       
   105        int       byte1;
       
   106        char      outputChar = '\uFFFD';
       
   107 
       
   108        byteOff = inOff;
       
   109        charOff = outOff;
       
   110 
       
   111        while (byteOff < inEnd) {
       
   112 
       
   113           byte1 = input[byteOff];
       
   114           if (byte1 < 0)
       
   115             byte1 += 256;
       
   116 
       
   117           switch (state) {
       
   118              case G0:
       
   119                 if (byte1 == SS2)
       
   120                    state = G2;
       
   121                 else if (byte1 == SS3) {
       
   122                    badInputLength = 1;
       
   123                    throw new MalformedInputException();
       
   124                 }
       
   125                 else if ( byte1 <= 0x9f )               // valid single byte
       
   126                    outputChar = byteToCharTable.charAt(byte1);
       
   127                 else if (byte1 < 0xa1 || byte1 > 0xfe) {
       
   128                    badInputLength = 1;
       
   129                    throw new MalformedInputException();
       
   130                 } else {                                // valid 1st byte for G1
       
   131                    firstByte = byte1;
       
   132                    state = G1;
       
   133                 }
       
   134                 break;
       
   135 
       
   136              case G1:
       
   137                 state = G0;
       
   138                 if ( byte1 < 0xa1 || byte1 > 0xfe) {   // valid second byte for G1
       
   139                    badInputLength = 1;
       
   140                    throw new MalformedInputException();
       
   141                 }
       
   142                 outputChar = mappingTableG1.charAt(((firstByte - 0xa1) * 94)  + byte1 - 0xa1);
       
   143                 break;
       
   144 
       
   145              case G2:
       
   146                 // set the correct mapping table for supported G2 sets
       
   147                 if ( byte1 == 0xa2)
       
   148                   mappingTableG2 = mappingTableG2a2;
       
   149                 else
       
   150                 if ( byte1 == 0xac)
       
   151                   mappingTableG2 = mappingTableG2ac;
       
   152                 else
       
   153                 if ( byte1 == 0xad)
       
   154                   mappingTableG2 = mappingTableG2ad;
       
   155                 else {
       
   156                    state = G0;
       
   157                    badInputLength = 1;
       
   158                    throw new MalformedInputException();
       
   159                 }
       
   160                 state = G3;
       
   161                 break;
       
   162 
       
   163              case G3:
       
   164                 if ( byte1 < 0xa1 || byte1 > 0xfe) {  // valid 1st byte for G2 set
       
   165                    state = G0;
       
   166                    badInputLength = 1;
       
   167                    throw new MalformedInputException();
       
   168                 }
       
   169                 firstByte = byte1;
       
   170                 state = G4;
       
   171                 break;
       
   172 
       
   173              case G4:
       
   174                 state = G0;
       
   175                 if ( byte1 < 0xa1 || byte1 > 0xfe) { // valid 2nd byte for G2 set
       
   176                    badInputLength = 1;
       
   177                    throw new MalformedInputException();
       
   178                 }
       
   179                 outputChar = mappingTableG2.charAt(((firstByte - 0xa1) * 94)  + byte1 - 0xa1);
       
   180                 break;
       
   181 
       
   182           }
       
   183 
       
   184           if (state == G0) {
       
   185              if (outputChar == '\uFFFD') {
       
   186                 if (subMode)
       
   187                    outputChar = subChars[0];
       
   188                 else {
       
   189                    badInputLength = 1;
       
   190                    throw new UnknownCharacterException();
       
   191                 }
       
   192              }
       
   193 
       
   194              if (charOff >= outEnd)
       
   195                 throw new ConversionBufferFullException();
       
   196 
       
   197              output[charOff++] = outputChar;
       
   198           }
       
   199 
       
   200           byteOff++;
       
   201 
       
   202        }
       
   203 
       
   204        return charOff - outOff;
       
   205 
       
   206     }
       
   207 }