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