blob: dadae731d7b047dcd573d553a613a5dd837c43fb [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2002 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
26package sun.io;
27
28/**
29 * @author Limin Shi
30 */
31
32public abstract class CharToByteDoubleByte extends CharToByteConverter {
33
34 /*
35 * 1st level index, provided by subclass
36 */
37 protected short index1[];
38
39 /*
40 * 2nd level index, provided by subclass
41 */
42 protected String index2[];
43
44 protected char highHalfZoneCode;
45
46 public short[] getIndex1() {
47 return index1;
48 }
49
50 public String[] getIndex2() {
51 return index2;
52 }
53
54 public int flush(byte[] output, int outStart, int outEnd)
55 throws MalformedInputException, ConversionBufferFullException
56 {
57 if (highHalfZoneCode != 0) {
58 highHalfZoneCode = 0;
59 badInputLength = 0;
60 throw new MalformedInputException();
61 }
62 byteOff = charOff = 0;
63 return 0;
64 }
65
66 /**
67 * Converts characters to sequences of bytes.
68 * Conversions that result in Exceptions can be restarted by calling
69 * convert again, with appropriately modified parameters.
70 * @return the characters written to output.
71 * @param input char array containing text in Unicode
72 * @param inStart offset in input array
73 * @param inEnd offset of last byte to be converted
74 * @param output byte array to receive conversion result
75 * @param outStart starting offset
76 * @param outEnd offset of last byte to be written to
77 * @throw UnsupportedCharacterException for any character
78 * that cannot be converted to the external character set.
79 */
80 public int convert(char[] input, int inOff, int inEnd,
81 byte[] output, int outOff, int outEnd)
82 throws MalformedInputException, UnknownCharacterException,
83 ConversionBufferFullException
84 {
85 char inputChar; // Input character to be converted
86 byte[] outputByte; // Output byte written to output
87 int inputSize = 0; // Size of input
88 int outputSize = 0; // Size of output
89 byte[] tmpbuf = new byte[2];
90
91 // Record beginning offsets
92 charOff = inOff;
93 byteOff = outOff;
94
95 if (highHalfZoneCode != 0) {
96 inputChar = highHalfZoneCode;
97 highHalfZoneCode = 0;
98 if (input[inOff] >= 0xdc00 && input[inOff] <= 0xdfff) {
99 // This is legal UTF16 sequence.
100 badInputLength = 1;
101 throw new UnknownCharacterException();
102 } else {
103 // This is illegal UTF16 sequence.
104 badInputLength = 0;
105 throw new MalformedInputException();
106 }
107 }
108
109 // Loop until we hit the end of the input
110 while(charOff < inEnd) {
111 inputSize = 1;
112 outputByte = tmpbuf;
113 inputChar = input[charOff]; // Get the input character
114
115 // Is this a high surrogate?
116 if(inputChar >= '\uD800' && inputChar <= '\uDBFF') {
117 // Is this the last character of the input?
118 if (charOff + 1 >= inEnd) {
119 highHalfZoneCode = inputChar;
120 break;
121 }
122
123 // Is there a low surrogate following?
124 inputChar = input[charOff + 1];
125 if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
126 // We have a valid surrogate pair. Too bad we don't do
127 // surrogates. Is substitution enabled?
128 if (subMode) {
129 outputByte = subBytes;
130 outputSize = subBytes.length;
131 inputSize = 2;
132 } else {
133 badInputLength = 2;
134 throw new UnknownCharacterException();
135 }
136 } else {
137 // We have a malformed surrogate pair
138 badInputLength = 1;
139 throw new MalformedInputException();
140 }
141 }
142 // Is this an unaccompanied low surrogate?
143 else if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
144 badInputLength = 1;
145 throw new MalformedInputException();
146 } else {
147 outputSize = convSingleByte(inputChar, outputByte);
148 if (outputSize == 0) { // DoubleByte
149 int ncode = getNative(inputChar);
150 if (ncode != 0 ) {
151 outputByte[0] = (byte) ((ncode & 0xff00) >> 8);
152 outputByte[1] = (byte) (ncode & 0xff);
153 outputSize = 2;
154 } else {
155 if (subMode) {
156 outputByte = subBytes;
157 outputSize = subBytes.length;
158 } else {
159 badInputLength = 1;
160 throw new UnknownCharacterException();
161 }
162 }
163 }
164 }
165
166 // If we don't have room for the output, throw an exception
167 if (byteOff + outputSize > outEnd)
168 throw new ConversionBufferFullException();
169
170 // Put the byte in the output buffer
171 for (int i = 0; i < outputSize; i++) {
172 output[byteOff++] = outputByte[i];
173 }
174 charOff += inputSize;
175 }
176 // Return the length written to the output buffer
177 return byteOff - outOff;
178 }
179
180
181 /**
182 * the maximum number of bytes needed to hold a converted char
183 * @returns the maximum number of bytes needed for a converted char
184 */
185 public int getMaxBytesPerChar() {
186 return 2;
187 }
188
189 /**
190 * Resets the converter.
191 * Call this method to reset the converter to its initial state
192 */
193 public void reset() {
194 byteOff = charOff = 0;
195 highHalfZoneCode = 0;
196 }
197
198 /**
199 * Return whether a character is mappable or not
200 * @return true if a character is mappable
201 */
202 public boolean canConvert(char ch) {
203 byte[] outByte = new byte[2];
204
205 if ((ch == (char) 0) || (convSingleByte(ch, outByte) != 0))
206 return true;
207 if (this.getNative(ch) != 0)
208 return true;
209 return false;
210 }
211
212
213 /*
214 * Can be changed by subclass
215 */
216 protected int convSingleByte(char inputChar, byte[] outputByte) {
217 if (inputChar < 0x80) {
218 outputByte[0] = (byte)(inputChar & 0x7f);
219 return 1;
220 }
221 return 0;
222 }
223
224 /*
225 * Can be changed by subclass
226 */
227 protected int getNative(char ch) {
228 int offset = index1[((ch & 0xff00) >> 8 )] << 8;
229 return index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
230 }
231
232}