jdk/src/share/classes/sun/io/CharToByteISO2022.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, 2000, 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 /**
       
    29 * @author Tom Zhou
       
    30 */
       
    31 
       
    32 public abstract class CharToByteISO2022 extends CharToByteConverter
       
    33 {
       
    34     private final byte ISO_ESC = 0x1b;
       
    35     private final byte ISO_SI = 0x0f;
       
    36     private final byte ISO_SO = 0x0e;
       
    37     private final byte ISO_SS2_7 = 0x4e;
       
    38     private final byte ISO_SS3_7 = 0x4f;
       
    39     private final byte SS2 = (byte)0x8e;
       
    40     private final byte P2 = (byte)0xA2;
       
    41     private final byte P3 = (byte)0xA3;
       
    42     private final byte MSB = (byte)0x80;
       
    43 
       
    44     protected final byte maximumDesignatorLength = 4;
       
    45 
       
    46     protected String SODesignator,
       
    47                      SS2Designator = null,
       
    48                      SS3Designator = null;
       
    49 
       
    50     protected CharToByteConverter codeConverter;
       
    51 
       
    52     private boolean shiftout = false;
       
    53     private boolean SODesDefined = false;
       
    54     private boolean SS2DesDefined = false;
       
    55     private boolean SS3DesDefined = false;
       
    56 
       
    57     private boolean newshiftout = false;
       
    58     private boolean newSODesDefined = false;
       
    59     private boolean newSS2DesDefined = false;
       
    60     private boolean newSS3DesDefined = false;
       
    61 
       
    62     public int flush(byte[] output, int outStart, int outEnd)
       
    63         throws MalformedInputException
       
    64     {
       
    65         reset();
       
    66         return 0;
       
    67     }
       
    68 
       
    69     public void reset() {
       
    70         shiftout = false;
       
    71         SODesDefined = false;
       
    72         SS2DesDefined = false;
       
    73         SS3DesDefined = false;
       
    74         byteOff = charOff = 0;
       
    75     }
       
    76 
       
    77     public boolean canConvert(char ch)
       
    78     {
       
    79         if (ch<0x80)
       
    80            return true;
       
    81         return codeConverter.canConvert(ch);
       
    82     }
       
    83 
       
    84     private int unicodeToNative(char unicode, byte ebyte[])
       
    85     {
       
    86         int     index = 0;
       
    87         byte    tmpByte[];
       
    88         byte    convByte[] = new byte[codeConverter.getMaxBytesPerChar()];
       
    89         char    convChar[] = {unicode};
       
    90         int     converted;
       
    91 
       
    92         try{
       
    93             converted = codeConverter.convert(convChar, 0, 1, convByte, 0,
       
    94                                         codeConverter.getMaxBytesPerChar());
       
    95         } catch(Exception e) {
       
    96             return -1;
       
    97         }
       
    98 
       
    99         if (converted == 2) {
       
   100             if (!SODesDefined) {
       
   101                 newSODesDefined = true;
       
   102                 ebyte[0] = ISO_ESC;
       
   103                 tmpByte = SODesignator.getBytes();
       
   104                 System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
       
   105                 index = tmpByte.length+1;
       
   106             }
       
   107             if (!shiftout) {
       
   108                 newshiftout = true;
       
   109                 ebyte[index++] = ISO_SO;
       
   110             }
       
   111             ebyte[index++] = (byte)(convByte[0]&0x7f);
       
   112             ebyte[index++] = (byte)(convByte[1]&0x7f);
       
   113         } else {
       
   114             if((convByte[0] == SS2)&&(convByte[1] == P2)) {
       
   115                 if (!SS2DesDefined) {
       
   116                     newSS2DesDefined = true;
       
   117                     ebyte[0] = ISO_ESC;
       
   118                     tmpByte = SS2Designator.getBytes();
       
   119                     System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
       
   120                     index = tmpByte.length+1;
       
   121                 }
       
   122                 ebyte[index++] = ISO_ESC;
       
   123                 ebyte[index++] = ISO_SS2_7;
       
   124                 ebyte[index++] = (byte)(convByte[2]&0x7f);
       
   125                 ebyte[index++] = (byte)(convByte[3]&0x7f);
       
   126             }
       
   127             if((convByte[0] == SS2)&&(convByte[1] == 0xA3))
       
   128             {
       
   129                 if(!SS3DesDefined){
       
   130                     newSS3DesDefined = true;
       
   131                     ebyte[0] = ISO_ESC;
       
   132                     tmpByte = SS3Designator.getBytes();
       
   133                     System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
       
   134                     index = tmpByte.length+1;
       
   135                 }
       
   136                 ebyte[index++] = ISO_ESC;
       
   137                 ebyte[index++] = ISO_SS3_7;
       
   138                 ebyte[index++] = (byte)(convByte[2]&0x7f);
       
   139                 ebyte[index++] = (byte)(convByte[3]&0x7f);
       
   140             }
       
   141         }
       
   142         return index;
       
   143     }
       
   144 
       
   145     /**
       
   146      * Character conversion
       
   147      */
       
   148     public int convert(char[] input, int inOff, int inEnd,
       
   149                        byte[] output, int outOff, int outEnd)
       
   150         throws UnknownCharacterException, MalformedInputException,
       
   151                ConversionBufferFullException
       
   152     {
       
   153         int outputSize;
       
   154         byte [] tmpbuf = new byte[this.getMaxBytesPerChar()];
       
   155         byte [] outputByte;
       
   156 
       
   157         byteOff = outOff;
       
   158 
       
   159         newshiftout = shiftout;
       
   160         newSODesDefined = SODesDefined;
       
   161         newSS2DesDefined = SS2DesDefined;
       
   162         newSS3DesDefined = SS3DesDefined;
       
   163 
       
   164         //Fixed 4122961 by bringing the charOff++ out to this
       
   165         // loop where it belongs, changing the loop from
       
   166         // while(){} to for(){}.
       
   167         for (charOff = inOff; charOff < inEnd; charOff++) {
       
   168             outputByte = tmpbuf;
       
   169             if (input[charOff] < 0x80) {        // ASCII
       
   170                 if (shiftout){
       
   171                     newshiftout = false;
       
   172                     outputSize = 2;
       
   173                     outputByte[0] = ISO_SI;
       
   174                     outputByte[1] = (byte)(input[charOff] & 0x7f);
       
   175                 } else {
       
   176                     outputSize = 1;
       
   177                     outputByte[0] = (byte)(input[charOff] & 0x7f);
       
   178                 }
       
   179                 if(input[charOff] == '\n'){
       
   180                     newSODesDefined = false;
       
   181                     newSS2DesDefined = false;
       
   182                     newSS3DesDefined = false;
       
   183                 }
       
   184             } else {
       
   185                 outputSize = unicodeToNative(input[charOff], outputByte);
       
   186             }
       
   187 
       
   188             if (outputSize == -1) {
       
   189                 if (subMode) {
       
   190                     if(!newSODesDefined){
       
   191                         newSODesDefined = !newSODesDefined;
       
   192                         outputByte[0] = ISO_SO;
       
   193                         outputByte[1] = (byte)'?';
       
   194                         outputSize = 2;
       
   195                     } else {
       
   196                         outputByte = subBytes;
       
   197                         outputSize = subBytes.length;
       
   198                     }
       
   199                 } else {
       
   200                     badInputLength = 1;
       
   201                     throw new UnknownCharacterException();
       
   202                 }
       
   203             }
       
   204 
       
   205             if (outEnd - byteOff < outputSize)
       
   206                 throw new ConversionBufferFullException();
       
   207 
       
   208             for (int i = 0; i < outputSize; i++)
       
   209                 output[byteOff++] = outputByte[i];
       
   210 
       
   211             // Bug 4266772. Update statuses only when output buffer has been
       
   212             // updated. When ConversionBufferFullException() has been throwed
       
   213             // we want to keep old statuses for next call.
       
   214             shiftout = newshiftout;
       
   215             SODesDefined = newSODesDefined;
       
   216             SS2DesDefined = newSS2DesDefined;
       
   217             SS3DesDefined = newSS3DesDefined;
       
   218         }
       
   219         return byteOff - outOff;
       
   220     }
       
   221 }