jdk/src/share/classes/sun/io/ByteToCharDBCS_EBCDIC.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2921 d9d491a5a169
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 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
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
public abstract class ByteToCharDBCS_EBCDIC extends ByteToCharConverter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    private static final int SBCS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    private static final int DBCS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private static final int SO = 0x0e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static final int SI = 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private int  currentState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private boolean savedBytePresent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private byte savedByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected String singleByteToChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    protected short index1[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected String index2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected int   mask1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected int   mask2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    protected int   shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public ByteToCharDBCS_EBCDIC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
       super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
       currentState = SBCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
       savedBytePresent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public int flush(char [] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
       throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
       if (savedBytePresent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
           reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
           throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
       reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
       return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public int convert(byte[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                       char[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        throws UnknownCharacterException, MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
       int  inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
       char outputChar = '\uFFFD';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       charOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
       byteOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
       while(byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
          int byte1, byte2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
          int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
          if (!savedBytePresent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            byte1 = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            byte1 = savedByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            savedBytePresent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
          if (byte1 == SO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
             // For SO characters - simply validate the state and if OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
             //    update the state and go to the next byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
             if (currentState != SBCS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                currentState = DBCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             if (byte1 == SI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // For SI characters - simply validate the state and if OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                //    update the state and go to the next byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (currentState != DBCS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                   badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                   throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                   currentState = SBCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                   byteOff+= inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // Process the real data characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (byte1 < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                   byte1 += 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (currentState == SBCS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                   outputChar = singleByteToChar.charAt(byte1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                   // for a DBCS character - architecture dictates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                   // valid range of 1st bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                   if (byte1 < 0x40 || byte1 > 0xfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                      badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                      throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                   if (byteOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                      // We have been split in the middle if a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                      // save the first byte for next time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                      savedByte = (byte)byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                      savedBytePresent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                      byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                   byte2 = input[byteOff+inputSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                   if (byte2 < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                      byte2 += 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                   inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                   // validate the pair of bytes meet the architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                   if ((byte1 != 0x40 || byte2 != 0x40) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                      (byte2 < 0x41 || byte2 > 0xfe)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                      badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                      throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                   // Lookup in the two level index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                   v = byte1 * 256 + byte2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                   outputChar = index2.charAt(index1[((v & mask1) >> shift)] + (v & mask2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (outputChar == '\uFFFD') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                   if (subMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                      outputChar = subChars[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                   else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                      badInputLength = inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                      throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (charOff >= outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                   throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                output[charOff++] = outputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
       return charOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *  Resets the converter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
       charOff = byteOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
       currentState = SBCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
       savedBytePresent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}