/*
* 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
* 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
//
/**
* 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
*/
// Character code descriptors. These can be used with the Java
// character encoding utilities. For Unicode, we use little on
// input,
// Error code for misidentified character set.
// Character codes.
// 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.
*/
throws ServiceLocationException {
return CHAR_ASCII;
return CHAR_LATIN1;
return CHAR_UTF8;
return CHAR_UNICODE;
return CHAR_UNICODE;
return CHAR_UNICODE;
return CHAR_UNICODE;
}
throw
"v1_unsupported_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.
*/
throws ServiceLocationException {
switch (code) {
case CHAR_ASCII: return ASCII;
case CHAR_LATIN1: return LATIN1;
case CHAR_UNICODE: return UNICODE;
}
throw
"v1_unsupported_encoding",
}
/**
* 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.
*/
throws ServiceLocationException {
try {
} catch (UnsupportedEncodingException ex) {
throw
"v1_unsupported_encoding",
} catch (IOException ex) {
}
byte b[] = baos.toByteArray();
int code = 0;
// Assemble the character code based on the encoding type.
if (b.length <= 4) {
throw
"v1_charcode_error",
}
if (b.length > 1) {
throw
"v1_charcode_error",
}
if (b.length > 3) {
throw
"v1_charcode_error",
}
if (b.length > 1) {
}
if (b.length > 2) {
}
}
}
/**
* 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.
*/
throws ServiceLocationException {
int code = 0;
try {
} catch (NumberFormatException ex) {
throw
"v1_stringcode_error",
}
// 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.
byte b[] = null;
// We create an array sized to the encoding.
b = new byte[4];
b[0] = b0;
b[1] = b1;
b[2] = b2;
b[3] = b3;
// single byte
b = new byte[1];
b[0] = b0;
throw
"v1_stringcode_error",
}
if (b3 != 0) {
throw
"v1_stringcode_error",
}
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 {
} catch (UnsupportedEncodingException ex) {
"v1_unsupported_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.
return UNICODE_LITTLE;
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.
return flaggedBytes;
}
}