jdk/src/share/classes/sun/io/ByteToCharEUC_JP_Solaris.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) 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 
       
    26 package sun.io;
       
    27 
       
    28 import sun.nio.cs.ext.JIS_X_0208_Solaris_Decoder;
       
    29 import sun.nio.cs.ext.JIS_X_0212_Solaris_Decoder;
       
    30 /**
       
    31  *
       
    32  * @author Limin Shi
       
    33  * @author Ian Little
       
    34  *
       
    35  * EUC_JP variant converter for Solaris with vendor defined chars
       
    36  * added (4765370)
       
    37  */
       
    38 
       
    39 
       
    40 public class ByteToCharEUC_JP_Solaris extends ByteToCharEUC_JP {
       
    41     private byte savedSecond = 0;
       
    42 
       
    43     ByteToCharJIS0201 bcJIS0201 = new ByteToCharJIS0201();
       
    44     ByteToCharJIS0212_Solaris bcJIS0212 = new ByteToCharJIS0212_Solaris();
       
    45 
       
    46     short[] j0208Index1 = JIS_X_0208_Solaris_Decoder.getIndex1();
       
    47     String[] j0208Index2 = JIS_X_0208_Solaris_Decoder.getIndex2();
       
    48     ByteToCharJIS0212_Solaris j0212Decoder = new ByteToCharJIS0212_Solaris();
       
    49 
       
    50     public ByteToCharEUC_JP_Solaris() {
       
    51         super();
       
    52         start = 0xA1;
       
    53         end = 0xFE;
       
    54         savedSecond = 0;
       
    55     }
       
    56 
       
    57     public int flush(char[] output, int outStart, int outEnd)
       
    58         throws MalformedInputException
       
    59     {
       
    60         if (savedSecond != 0) {
       
    61             reset();
       
    62             throw new MalformedInputException();
       
    63         }
       
    64         reset();
       
    65         return 0;
       
    66     }
       
    67 
       
    68     /**
       
    69      * Resets the converter.
       
    70      * Call this method to reset the converter to its initial state
       
    71      */
       
    72     public void reset() {
       
    73         super.reset();
       
    74         savedSecond = 0;
       
    75     }
       
    76 
       
    77     public String getCharacterEncoding() {
       
    78         return "eucJP-open";
       
    79     }
       
    80 
       
    81     protected char convSingleByte(int b) {
       
    82         if (b < 0 || b > 0x7F)
       
    83             return REPLACE_CHAR;
       
    84         return bcJIS0201.getUnicode(b);
       
    85     }
       
    86 
       
    87     protected char getUnicode(int byte1, int byte2) {
       
    88         if (byte1 == 0x8E) {
       
    89             return bcJIS0201.getUnicode(byte2 - 256);
       
    90         }
       
    91         // Fix for bug 4121358 - similar fix for bug 4117820 put
       
    92         // into ByteToCharDoubleByte.getUnicode()
       
    93         if (((byte1 < 0) || (byte1 > j0208Index1.length))
       
    94             || ((byte2 < start) || (byte2 > end)))
       
    95             return REPLACE_CHAR;
       
    96 
       
    97         char result = super.getUnicode(byte1, byte2);
       
    98         if (result != '\uFFFD') {
       
    99             return result;
       
   100         } else {
       
   101             int n = (j0208Index1[byte1 - 0x80] & 0xf) * (end - start + 1)
       
   102                 + (byte2 - start);
       
   103         return j0208Index2[j0208Index1[byte1 - 0x80] >> 4].charAt(n);
       
   104         }
       
   105     }
       
   106 
       
   107     protected char decode0212(int byte1, int byte2) {
       
   108         return j0212Decoder.getUnicode(byte1, byte2);
       
   109     }
       
   110 
       
   111     /**
       
   112      * Converts sequences of bytes to characters.
       
   113      * Conversions that result in Exceptions can be restarted by calling
       
   114      * convert again, with appropriately modified parameters.
       
   115      * @return the characters written to output.
       
   116      * @param input byte array containing text in Double/single Byte
       
   117      * @param inStart offset in input array
       
   118      * @param inEnd offset of last byte to be converted
       
   119      * @param output character array to receive conversion result
       
   120      * @param outStart starting offset
       
   121      * @param outEnd offset of last byte to be written to
       
   122      * @throw UnsupportedCharacterException for any bytes
       
   123      * that cannot be converted to the external character set.
       
   124      */
       
   125     public int convert(byte[] input, int inOff, int inEnd,
       
   126                        char[] output, int outOff, int outEnd)
       
   127         throws UnknownCharacterException,
       
   128                ConversionBufferFullException
       
   129     {
       
   130         char    outputChar = REPLACE_CHAR;
       
   131         int     inputSize = 0;          // Size of input
       
   132 
       
   133         // Record beginning offsets
       
   134         charOff = outOff;
       
   135         byteOff = inOff;
       
   136 
       
   137         // Loop until we hit the end of the input
       
   138         while (byteOff < inEnd) {
       
   139             int byte1, byte2;
       
   140 
       
   141             if (savedByte == 0) {
       
   142                 byte1 = input[byteOff];
       
   143                 inputSize = 1;
       
   144             } else {
       
   145                 byte1 = savedByte;
       
   146                 savedByte = 0;
       
   147                 inputSize = 0;
       
   148             }
       
   149 
       
   150             outputChar = convSingleByte(byte1);
       
   151 
       
   152             if (outputChar == REPLACE_CHAR) {   // Multibyte char
       
   153                 if ((byte1 & 0xff) == 0x8F) {   // JIS0212
       
   154                     if (byteOff + inputSize + 1 >= inEnd) {
       
   155                         // split in the middle of a character
       
   156                         // save the first 2 bytes for next time around
       
   157                         savedByte = (byte) byte1;
       
   158                         byteOff += inputSize;
       
   159                         if (byteOff < inEnd) {
       
   160                             savedSecond = input[byteOff];
       
   161                             byteOff++;
       
   162                         }
       
   163                         break;
       
   164                     }
       
   165                     if (savedSecond != 0) {
       
   166                         byte1 = savedSecond & 0xff;
       
   167                         savedSecond = 0;
       
   168                     } else {
       
   169                         byte1 = input[byteOff + inputSize] & 0xff;
       
   170                         inputSize++;
       
   171                     }
       
   172                     byte2 = input[byteOff + inputSize] & 0xff;
       
   173                     inputSize++;
       
   174                     outputChar = bcJIS0212.getUnicode(byte1-0x80, byte2-0x80);
       
   175                 } else { // JIS0208
       
   176                     if (byteOff + inputSize >= inEnd) {
       
   177                         // split in the middle of a character
       
   178                         // save the first byte for next time around
       
   179                         savedByte = (byte) byte1;
       
   180                         byteOff += inputSize;
       
   181                         break;
       
   182                     }
       
   183                     byte1 &= 0xff;
       
   184                     byte2 = input[byteOff + inputSize] & 0xff;
       
   185                     inputSize++;
       
   186                     outputChar = getUnicode(byte1, byte2);
       
   187                 }
       
   188             }
       
   189 
       
   190             if (outputChar == REPLACE_CHAR) {
       
   191                 if (subMode)
       
   192                     outputChar = subChars[0];
       
   193                 else {
       
   194                     badInputLength = inputSize;
       
   195                     throw new UnknownCharacterException();
       
   196                 }
       
   197             }
       
   198 
       
   199             if (charOff >= outEnd)
       
   200                 throw new ConversionBufferFullException();
       
   201 
       
   202             output[charOff++] = outputChar;
       
   203             byteOff += inputSize;
       
   204         }
       
   205 
       
   206         return charOff - outOff;
       
   207     }
       
   208 
       
   209 }