123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package gnu.gcj.convert;
- public class Output_UTF8 extends UnicodeToBytes
- {
- public String getName() { return "UTF8"; }
-
- public boolean standardUTF8 = true;
-
- char hi_part;
-
- int value;
-
- int bytes_todo;
- public int write (char[] inbuffer, int inpos, int inlength)
- {
- int start_pos = inpos;
- int avail = buf.length - count;
- for (;;)
- {
- if (avail == 0 || (inlength == 0 && bytes_todo == 0 && hi_part == 0))
- break;
-
-
-
-
-
-
-
-
-
- if (bytes_todo > 0)
- {
- do
- {
- bytes_todo--;
- buf[count++] = (byte)
- (((value >> (bytes_todo * 6)) & 0x3F) | 0x80);
- avail--;
- }
- while (bytes_todo > 0 && avail > 0);
- continue;
- }
-
- if (inlength == 0 && hi_part != 0)
- {
- buf[count++] = (byte) (0xE0 | (hi_part >> 12));
- value = hi_part;
- hi_part = 0;
- avail--;
- bytes_todo = 2;
- continue;
- }
- char ch = inbuffer[inpos++];
- inlength--;
- if (hi_part != 0 && (ch <= 0xDBFF || ch > 0xDFFF))
- {
-
-
-
- --inpos;
- ++inlength;
- buf[count++] = (byte) (0xE0 | (hi_part >> 12));
- value = hi_part;
- hi_part = 0;
- avail--;
- bytes_todo = 2;
- }
- else if (hi_part == 0 && ch >= 0xDC00 && ch <= 0xDFFF)
- {
-
-
-
- buf[count++] = (byte) (0xE0 | (ch >> 12));
- value = ch;
- avail--;
- bytes_todo = 2;
- }
- else if (ch < 128 && (ch != 0 || standardUTF8))
- {
- avail--;
- buf[count++] = (byte) ch;
- }
- else if (ch <= 0x07FF)
- {
- buf[count++] = (byte) (0xC0 | (ch >> 6));
- avail--;
- value = ch;
- bytes_todo = 1;
- }
- else if (ch >= 0xD800 && ch <= 0xDFFF && standardUTF8)
- {
- if (ch <= 0xDBFF)
- {
-
-
- hi_part = ch;
- }
- else
- {
- value = (hi_part - 0xD800) * 0x400 + (ch - 0xDC00) + 0x10000;
- buf[count++] = (byte) (0xF0 | (value >> 18));
- avail--;
- bytes_todo = 3;
- hi_part = 0;
- }
- }
- else
- {
- buf[count++] = (byte) (0xE0 | (ch >> 12));
- value = ch;
- avail--;
- bytes_todo = 2;
- }
- }
- return inpos - start_pos;
- }
- public boolean havePendingBytes()
- {
- return bytes_todo > 0 || hi_part != 0;
- }
- }
|