jdk/src/share/classes/sun/io/CharToByteISO2022.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2000 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
* @author Tom Zhou
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public abstract class CharToByteISO2022 extends CharToByteConverter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private final byte ISO_ESC = 0x1b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private final byte ISO_SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final byte ISO_SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private final byte ISO_SS2_7 = 0x4e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private final byte ISO_SS3_7 = 0x4f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private final byte SS2 = (byte)0x8e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final byte P2 = (byte)0xA2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final byte P3 = (byte)0xA3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private final byte MSB = (byte)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected final byte maximumDesignatorLength = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected String SODesignator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                     SS2Designator = null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                     SS3Designator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected CharToByteConverter codeConverter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private boolean SODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean SS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean SS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int flush(byte[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        shiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        SODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        SS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public boolean canConvert(char ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (ch<0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
           return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return codeConverter.canConvert(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private int unicodeToNative(char unicode, byte ebyte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int     index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        byte    tmpByte[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        byte    convByte[] = new byte[codeConverter.getMaxBytesPerChar()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        char    convChar[] = {unicode};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int     converted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            converted = codeConverter.convert(convChar, 0, 1, convByte, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                        codeConverter.getMaxBytesPerChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (converted == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (!SODesDefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                newSODesDefined = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                ebyte[0] = ISO_ESC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                tmpByte = SODesignator.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                index = tmpByte.length+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (!shiftout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                newshiftout = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                ebyte[index++] = ISO_SO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            ebyte[index++] = (byte)(convByte[0]&0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ebyte[index++] = (byte)(convByte[1]&0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if((convByte[0] == SS2)&&(convByte[1] == P2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (!SS2DesDefined) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    newSS2DesDefined = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    ebyte[0] = ISO_ESC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    tmpByte = SS2Designator.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    index = tmpByte.length+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                ebyte[index++] = ISO_ESC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                ebyte[index++] = ISO_SS2_7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ebyte[index++] = (byte)(convByte[2]&0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                ebyte[index++] = (byte)(convByte[3]&0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if((convByte[0] == SS2)&&(convByte[1] == 0xA3))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if(!SS3DesDefined){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    newSS3DesDefined = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    ebyte[0] = ISO_ESC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    tmpByte = SS3Designator.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    index = tmpByte.length+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ebyte[index++] = ISO_ESC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                ebyte[index++] = ISO_SS3_7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                ebyte[index++] = (byte)(convByte[2]&0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                ebyte[index++] = (byte)(convByte[3]&0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public int convert(char[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                       byte[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throws UnknownCharacterException, MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int outputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        byte [] tmpbuf = new byte[this.getMaxBytesPerChar()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        byte [] outputByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        byteOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        newshiftout = shiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        newSODesDefined = SODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        newSS2DesDefined = SS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        newSS3DesDefined = SS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        //Fixed 4122961 by bringing the charOff++ out to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // loop where it belongs, changing the loop from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // while(){} to for(){}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        for (charOff = inOff; charOff < inEnd; charOff++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            outputByte = tmpbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (input[charOff] < 0x80) {        // ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (shiftout){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    newshiftout = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    outputByte[0] = ISO_SI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    outputByte[1] = (byte)(input[charOff] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    outputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    outputByte[0] = (byte)(input[charOff] & 0x7f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if(input[charOff] == '\n'){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    newSODesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    newSS2DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    newSS3DesDefined = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                outputSize = unicodeToNative(input[charOff], outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (outputSize == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    if(!newSODesDefined){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        newSODesDefined = !newSODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        outputByte[0] = ISO_SO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        outputByte[1] = (byte)'?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        outputByte = subBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        outputSize = subBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (outEnd - byteOff < outputSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            for (int i = 0; i < outputSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                output[byteOff++] = outputByte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // Bug 4266772. Update statuses only when output buffer has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // updated. When ConversionBufferFullException() has been throwed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // we want to keep old statuses for next call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            shiftout = newshiftout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            SODesDefined = newSODesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            SS2DesDefined = newSS2DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            SS3DesDefined = newSS3DesDefined;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return byteOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}