static String convertFromUTF (byte[] buf)
throws EOFException, UTFDataFormatException
{
// Give StringBuffer an initial estimated size to avoid
// enlarge buffer frequently
CPStringBuilder strbuf = new CPStringBuilder (buf.length / 2 + 2);
for (int i = 0; i < buf.length; )
{
if ((buf [i] & 0x80) == 0) // bit pattern 0xxxxxxx
strbuf.append ((char) (buf [i++] & 0xFF));
else if ((buf [i] & 0xE0) == 0xC0) // bit pattern 110xxxxx
{
if (i + 1 >= buf.length
|| (buf [i + 1] & 0xC0) != 0x80)
throw new UTFDataFormatException ();
strbuf.append((char) (((buf [i++] & 0x1F) << 6)
| (buf [i++] & 0x3F)));
}
else if ((buf [i] & 0xF0) == 0xE0) // bit pattern 1110xxxx
{
if (i + 2 >= buf.length
|| (buf [i + 1] & 0xC0) != 0x80
|| (buf [i + 2] & 0xC0) != 0x80)
throw new UTFDataFormatException ();
strbuf.append ((char) (((buf [i++] & 0x0F) << 12)
| ((buf [i++] & 0x3F) << 6)
| (buf [i++] & 0x3F)));
}
else // must be ((buf [i] & 0xF0) == 0xF0 || (buf [i] & 0xC0) == 0x80)
throw new UTFDataFormatException (); // bit patterns 1111xxxx or
// 10xxxxxx
}
return strbuf.toString ();
}