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