jdk/src/share/classes/sun/io/CharToByteASCII.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 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
public class CharToByteASCII extends CharToByteConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
    // Return the character set ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    public String getCharacterEncoding()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
        return "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private char highHalfZoneCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public int flush(byte[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (highHalfZoneCode != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            throw new MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                ("String ends with <High Half Zone code> of UTF16");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    * Character conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public int convert(char[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                       byte[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        throws MalformedInputException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
               UnknownCharacterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        char    inputChar;          // Input character to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        byte[]  outputByte;         // Output byte written to output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        byte[]  tmpArray = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int     inputSize;          // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        int     outputSize;         // Size of output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // Record beginning offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        charOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        byteOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (highHalfZoneCode != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            inputChar = highHalfZoneCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (input[inOff] >= 0xdc00 && input[inOff] <= 0xdfff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                // This is legal UTF16 sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // This is illegal UTF16 sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                throw new MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    ("Previous converted string ends with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                     "<High Half Zone Code> of UTF16 " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                     ", but this string is not begin with <Low Half Zone>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Loop until we hit the end of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        while(charOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            outputByte = tmpArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // Get the input character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            inputChar = input[charOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // default outputSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            outputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // Assume this is a simple character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // Is this a high surrogate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if(inputChar >= '\uD800' && inputChar <= '\uDBFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                // Is this the last character in the input?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (charOff + 1 == inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    highHalfZoneCode = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                // Is there a low surrogate following?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                inputChar = input[charOff + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    // We have a valid surrogate pair.  Too bad we don't map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    //  surrogates.  Is substitution enabled?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        outputByte = subBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        outputSize = subBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        inputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    // We have a malformed surrogate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // Is this an unaccompanied low surrogate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            else if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Not part of a surrogate, so try to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // Is this character mappable?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (inputChar <= '\u007F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    outputByte[0] = (byte)inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    // Is substitution enabled?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        outputByte = subBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        outputSize = subBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // If we don't have room for the output, throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (byteOff + outputSize > outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // Put the byte in the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            for (int i = 0; i < outputSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                output[byteOff++] = outputByte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            charOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // Return the length written to the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return byteOff-outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // Determine if a character is mappable or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public boolean canConvert(char ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return (ch <= '\u007F');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // Reset the converter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * returns the maximum number of bytes needed to convert a char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public int getMaxBytesPerChar()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}