UTF_8_Writerpublic 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 | pendingSurrogatepending high surrogate code unit, or zero | private static final int | replacementValueThis value replaces invalid characters
(that is, surrogates code units without a pair) |
Methods Summary |
---|
public void | close()Close the writer and the output stream.
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.Writer | open(java.io.OutputStream outputStream, java.lang.String encoding)Open the writer.
pendingSurrogate = 0;
return super.open(outputStream,encoding);
| public int | sizeOf(char[] array, int offset, int length)Get the size in chars of an array of bytes.
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 void | write(char[] cbuf, int off, int len)Write a portion of an array of characters.
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++;
}
|
|