jdk/src/share/classes/sun/io/CharToByteEUC_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, 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.EUC_TW;
       
    29 
       
    30 /*
       
    31  * @author Limin Shi
       
    32  */
       
    33 
       
    34 public class CharToByteEUC_TW extends CharToByteConverter
       
    35 {
       
    36     private final EUC_TW.Encoder enc = (EUC_TW.Encoder)(new EUC_TW().newEncoder());
       
    37 
       
    38     public int flush(byte[] output, int outStart, int outEnd)
       
    39         throws MalformedInputException
       
    40     {
       
    41         reset();
       
    42         return 0;
       
    43     }
       
    44 
       
    45     public void reset() {
       
    46         byteOff = charOff = 0;
       
    47     }
       
    48 
       
    49     public boolean canConvert(char ch){
       
    50         return enc.canEncode(ch);
       
    51     }
       
    52 
       
    53     /**
       
    54      * Character conversion
       
    55      */
       
    56     public int convert(char[] input, int inOff, int inEnd,
       
    57                        byte[] output, int outOff, int outEnd)
       
    58         throws UnknownCharacterException, MalformedInputException,
       
    59                ConversionBufferFullException
       
    60     {
       
    61         int outputSize;
       
    62         byte [] tmpbuf = new byte[4];;
       
    63         byte [] outputByte;
       
    64         byteOff = outOff;
       
    65 
       
    66         //Fixed 4122961 by bringing the charOff++ out to this
       
    67         // loop where it belongs, changing the loop from
       
    68         // while(){} to for(){}.
       
    69         for (charOff = inOff; charOff < inEnd; charOff++) {
       
    70             outputByte = tmpbuf;
       
    71             if ( input[charOff] < 0x80) {       // ASCII
       
    72                 outputSize = 1;
       
    73                 outputByte[0] = (byte)(input[charOff] & 0x7f);
       
    74             } else {
       
    75                 outputSize = enc.toEUC(input[charOff], outputByte);
       
    76             }
       
    77 
       
    78             if (outputSize == -1) {
       
    79                 if (subMode) {
       
    80                     outputByte = subBytes;
       
    81                     outputSize = subBytes.length;
       
    82                 } else {
       
    83                     badInputLength = 1;
       
    84                     throw new UnknownCharacterException();
       
    85                 }
       
    86             }
       
    87 
       
    88             if (outEnd - byteOff < outputSize)
       
    89                 throw new ConversionBufferFullException();
       
    90 
       
    91             for (int i = 0; i < outputSize; i++)
       
    92                 output[byteOff++] = outputByte[i];
       
    93         }
       
    94 
       
    95         return byteOff - outOff;
       
    96 
       
    97     }
       
    98 
       
    99     /**
       
   100      * returns the maximum number of bytes needed to convert a char
       
   101      */
       
   102     public int getMaxBytesPerChar() {
       
   103         return 4;
       
   104     }
       
   105 
       
   106     /**
       
   107      * Return the character set ID
       
   108      */
       
   109     public String getCharacterEncoding() {
       
   110         return "EUC_TW";
       
   111     }
       
   112 }