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