17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59a70fc3bSMark J. Nelson  * Common Development and Distribution License (the "License").
69a70fc3bSMark J. Nelson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate //  IANACharCode.java: SLPv1 Character encoding support
287c478bd9Sstevel@tonic-gate //  Author:           James Kempf
297c478bd9Sstevel@tonic-gate //  Created On:       Fri Sep 11 13:24:02 1998
307c478bd9Sstevel@tonic-gate //  Last Modified By: James Kempf
317c478bd9Sstevel@tonic-gate //  Last Modified On: Wed Oct 28 14:33:02 1998
327c478bd9Sstevel@tonic-gate //  Update Count:     7
337c478bd9Sstevel@tonic-gate //
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate package com.sun.slp;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate import java.util.*;
397c478bd9Sstevel@tonic-gate import java.io.*;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /**
427c478bd9Sstevel@tonic-gate  * The IANACharCode class supports static methods for decoding IANA
437c478bd9Sstevel@tonic-gate  * character codes into strings appropriate for the Java Writer subclass
447c478bd9Sstevel@tonic-gate  * encoding String arguments, and for encoding the String descriptions
457c478bd9Sstevel@tonic-gate  * of character codings into the integer codes. Ideally, Java itself
467c478bd9Sstevel@tonic-gate  * should support this.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * @author James Kempf
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate abstract class IANACharCode extends Object {
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate     // Character code descriptors. These can be used with the Java
547c478bd9Sstevel@tonic-gate     //  character encoding utilities. For Unicode, we use little on
557c478bd9Sstevel@tonic-gate     //  input,
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate     static final String ASCII = "Default";
587c478bd9Sstevel@tonic-gate     static final String LATIN1 = "latin1";
597c478bd9Sstevel@tonic-gate     static final String UTF8 = "UTF8";
607c478bd9Sstevel@tonic-gate     static final String UNICODE = "Unicode";
617c478bd9Sstevel@tonic-gate     static final String UNICODE_LITTLE = "UnicodeLittle";
627c478bd9Sstevel@tonic-gate     static final String UNICODE_BIG = "UnicodeBig";
637c478bd9Sstevel@tonic-gate     static final String UNICODE_BIG_NO_HDR = "UnicodeBigNoHdr";
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate     // Error code for misidentified character set.
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate     static final short CHARSET_NOT_UNDERSTOOD = 5;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate     // Character codes.
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate     protected static final int CHAR_ASCII   = 3;
727c478bd9Sstevel@tonic-gate     protected static final int CHAR_LATIN1  = 4;
737c478bd9Sstevel@tonic-gate     protected static final int CHAR_UTF8    = 6;
747c478bd9Sstevel@tonic-gate     protected static final int CHAR_UNICODE = 1000;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate     // First two bytes indicate that string is big/little endian Unicode.
777c478bd9Sstevel@tonic-gate     //  If this flag isn't set, then big endian is assumed and we
787c478bd9Sstevel@tonic-gate     //  must add the big endian bytes on every call.
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate     protected static final byte[] UNICODE_LITTLE_FLAG =
817c478bd9Sstevel@tonic-gate 					{(byte)0xFF, (byte)0xFE};
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate     protected static final byte[] UNICODE_BIG_FLAG =
847c478bd9Sstevel@tonic-gate 					{(byte)0xFE, (byte)0xFF};
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate     /**
877c478bd9Sstevel@tonic-gate      * Encode the String describing a character encoding into
887c478bd9Sstevel@tonic-gate      * the approprate integer descriptor code.
897c478bd9Sstevel@tonic-gate      *
907c478bd9Sstevel@tonic-gate      * @param encoding The String describing the encoding.
917c478bd9Sstevel@tonic-gate      * @exception ServiceLocationCharSetNotUnderstoodException Thrown if the
927c478bd9Sstevel@tonic-gate      *			String is not recognized.
937c478bd9Sstevel@tonic-gate      */
947c478bd9Sstevel@tonic-gate 
encodeCharacterEncoding(String encoding)957c478bd9Sstevel@tonic-gate     static int encodeCharacterEncoding(String encoding)
967c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (encoding.equals(ASCII)) {
997c478bd9Sstevel@tonic-gate 	    return CHAR_ASCII;
1007c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(LATIN1)) {
1017c478bd9Sstevel@tonic-gate 	    return CHAR_LATIN1;
1027c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UTF8)) {
1037c478bd9Sstevel@tonic-gate 	    return CHAR_UTF8;
1047c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UNICODE)) {
1057c478bd9Sstevel@tonic-gate 	    return CHAR_UNICODE;
1067c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UNICODE_BIG)) {
1077c478bd9Sstevel@tonic-gate 	    return CHAR_UNICODE;
1087c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UNICODE_LITTLE)) {
1097c478bd9Sstevel@tonic-gate 	    return CHAR_UNICODE;
1107c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UNICODE_BIG_NO_HDR)) {
1117c478bd9Sstevel@tonic-gate 	    return CHAR_UNICODE;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	throw
1157c478bd9Sstevel@tonic-gate 	    new ServiceLocationException(
1167c478bd9Sstevel@tonic-gate 				CHARSET_NOT_UNDERSTOOD,
1177c478bd9Sstevel@tonic-gate 				"v1_unsupported_encoding",
1187c478bd9Sstevel@tonic-gate 				new Object[] {encoding});
1197c478bd9Sstevel@tonic-gate     }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate     /**
1227c478bd9Sstevel@tonic-gate      * Decode the integer describing a character encoding into
1237c478bd9Sstevel@tonic-gate      * the approprate String descriptor.
1247c478bd9Sstevel@tonic-gate      *
1257c478bd9Sstevel@tonic-gate      * @param code The integer coding the String set.
1267c478bd9Sstevel@tonic-gate      * @exception ServiceLocationCharSetNotUnderstoodException Thrown if the
1277c478bd9Sstevel@tonic-gate      *			integer is not recognized.
1287c478bd9Sstevel@tonic-gate      */
1297c478bd9Sstevel@tonic-gate 
decodeCharacterEncoding(int code)1307c478bd9Sstevel@tonic-gate     static String decodeCharacterEncoding(int code)
1317c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	switch (code) {
1347c478bd9Sstevel@tonic-gate 	case CHAR_ASCII: 	return ASCII;
1357c478bd9Sstevel@tonic-gate 	case CHAR_LATIN1:	return LATIN1;
1367c478bd9Sstevel@tonic-gate 	case CHAR_UTF8:	return UTF8;
1377c478bd9Sstevel@tonic-gate 	case CHAR_UNICODE:	return UNICODE;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	throw
1417c478bd9Sstevel@tonic-gate 	    new ServiceLocationException(
1427c478bd9Sstevel@tonic-gate 				CHARSET_NOT_UNDERSTOOD,
1437c478bd9Sstevel@tonic-gate 				"v1_unsupported_encoding",
1447c478bd9Sstevel@tonic-gate 				new Object[] {Integer.toString(code)});
1457c478bd9Sstevel@tonic-gate     }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate     /**
1487c478bd9Sstevel@tonic-gate      * Return a string of integers giving the character's encoding in
1497c478bd9Sstevel@tonic-gate      * the character set passed in as encoding.
1507c478bd9Sstevel@tonic-gate      *
1517c478bd9Sstevel@tonic-gate      * @param c The character to escape.
1527c478bd9Sstevel@tonic-gate      * @param encoding The character set encoding to use.
1537c478bd9Sstevel@tonic-gate      * @return The character as a string of integers for the encoding.
1547c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException Thrown if the encoding is not
1557c478bd9Sstevel@tonic-gate      *		 recognized, if the character's encoding
1567c478bd9Sstevel@tonic-gate      *		 has more than 8 bytes or if the sign bit gets turned on.
1577c478bd9Sstevel@tonic-gate      */
1587c478bd9Sstevel@tonic-gate 
escapeChar(char c, String encoding)1597c478bd9Sstevel@tonic-gate     static String escapeChar(char c, String encoding)
1607c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	try {
1657c478bd9Sstevel@tonic-gate 	    OutputStreamWriter osw = new OutputStreamWriter(baos, encoding);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	    osw.write(c);
1687c478bd9Sstevel@tonic-gate 	    osw.flush();
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	} catch (UnsupportedEncodingException ex) {
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	    throw
1737c478bd9Sstevel@tonic-gate 		new ServiceLocationException(
1747c478bd9Sstevel@tonic-gate 				CHARSET_NOT_UNDERSTOOD,
1757c478bd9Sstevel@tonic-gate 				"v1_unsupported_encoding",
1767c478bd9Sstevel@tonic-gate 				new Object[] {encoding});
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	} catch (IOException ex) {
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	byte b[] = baos.toByteArray();
1837c478bd9Sstevel@tonic-gate 	int code = 0;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	// Assemble the character code based on the encoding type.
1867c478bd9Sstevel@tonic-gate 
187*55fea89dSDan Cross 	if (encoding.equals(UNICODE) ||
1887c478bd9Sstevel@tonic-gate 	    encoding.equals(UNICODE_BIG) ||
1897c478bd9Sstevel@tonic-gate 	    encoding.equals(UNICODE_LITTLE)) {
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	    code = (int)(b[0] & 0xFF);		// control bytes...
1927c478bd9Sstevel@tonic-gate 	    code = (int)(code | ((b[1] & 0xFF) << 8));
1937c478bd9Sstevel@tonic-gate 	    code = (int)(code | ((b[2] & 0xFF) << 16));
1947c478bd9Sstevel@tonic-gate 	    code = (int)(code | ((b[3] & 0xFF) << 24));
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	    if (b.length <= 4) {
1977c478bd9Sstevel@tonic-gate 		throw
1987c478bd9Sstevel@tonic-gate 		    new ServiceLocationException(
1997c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
2007c478bd9Sstevel@tonic-gate 				"v1_charcode_error",
2017c478bd9Sstevel@tonic-gate 				new Object[] {new Character(c), encoding});
2027c478bd9Sstevel@tonic-gate 	    }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(ASCII) || encoding.equals(LATIN1)) {
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	    code = (int)(b[0] & 0xFF);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	    if (b.length > 1) {
2097c478bd9Sstevel@tonic-gate 		throw
2107c478bd9Sstevel@tonic-gate 		    new ServiceLocationException(
2117c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
2127c478bd9Sstevel@tonic-gate 				"v1_charcode_error",
2137c478bd9Sstevel@tonic-gate 				new Object[] {new Character(c), encoding});
2147c478bd9Sstevel@tonic-gate 	    }
2157c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UTF8)) {
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	    if (b.length > 3) {
2187c478bd9Sstevel@tonic-gate 		throw
2197c478bd9Sstevel@tonic-gate 		    new ServiceLocationException(
2207c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
2217c478bd9Sstevel@tonic-gate 				"v1_charcode_error",
2227c478bd9Sstevel@tonic-gate 				new Object[] {new Character(c), encoding});
2237c478bd9Sstevel@tonic-gate 	    }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	    code = (int)(b[0] & 0xFF);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	    if (b.length > 1) {
2297c478bd9Sstevel@tonic-gate 		code = (int)(code | ((b[1] & 0xFF) << 8));
2307c478bd9Sstevel@tonic-gate 	    }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	    if (b.length > 2) {
2337c478bd9Sstevel@tonic-gate 		code = (int)(code | ((b[2] & 0xFF) << 16));
2347c478bd9Sstevel@tonic-gate 	    }
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return Integer.toString(code);
2387c478bd9Sstevel@tonic-gate     }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate     /**
2417c478bd9Sstevel@tonic-gate      * Unescape the character encoded as the string.
2427c478bd9Sstevel@tonic-gate      *
2437c478bd9Sstevel@tonic-gate      * @param ch The character as a string of Integers.
2447c478bd9Sstevel@tonic-gate      * @param encoding The character set encoding to use.
2457c478bd9Sstevel@tonic-gate      * @return The character.
2467c478bd9Sstevel@tonic-gate      * @exception ServiceLocationException Thrown if the string can't
2477c478bd9Sstevel@tonic-gate      *		 be parsed into an integer or if the encoding isn't
2487c478bd9Sstevel@tonic-gate      *		 recognized.
2497c478bd9Sstevel@tonic-gate      */
2507c478bd9Sstevel@tonic-gate 
unescapeChar(String ch, String encoding)2517c478bd9Sstevel@tonic-gate     static String unescapeChar(String ch, String encoding)
2527c478bd9Sstevel@tonic-gate 	throws ServiceLocationException {
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	int code = 0;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	try {
2577c478bd9Sstevel@tonic-gate 	    code = Integer.parseInt(ch);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	} catch (NumberFormatException ex) {
2607c478bd9Sstevel@tonic-gate 	    throw
2617c478bd9Sstevel@tonic-gate 		new ServiceLocationException(
2627c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
2637c478bd9Sstevel@tonic-gate 				"v1_stringcode_error",
2647c478bd9Sstevel@tonic-gate 				new Object[] {ch, encoding});
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	// Convert to bytes. We need to taylor the array size to the
2697c478bd9Sstevel@tonic-gate 	//  number of bytes because otherwise, in encodings that
2707c478bd9Sstevel@tonic-gate 	//  take less bytes, the resulting string will have garbage
2717c478bd9Sstevel@tonic-gate 	//  in it.
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	String str = null;
2747c478bd9Sstevel@tonic-gate 	byte b0 = 0, b1 = 0, b2 = 0, b3 = 0;
2757c478bd9Sstevel@tonic-gate 	byte b[] = null;
2767c478bd9Sstevel@tonic-gate 
277*55fea89dSDan Cross 	b0 = (byte) (code & 0xFF);
2787c478bd9Sstevel@tonic-gate 	b1 = (byte) ((code >> 8) & 0xFF);
2797c478bd9Sstevel@tonic-gate 	b2 = (byte) ((code >> 16) & 0xFF);
2807c478bd9Sstevel@tonic-gate 	b3 = (byte) ((code >> 24) & 0xFf);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	// We create an array sized to the encoding.
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (encoding.equals(UNICODE_BIG) ||
2857c478bd9Sstevel@tonic-gate 	    encoding.equals(UNICODE_LITTLE)) {
2867c478bd9Sstevel@tonic-gate 	    b = new byte[4];
2877c478bd9Sstevel@tonic-gate 	    b[0] = b0;
2887c478bd9Sstevel@tonic-gate 	    b[1] = b1;
2897c478bd9Sstevel@tonic-gate 	    b[2] = b2;
2907c478bd9Sstevel@tonic-gate 	    b[3] = b3;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(LATIN1) || encoding.equals(ASCII)) {
2937c478bd9Sstevel@tonic-gate 	    // single byte
2947c478bd9Sstevel@tonic-gate 	    b = new byte[1];
2957c478bd9Sstevel@tonic-gate 	    b[0] = b0;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	    if (b1 != 0 || b2 != 0) {
2987c478bd9Sstevel@tonic-gate 		throw
2997c478bd9Sstevel@tonic-gate 		    new ServiceLocationException(
3007c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
3017c478bd9Sstevel@tonic-gate 				"v1_stringcode_error",
3027c478bd9Sstevel@tonic-gate 				new Object[] {ch, encoding});
3037c478bd9Sstevel@tonic-gate 	    }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	} else if (encoding.equals(UTF8)) {// vari-byte
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	    if (b3 != 0) {
3097c478bd9Sstevel@tonic-gate 		throw
3107c478bd9Sstevel@tonic-gate 		    new ServiceLocationException(
3117c478bd9Sstevel@tonic-gate 				ServiceLocationException.PARSE_ERROR,
3127c478bd9Sstevel@tonic-gate 				"v1_stringcode_error",
3137c478bd9Sstevel@tonic-gate 				new Object[] {ch, encoding});
3147c478bd9Sstevel@tonic-gate 	    }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	    if (b2 != 0) {
3177c478bd9Sstevel@tonic-gate 		b = new byte[3];
3187c478bd9Sstevel@tonic-gate 		b[2] = b2;
3197c478bd9Sstevel@tonic-gate 		b[1] = b1;
3207c478bd9Sstevel@tonic-gate 		b[0] = b0;
3217c478bd9Sstevel@tonic-gate 	    } else if (b1 != 0) {
3227c478bd9Sstevel@tonic-gate 		b = new byte[2];
3237c478bd9Sstevel@tonic-gate 		b[1] = b1;
3247c478bd9Sstevel@tonic-gate 		b[0] = b0;
3257c478bd9Sstevel@tonic-gate 	    } else {
3267c478bd9Sstevel@tonic-gate 		b = new byte[1];
3277c478bd9Sstevel@tonic-gate 		b[0] = b0;
3287c478bd9Sstevel@tonic-gate 	    }
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	// Make a string out of it.
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	try {
3347c478bd9Sstevel@tonic-gate 	    str = new String(b, encoding);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	} catch (UnsupportedEncodingException ex) {
3377c478bd9Sstevel@tonic-gate 	    Assert.slpassert(false,
3387c478bd9Sstevel@tonic-gate 			  "v1_unsupported_encoding",
3397c478bd9Sstevel@tonic-gate 			  new Object[] {encoding});
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	return str;
3437c478bd9Sstevel@tonic-gate     }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate     // Determine from the flag bytes whether this is big or little endian
3467c478bd9Sstevel@tonic-gate     //  Unicode. If there are no flag bytes, then just return UNICODE.
3477c478bd9Sstevel@tonic-gate 
getUnicodeEndianess(byte[] bytes)3487c478bd9Sstevel@tonic-gate     static String getUnicodeEndianess(byte[] bytes) {
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (bytes.length >= 2) {
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	    if (bytes[0] == UNICODE_LITTLE_FLAG[0] &&
3537c478bd9Sstevel@tonic-gate 		bytes[1] == UNICODE_LITTLE_FLAG[1]) {
3547c478bd9Sstevel@tonic-gate 		return UNICODE_LITTLE;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	    } else if (bytes[0] == UNICODE_BIG_FLAG[0] &&
3577c478bd9Sstevel@tonic-gate 		       bytes[1] == UNICODE_BIG_FLAG[1]) {
3587c478bd9Sstevel@tonic-gate 		return UNICODE_BIG;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	    }
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	// We can`t tell from the byte header, so it's big endian. But
3647c478bd9Sstevel@tonic-gate 	//  since we need to add the byte header, we say we don't know.
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	return UNICODE;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate     }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate     // Add the big endian flag to a Unicode string.
3717c478bd9Sstevel@tonic-gate 
addBigEndianFlag(byte[] bytes)3727c478bd9Sstevel@tonic-gate     static byte[] addBigEndianFlag(byte[] bytes) {
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	byte[] flaggedBytes = new byte[bytes.length + 2];
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	flaggedBytes[0] = UNICODE_BIG_FLAG[0];
3777c478bd9Sstevel@tonic-gate 	flaggedBytes[1] = UNICODE_BIG_FLAG[1];
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	System.arraycopy(flaggedBytes, 2, bytes, 0, bytes.length);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	return flaggedBytes;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate     }
3847c478bd9Sstevel@tonic-gate }
385