package com.sun.slp;
import java.util.*;
import java.io.*;
abstract class IANACharCode extends Object {
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";
static final short CHARSET_NOT_UNDERSTOOD = 5;
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;
protected static final byte[] UNICODE_LITTLE_FLAG =
{(byte)0xFF, (byte)0xFE};
protected static final byte[] UNICODE_BIG_FLAG =
{(byte)0xFE, (byte)0xFF};
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});
}
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)});
}
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;
if (encoding.equals(UNICODE) ||
encoding.equals(UNICODE_BIG) ||
encoding.equals(UNICODE_LITTLE)) {
code = (int)(b[0] & 0xFF);
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[] {Character.valueOf(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[] {Character.valueOf(c), encoding});
}
} else if (encoding.equals(UTF8)) {
if (b.length > 3) {
throw
new ServiceLocationException(
ServiceLocationException.PARSE_ERROR,
"v1_charcode_error",
new Object[] {Character.valueOf(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);
}
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});
}
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);
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)) {
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)) {
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;
}
}
try {
str = new String(b, encoding);
} catch (UnsupportedEncodingException ex) {
Assert.slpassert(false,
"v1_unsupported_encoding",
new Object[] {encoding});
}
return str;
}
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;
}
}
return UNICODE;
}
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;
}
}