/*
 * Java core library component.
 *
 * Copyright (c) 1997, 1998
 *      Transvirtual Technologies, Inc.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file.
 */

package kaffe.io;

public class CharToByteUTF8 extends CharToByteConverter {

public CharToByteUTF8() {
}

public int convert ( char[] from, int fpos, int flen, byte[] to, int tpos, int tlen ) {
	int o = tpos;
	int oe = tpos + tlen;
	int i = fpos;
	int ie = fpos + flen;

	for (; i < ie && o < oe; i++) {
                char chr = from[i];
                if (chr >= '\u0001' && chr <= '\u007F') {
			to[o++] = (byte)chr;
		}
                else if (chr <= '\u07FF') {
                        to[o++] = (byte)(0xC0 | (0x3F & (chr >> 6)));
                        to[o++] = (byte)(0x80 | (0x3F & chr));
                }
                else {
                        to[o++] = (byte)(0xE0 | (0x0F & (chr >> 12)));
                        to[o++] = (byte)(0x80 | (0x3F & (chr >>  6)));
                        to[o++] = (byte)(0x80 | (0x3F & chr));
                }
        }

	// Carry anything left.
	if (ie > i) {
		carry(from, i, ie - i);
	}

	return (o - tpos);
}

public int getNumberOfBytes ( char[] from, int fpos, int flen ) {
	return flen;
}

}

@@ -512,18 +512,8 @@
 %3C ie
- && o %3C oe
 ; i+
@@ -513,24 +513,24 @@
  ie; i++) %7B%0A
+
             
@@ -611,16 +611,50 @@
 07F') %7B%0A
+%09%09%09if (o %3E= oe) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A
 %09%09%09to%5Bo+
@@ -713,24 +713,62 @@
 '%5Cu07FF') %7B%0A
+%09%09%09if (o + 1 %3E= oe) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A
             
@@ -898,32 +898,32 @@
               %7D%0A
-
                 
@@ -929,16 +929,54 @@
  else %7B%0A
+%09%09%09if (o + 2 %3E= oe) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A
         

Fix test of buffer overflow. Reported by Ito Kazumitsu <ito@htk.hitachi-cable.co.jp>.