1:
8:
9: package ;
10:
11:
16:
17: public class Output_UTF8 extends UnicodeToBytes
18: {
19: public String getName() { return "UTF8"; }
20:
21:
24: public boolean standardUTF8 = true;
25:
26:
27: char hi_part;
28:
29: int value;
30:
31: int bytes_todo;
32:
33: public int write (char[] inbuffer, int inpos, int inlength)
34: {
35: int start_pos = inpos;
36: int avail = buf.length - count;
37: for (;;)
38: {
39: if (avail == 0 || (inlength == 0 && bytes_todo == 0 && hi_part == 0))
40: break;
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51: if (bytes_todo > 0)
52: {
53: do
54: {
55: bytes_todo--;
56: buf[count++] = (byte)
57: (((value >> (bytes_todo * 6)) & 0x3F) | 0x80);
58: avail--;
59: }
60: while (bytes_todo > 0 && avail > 0);
61: continue;
62: }
63:
64:
65: if (inlength == 0 && hi_part != 0)
66: {
67: buf[count++] = (byte) (0xE0 | (hi_part >> 12));
68: value = hi_part;
69: hi_part = 0;
70: avail--;
71: bytes_todo = 2;
72: continue;
73: }
74:
75: char ch = inbuffer[inpos++];
76: inlength--;
77:
78: if (hi_part != 0 && (ch <= 0xDBFF || ch > 0xDFFF))
79: {
80:
81:
82:
83: --inpos;
84: ++inlength;
85: buf[count++] = (byte) (0xE0 | (hi_part >> 12));
86: value = hi_part;
87: hi_part = 0;
88: avail--;
89: bytes_todo = 2;
90: }
91: else if (hi_part == 0 && ch >= 0xDC00 && ch <= 0xDFFF)
92: {
93:
94:
95:
96: buf[count++] = (byte) (0xE0 | (ch >> 12));
97: value = ch;
98: avail--;
99: bytes_todo = 2;
100: }
101: else if (ch < 128 && (ch != 0 || standardUTF8))
102: {
103: avail--;
104: buf[count++] = (byte) ch;
105: }
106: else if (ch <= 0x07FF)
107: {
108: buf[count++] = (byte) (0xC0 | (ch >> 6));
109: avail--;
110: value = ch;
111: bytes_todo = 1;
112: }
113: else if (ch >= 0xD800 && ch <= 0xDFFF && standardUTF8)
114: {
115: if (ch <= 0xDBFF)
116: {
117:
118:
119: hi_part = ch;
120: }
121: else
122: {
123: value = (hi_part - 0xD800) * 0x400 + (ch - 0xDC00) + 0x10000;
124: buf[count++] = (byte) (0xF0 | (value >> 18));
125: avail--;
126: bytes_todo = 3;
127: hi_part = 0;
128: }
129: }
130: else
131: {
132: buf[count++] = (byte) (0xE0 | (ch >> 12));
133: value = ch;
134: avail--;
135: bytes_todo = 2;
136: }
137: }
138: return inpos - start_pos;
139: }
140:
141: public boolean havePendingBytes()
142: {
143: return bytes_todo > 0 || hi_part != 0;
144: }
145:
146: }