in kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java [110:164]
public static String fromUTF8ByteArray(byte[] bytes) {
int i = 0;
int length = 0;
while (i < bytes.length) {
length++;
if ((bytes[i] & 0xf0) == 0xf0) {
// surrogate pair
length++;
i += 4;
} else if ((bytes[i] & 0xe0) == 0xe0) {
i += 3;
} else if ((bytes[i] & 0xc0) == 0xc0) {
i += 2;
} else {
i += 1;
}
}
char[] cs = new char[length];
i = 0;
length = 0;
while (i < bytes.length) {
char ch;
if ((bytes[i] & 0xf0) == 0xf0) {
int codePoint = ((bytes[i] & 0x03) << 18) | ((bytes[i + 1] & 0x3F) << 12)
| ((bytes[i + 2] & 0x3F) << 6) | (bytes[i + 3] & 0x3F);
int u = codePoint - 0x10000;
char w1 = (char) (0xD800 | (u >> 10));
char w2 = (char) (0xDC00 | (u & 0x3FF));
cs[length++] = w1;
ch = w2;
i += 4;
} else if ((bytes[i] & 0xe0) == 0xe0) {
ch = (char) (((bytes[i] & 0x0f) << 12)
| ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f));
i += 3;
} else if ((bytes[i] & 0xd0) == 0xd0) {
ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f));
i += 2;
} else if ((bytes[i] & 0xc0) == 0xc0) {
ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f));
i += 2;
} else {
ch = (char) (bytes[i] & 0xff);
i += 1;
}
cs[length++] = ch;
}
return new String(cs);
}