1 /* |
|
2 * Copyright (c) 2003, 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 |
|
26 package sun.io; |
|
27 |
|
28 import sun.nio.cs.ext.JIS_X_0208_Solaris_Encoder; |
|
29 import sun.nio.cs.ext.JIS_X_0212_Solaris_Encoder; |
|
30 |
|
31 /** |
|
32 * @author Limin Shi |
|
33 * @author Ian Little |
|
34 * |
|
35 * EUC_JP variant converter for Solaris with vendor defined chars |
|
36 * added (4765370) |
|
37 */ |
|
38 |
|
39 public class CharToByteEUC_JP_Solaris extends CharToByteEUC_JP { |
|
40 CharToByteJIS0201 cbJIS0201 = new CharToByteJIS0201(); |
|
41 CharToByteJIS0212_Solaris cbJIS0212 = new CharToByteJIS0212_Solaris(); |
|
42 |
|
43 short[] j0208Index1 = JIS_X_0208_Solaris_Encoder.getIndex1(); |
|
44 String[] j0208Index2 = JIS_X_0208_Solaris_Encoder.getIndex2(); |
|
45 |
|
46 public String getCharacterEncoding() { |
|
47 return "eucJP-open"; |
|
48 } |
|
49 |
|
50 protected int convSingleByte(char inputChar, byte[] outputByte) { |
|
51 byte b; |
|
52 |
|
53 if (inputChar == 0) { |
|
54 outputByte[0] = (byte)0; |
|
55 return 1; |
|
56 } |
|
57 |
|
58 if ((b = cbJIS0201.getNative(inputChar)) == 0) |
|
59 return 0; |
|
60 |
|
61 if (b > 0 && b < 128) { |
|
62 outputByte[0] = b; |
|
63 return 1; |
|
64 } |
|
65 outputByte[0] = (byte)0x8E; |
|
66 outputByte[1] = b; |
|
67 return 2; |
|
68 } |
|
69 |
|
70 protected int getNative(char ch) { |
|
71 int r = super.getNative(ch); |
|
72 if (r != 0) { |
|
73 return r; |
|
74 } else { |
|
75 int offset = j0208Index1[((ch & 0xff00) >> 8 )] << 8; |
|
76 r = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff)); |
|
77 if (r > 0x7500) |
|
78 return 0x8f8080 + cbJIS0212.getNative(ch); |
|
79 } |
|
80 return (r == 0)? r : r + 0x8080; |
|
81 } |
|
82 |
|
83 |
|
84 /** |
|
85 * Converts characters to sequences of bytes. |
|
86 * Conversions that result in Exceptions can be restarted by calling |
|
87 * convert again, with appropriately modified parameters. |
|
88 * @return the characters written to output. |
|
89 * @param input char array containing text in Unicode |
|
90 * @param inStart offset in input array |
|
91 * @param inEnd offset of last byte to be converted |
|
92 * @param output byte array to receive conversion result |
|
93 * @param outStart starting offset |
|
94 * @param outEnd offset of last byte to be written to |
|
95 * @throw UnsupportedCharacterException for any character |
|
96 * that cannot be converted to the external character set. |
|
97 */ |
|
98 public int convert(char[] input, int inOff, int inEnd, |
|
99 byte[] output, int outOff, int outEnd) |
|
100 throws MalformedInputException, UnknownCharacterException, |
|
101 ConversionBufferFullException |
|
102 { |
|
103 char inputChar; // Input character to be converted |
|
104 byte[] outputByte; // Output byte written to output |
|
105 int inputSize = 0; // Size of input |
|
106 int outputSize = 0; // Size of output |
|
107 byte[] tmpbuf = new byte[4]; |
|
108 |
|
109 // Record beginning offsets |
|
110 charOff = inOff; |
|
111 byteOff = outOff; |
|
112 |
|
113 if (highHalfZoneCode != 0) { |
|
114 inputChar = highHalfZoneCode; |
|
115 highHalfZoneCode = 0; |
|
116 if (input[inOff] >= 0xdc00 && input[inOff] <= 0xdfff) { |
|
117 // This is legal UTF16 sequence. |
|
118 badInputLength = 1; |
|
119 throw new UnknownCharacterException(); |
|
120 } else { |
|
121 // This is illegal UTF16 sequence. |
|
122 badInputLength = 0; |
|
123 throw new MalformedInputException(); |
|
124 } |
|
125 } |
|
126 |
|
127 // Loop until we hit the end of the input |
|
128 while(charOff < inEnd) { |
|
129 inputSize = 1; |
|
130 outputByte = tmpbuf; |
|
131 inputChar = input[charOff]; // Get the input character |
|
132 |
|
133 // Is this a high surrogate? |
|
134 if(inputChar >= '\uD800' && inputChar <= '\uDBFF') { |
|
135 // Is this the last character of the input? |
|
136 if (charOff + 1 >= inEnd) { |
|
137 highHalfZoneCode = inputChar; |
|
138 break; |
|
139 } |
|
140 |
|
141 // Is there a low surrogate following? |
|
142 inputChar = input[charOff + 1]; |
|
143 if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') { |
|
144 // We have a valid surrogate pair. Too bad we don't do |
|
145 // surrogates. Is substitution enabled? |
|
146 if (subMode) { |
|
147 outputByte = subBytes; |
|
148 outputSize = subBytes.length; |
|
149 inputSize = 2; |
|
150 } else { |
|
151 badInputLength = 2; |
|
152 throw new UnknownCharacterException(); |
|
153 } |
|
154 } else { |
|
155 // We have a malformed surrogate pair |
|
156 badInputLength = 1; |
|
157 throw new MalformedInputException(); |
|
158 } |
|
159 } |
|
160 // Is this an unaccompanied low surrogate? |
|
161 else if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') { |
|
162 badInputLength = 1; |
|
163 throw new MalformedInputException(); |
|
164 } else { |
|
165 outputSize = convSingleByte(inputChar, outputByte); |
|
166 if (outputSize == 0) { // DoubleByte |
|
167 int ncode = getNative(inputChar); |
|
168 if (ncode != 0 ) { |
|
169 if ((ncode & 0xFF0000) == 0) { |
|
170 outputByte[0] = (byte) ((ncode & 0xff00) >> 8); |
|
171 outputByte[1] = (byte) (ncode & 0xff); |
|
172 outputSize = 2; |
|
173 } else { |
|
174 outputByte[0] = (byte) 0x8F; |
|
175 outputByte[1] = (byte) ((ncode & 0xff00) >> 8); |
|
176 outputByte[2] = (byte) (ncode & 0xff); |
|
177 outputSize = 3; |
|
178 } |
|
179 } else { |
|
180 if (subMode) { |
|
181 outputByte = subBytes; |
|
182 outputSize = subBytes.length; |
|
183 } else { |
|
184 badInputLength = 1; |
|
185 throw new UnknownCharacterException(); |
|
186 } |
|
187 } |
|
188 } |
|
189 } |
|
190 |
|
191 // If we don't have room for the output, throw an exception |
|
192 if (byteOff + outputSize > outEnd) |
|
193 throw new ConversionBufferFullException(); |
|
194 |
|
195 // Put the byte in the output buffer |
|
196 for (int i = 0; i < outputSize; i++) { |
|
197 output[byteOff++] = outputByte[i]; |
|
198 } |
|
199 charOff += inputSize; |
|
200 } |
|
201 // Return the length written to the output buffer |
|
202 return byteOff - outOff; |
|
203 } |
|
204 |
|
205 |
|
206 /** |
|
207 * the maximum number of bytes needed to hold a converted char |
|
208 * @returns the maximum number of bytes needed for a converted char |
|
209 */ |
|
210 public int getMaxBytesPerChar() { |
|
211 return 3; |
|
212 } |
|
213 } |
|