FileDocCategorySizeDatePackage
UTF_8_Writer.javaAPI DocphoneME MR2 API (J2ME)6581Wed May 02 18:00:02 BST 2007com.sun.cldc.i18n.j2me

UTF_8_Writer

public class UTF_8_Writer extends com.sun.cldc.i18n.StreamWriter
Writer for UTF-8 encoded output streams. NOTE: The UTF-8 writer only supports UCS-2, or Unicode, to UTF-8 conversion. There is no support for UTF-16 encoded characters outside of the Basic Multilingual Plane (BMP). These are encoded in UTF-16 using previously reserved values between U+D800 and U+DFFF. Additionally, the UTF-8 writer does not support any character that requires 4 or more UTF-8 encoded bytes.

Fields Summary
protected int
pendingSurrogate
pending high surrogate code unit, or zero
private static final int
replacementValue
This value replaces invalid characters (that is, surrogates code units without a pair)
Constructors Summary
Methods Summary
public voidclose()
Close the writer and the output stream.

throws
IOException

        if (0 != pendingSurrogate) {
            // write replacement value instead of the unpaired surrogate
            byte[] outputByte = new byte[1];
            outputByte[0] = replacementValue;
            out.write(outputByte, 0, 1);
        }
        pendingSurrogate = 0;
        super.close();
    
public java.io.Writeropen(java.io.OutputStream outputStream, java.lang.String encoding)
Open the writer.

param
outputStream
param
encoding encoding
return
the writer
throws
UnsupportedEncodingException

        pendingSurrogate = 0;
        return super.open(outputStream,encoding);
    
public intsizeOf(char[] array, int offset, int length)
Get the size in chars of an array of bytes.

param
array Source buffer
param
offset Offset at which to start counting characters
param
length number of bytes to use for counting
return
number of characters that would be converted

        int outputSize = 0;
        int inputChar;

        while (offset < length) {
            inputChar = array[offset];
            if (inputChar < 0x80) {
            outputSize++;
            } else if (inputChar < 0x800) {
            outputSize += 2;
            } else {
            outputSize += 3;
            }
            offset++;
        }
        return outputSize;
    
public voidwrite(char[] cbuf, int off, int len)
Write a portion of an array of characters.

param
cbuf Array of characters
param
off Offset from which to start writing characters
param
len Number of characters to write
exception
IOException If an I/O error occurs

                                                       
              
        byte[] outputByte = new byte[4];     // Never more than 4 encoded bytes
        int inputChar;
        int outputSize;
        int count = 0;

        while (count < len) {
            inputChar = 0xffff & cbuf[off + count];
            if (0 != pendingSurrogate) {
                if (0xdc00<=inputChar && inputChar<=0xdfff) {
                //000u uuuu xxxx xxxx xxxx xxxx
                //1101 10ww wwxx xxxx   1101 11xx xxxx xxxx
                    final int highHalf = (pendingSurrogate & 0x03ff) + 0x0040;
                    final int lowHalf = inputChar & 0x03ff;
                    inputChar = (highHalf << 10) | lowHalf;
                } else {
                    // write replacement value instead of unpaired surrogate
                    outputByte[0] = replacementValue;
                    outputSize = 1;
                    out.write(outputByte, 0, outputSize);
                }
                pendingSurrogate = 0;
            }
            if (inputChar < 0x80) {
                outputByte[0] = (byte)inputChar;
                outputSize = 1;
            } else if (inputChar < 0x800) {
                outputByte[0] = (byte)(0xc0 | ((inputChar >> 6) & 0x1f));
                outputByte[1] = (byte)(0x80 | (inputChar & 0x3f));
                outputSize = 2;
            } else if (0xd800<=inputChar && inputChar<=0xdbff) {
                pendingSurrogate = inputChar;
                outputSize = 0;
            } else if (0xdc00<=inputChar && inputChar<=0xdfff) {
                // unpaired surrogate
                outputByte[0] = replacementValue;
                outputSize = 1;
            } else if (inputChar < 0x10000) {
                outputByte[0] = (byte)(0xe0 | ((inputChar >> 12) & 0x0f));
                outputByte[1] = (byte)(0x80 | ((inputChar >> 6) & 0x3f));
                outputByte[2] = (byte)(0x80 | (inputChar & 0x3f));
                outputSize = 3;
            } else {
                /* 21 bits: 1111 0xxx  10xx xxxx  10xx xxxx  10xx xxxx
                 * a aabb  bbbb cccc  ccdd dddd
                */
                outputByte[0] = (byte)(0xf0 | ((inputChar >> 18) & 0x07));
                outputByte[1] = (byte)(0x80 | ((inputChar >> 12) & 0x3f));
                outputByte[2] = (byte)(0x80 | ((inputChar >> 6) & 0x3f));
                outputByte[3] = (byte)(0x80 | (inputChar & 0x3f));
                outputSize = 4;
            }
            out.write(outputByte, 0, outputSize);
            count++;
        }