FileDocCategorySizeDatePackage
TextEncoder.javaAPI DocphoneME MR2 API (J2ME)15769Wed May 02 18:00:32 BST 2007com.sun.midp.io.j2me.sms

TextEncoder

public class TextEncoder extends Object
Text encoder and decoder for GSM 7-bit text and UCS-2 characters.

Fields Summary
protected static byte[]
chars7Bit
GSM 7-bit character to UCS-2 mapping tables.
protected static byte[]
escaped7BitChars
GSM 7-bit escaped character to UCS-2 mapping tables.
protected static char[]
charsUCS2
GSM UCS-2 mapping tables.
protected static char[]
escapedUCS2
GSM escaped character UCS-2 mapping tables.
Constructors Summary
Methods Summary
public static byte[]decode(byte[] gsm7bytes)
Converts a GSM 7-bit encoded byte array into a UCS-2 byte array.

param
gsm7bytes an array of GSM 7-bit encoded characters
return
an array of UCS-2 characters in a byte array

	/*
	 * Initialize a buffer with expected size twice that of
	 * the 7-bit encoded text
	 */
	ByteArrayOutputStream bos =
	    new ByteArrayOutputStream(gsm7bytes.length * 2);

	for (int i = 0; i < gsm7bytes.length; i++) {
	    /*
	     * Check for escaped characters first.
	     */
	    if (gsm7bytes[i] == 0x1b) {
		/*
		 * Advance the pointer past the escape.
		 */
		i++;

		for (int j = 0; j < escaped7BitChars.length; j++) {
		    if (gsm7bytes[i] == escaped7BitChars[j]) {
			bos.write(escapedUCS2[j] >> 8);
			bos.write(escapedUCS2[j] & 0xFF);
			break;
		    }
		}

	    } else {
		for (int j = 0; j < chars7Bit.length; j++) {
		    if (gsm7bytes[i] == chars7Bit[j]) {
			bos.write(charsUCS2[j] >> 8);
			bos.write(charsUCS2[j] & 0xFF);
			break;
		    }
		}
	    }
	}

	return bos.toByteArray();
    
public static byte[]encode(byte[] ucsbytes)
Converts a UCS-2 character array into GSM 7-bit bytes.

param
ucsbytes an array of UCS-2 characters in a byte array
return
array of GSM 7-bit bytes if the conversion was successful, otherwise return null to indicate that some UCS-2 values were included that can not be translated to the GSM 7-bit format


                                                                   
         
	/*
	 * Initialize a buffer with expected size twice that of
	 * the 7-bit encoded text.
	 */
	ByteArrayOutputStream bos = new ByteArrayOutputStream(ucsbytes.length);

	/*
	 * Walk through the UCS 2 characters 2 bytes at a time.
	 * All characters must be in the direct or extended UCS
	 * character tables. If not we reject the entire conversion.
	 */
	for (int i = 0; i < ucsbytes.length; i += 2) {
	    int j;
	    for (j = 0; j < charsUCS2.length; j++) {
		if (ucsbytes[i] == (charsUCS2[j] >> 8) &&
		    ucsbytes[i+1] == (charsUCS2[j] & 0xFF)) {
		    bos.write(chars7Bit[j]);
		    break;
		}
	    }

	    /*
	     * If you get to the end of the basic character table,
	     * check the extra escaped sequence table, too.
	     */
	    if (j == charsUCS2.length) {
		int k;
		for (k = 0; k < escapedUCS2.length; k++) {
		    if (ucsbytes[i] == (escapedUCS2[k] >> 8) &&
			ucsbytes[i+1] == (escapedUCS2[k] & 0xFF)) {
			bos.write(0x1b);
			bos.write(escaped7BitChars[k]);
			break;
		    }
		}
		/*
		 * If no match is found in either table,
		 * return null to indicate UCS 2 characters
		 * were found that are not included in the
		 * GSM 7 bit encoding.
		 */
		if (k == escapedUCS2.length) {
		    return null;
		}
	    }
	}
	return bos.toByteArray();
    
public static byte[]toByteArray(java.lang.String data)
Converts a string to a UCS-2 byte array.

param
data a String to be converted
return
an array of bytes in UCS-2 character

	char[] c = data.toCharArray();
	ByteArrayOutputStream bos =
	    new ByteArrayOutputStream(data.length());
	for (int i = 0; i < c.length; i ++) {
	    bos.write(c[i] >> 8);
	    bos.write(c[i] & 0xFF);
	}
	return bos.toByteArray();
    
public static java.lang.StringtoString(byte[] ucsbytes)
Gets a String from the UCS-2 byte array.

param
ucsbytes an array of UCS-2 characters as a byte array
return
Java string

	char[] c = new char [ucsbytes.length/2];
	/*
	 * Create a string from the raw UCS 2 bytes.
	 */
	for (int i = 0; i < ucsbytes.length; i += 2) {
	    c[i/2] = (char)((ucsbytes[i] << 8)
			    +  (ucsbytes[i+1] & 0xFF));
	}
	return new String(c);