1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*
*/
// IANACharCode.java: SLPv1 Character encoding support
// Author: James Kempf
// Created On: Fri Sep 11 13:24:02 1998
// Last Modified By: James Kempf
// Last Modified On: Wed Oct 28 14:33:02 1998
// Update Count: 7
//
package com.sun.slp;
import java.util.*;
import java.io.*;
/**
* The IANACharCode class supports static methods for decoding IANA
* character codes into strings appropriate for the Java Writer subclass
* encoding String arguments, and for encoding the String descriptions
* of character codings into the integer codes. Ideally, Java itself
* should support this.
*
* @author James Kempf
*/
abstract class IANACharCode extends Object {
// Character code descriptors. These can be used with the Java
// character encoding utilities. For Unicode, we use little on
// input,
static final String ASCII = "Default";
static final String LATIN1 = "latin1";
static final String UTF8 = "UTF8";
static final String UNICODE = "Unicode";
static final String UNICODE_LITTLE = "UnicodeLittle";
static final String UNICODE_BIG = "UnicodeBig";
static final String UNICODE_BIG_NO_HDR = "UnicodeBigNoHdr";
// Error code for misidentified character set.
static final short CHARSET_NOT_UNDERSTOOD = 5;
// Character codes.
protected static final int CHAR_ASCII = 3;
protected static final int CHAR_LATIN1 = 4;
protected static final int CHAR_UTF8 = 6;
protected static final int CHAR_UNICODE = 1000;
// First two bytes indicate that string is big/little endian Unicode.
// If this flag isn't set, then big endian is assumed and we
// must add the big endian bytes on every call.
protected static final byte[] UNICODE_LITTLE_FLAG =
{(byte)0xFF, (byte)0xFE};
protected static final byte[] UNICODE_BIG_FLAG =
{(byte)0xFE, (byte)0xFF};
/**
* Encode the String describing a character encoding into
* the approprate integer descriptor code.
*
* @param encoding The String describing the encoding.
* @exception ServiceLocationCharSetNotUnderstoodException Thrown if the
* String is not recognized.
*/
static int encodeCharacterEncoding(String encoding)
throws ServiceLocationException {
if (encoding.equals(ASCII)) {
return CHAR_ASCII;
} else if (encoding.equals(LATIN1)) {
return CHAR_LATIN1;
} else if (encoding.equals(UTF8)) {
return CHAR_UTF8;
} else if (encoding.equals(UNICODE)) {
return CHAR_UNICODE;
} else if (encoding.equals(UNICODE_BIG)) {
return CHAR_UNICODE;
} else if (encoding.equals(UNICODE_LITTLE)) {
return CHAR_UNICODE;
} else if (encoding.equals(UNICODE_BIG_NO_HDR)) {
return CHAR_UNICODE;
}
throw
new ServiceLocationException(
CHARSET_NOT_UNDERSTOOD,
"v1_unsupported_encoding",
new Object[] {encoding});
}
/**
* Decode the integer describing a character encoding into
* the approprate String descriptor.
*
* @param code The integer coding the String set.
* @exception ServiceLocationCharSetNotUnderstoodException Thrown if the
* integer is not recognized.
*/
static String decodeCharacterEncoding(int code)
throws ServiceLocationException {
switch (code) {
case CHAR_ASCII: return ASCII;
case CHAR_LATIN1: return LATIN1;
case CHAR_UTF8: return UTF8;
case CHAR_UNICODE: return UNICODE;
}
throw
new ServiceLocationException(
CHARSET_NOT_UNDERSTOOD,
"v1_unsupported_encoding",
new Object[] {Integer.toString(code)});
}
/**
* Return a string of integers giving the character's encoding in
* the character set passed in as encoding.
*
* @param c The character to escape.
* @param encoding The character set encoding to use.
* @return The character as a string of integers for the encoding.
* @exception ServiceLocationException Thrown if the encoding is not
* recognized, if the character's encoding
* has more than 8 bytes or if the sign bit gets turned on.
*/
static String escapeChar(char c, String encoding)
throws ServiceLocationException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
OutputStreamWriter osw = new OutputStreamWriter(baos, encoding);
osw.write(c);
osw.flush();
} catch (UnsupportedEncodingException ex) {
throw
new ServiceLocationException(
CHARSET_NOT_UNDERSTOOD,
"v1_unsupported_encoding",
new Object[] {encoding});
} catch (IOException ex) {
}
byte b[] = baos.toByteArray();
int code = 0;
// Assemble the character code based on the encoding type.
if (encoding.equals(UNICODE) ||
encoding.equals(UNICODE_BIG) ||
encoding.equals(UNICODE_LITTLE)) {
code = (int)(b[0] & 0xFF); // control bytes...
code = (int)(code | ((b[1] & 0xFF) << 8));
code = (int)(code | ((b[2] & 0xFF) << 16));
code = (int)(code | ((b[3] & 0xFF) << 24));
if (b.length <= 4) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_charcode_error",
new Object[] {new Character(c), encoding});
}
} else if (encoding.equals(ASCII) || encoding.equals(LATIN1)) {
code = (int)(b[0] & 0xFF);
if (b.length > 1) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_charcode_error",
new Object[] {new Character(c), encoding});
}
} else if (encoding.equals(UTF8)) {
if (b.length > 3) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_charcode_error",
new Object[] {new Character(c), encoding});
}
code = (int)(b[0] & 0xFF);
if (b.length > 1) {
code = (int)(code | ((b[1] & 0xFF) << 8));
}
if (b.length > 2) {
code = (int)(code | ((b[2] & 0xFF) << 16));
}
}
return Integer.toString(code);
}
/**
* Unescape the character encoded as the string.
*
* @param ch The character as a string of Integers.
* @param encoding The character set encoding to use.
* @return The character.
* @exception ServiceLocationException Thrown if the string can't
* be parsed into an integer or if the encoding isn't
* recognized.
*/
static String unescapeChar(String ch, String encoding)
throws ServiceLocationException {
int code = 0;
try {
code = Integer.parseInt(ch);
} catch (NumberFormatException ex) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_stringcode_error",
new Object[] {ch, encoding});
}
// Convert to bytes. We need to taylor the array size to the
// number of bytes because otherwise, in encodings that
// take less bytes, the resulting string will have garbage
// in it.
String str = null;
byte b0 = 0, b1 = 0, b2 = 0, b3 = 0;
byte b[] = null;
b0 = (byte) (code & 0xFF);
b1 = (byte) ((code >> 8) & 0xFF);
b2 = (byte) ((code >> 16) & 0xFF);
b3 = (byte) ((code >> 24) & 0xFf);
// We create an array sized to the encoding.
if (encoding.equals(UNICODE_BIG) ||
encoding.equals(UNICODE_LITTLE)) {
b = new byte[4];
b[0] = b0;
b[1] = b1;
b[2] = b2;
b[3] = b3;
} else if (encoding.equals(LATIN1) || encoding.equals(ASCII)) {
// single byte
b = new byte[1];
b[0] = b0;
if (b1 != 0 || b2 != 0) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_stringcode_error",
new Object[] {ch, encoding});
}
} else if (encoding.equals(UTF8)) {// vari-byte
if (b3 != 0) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_stringcode_error",
new Object[] {ch, encoding});
}
if (b2 != 0) {
b = new byte[3];
b[2] = b2;
b[1] = b1;
b[0] = b0;
} else if (b1 != 0) {
b = new byte[2];
b[1] = b1;
b[0] = b0;
} else {
b = new byte[1];
b[0] = b0;
}
}
// Make a string out of it.
try {
str = new String(b, encoding);
} catch (UnsupportedEncodingException ex) {
Assert.slpassert(false,
"v1_unsupported_encoding",
new Object[] {encoding});
}
return str;
}
// Determine from the flag bytes whether this is big or little endian
// Unicode. If there are no flag bytes, then just return UNICODE.
static String getUnicodeEndianess(byte[] bytes) {
if (bytes.length >= 2) {
if (bytes[0] == UNICODE_LITTLE_FLAG[0] &&
bytes[1] == UNICODE_LITTLE_FLAG[1]) {
return UNICODE_LITTLE;
} else if (bytes[0] == UNICODE_BIG_FLAG[0] &&
bytes[1] == UNICODE_BIG_FLAG[1]) {
return UNICODE_BIG;
}
}
// We can`t tell from the byte header, so it's big endian. But
// since we need to add the byte header, we say we don't know.
return UNICODE;
}
// Add the big endian flag to a Unicode string.
static byte[] addBigEndianFlag(byte[] bytes) {
byte[] flaggedBytes = new byte[bytes.length + 2];
flaggedBytes[0] = UNICODE_BIG_FLAG[0];
flaggedBytes[1] = UNICODE_BIG_FLAG[1];
System.arraycopy(flaggedBytes, 2, bytes, 0, bytes.length);
return flaggedBytes;
}
}
|