/**
* Converts from utf8 to iso-8859-1
*/
public static Value utf8_decode(Env env, StringValue str)
{
StringValue sb = env.createUnicodeBuilder();
int len = str.length();
for (int i = 0; i < len; i++) {
int ch = str.charAt(i) & 0xff;
if (ch < 0x80)
sb.append((char) ch);
else if ((ch & 0xe0) == 0xc0) {
int d1 = (ch & 0x1f) << 6;
int d2 = str.charAt(++i) & 0x3f;
sb.append((char) (d1 + d2));
}
else {
int d1 = (ch & 0xf) << 12;
int d2 = (str.charAt(++i) & 0x3f) << 6;
int d3 = (str.charAt(++i) & 0x3f);
sb.append((char) (d1 + d2 + d3));
}
}
return sb;
}