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