in src/nms-api/Util/EndianBinaryReader.cs [193:259]
private string doReadString(int utfLength)
{
char[] result = new char[utfLength];
byte[] buffer = new byte[utfLength];
int bytesRead = 0;
while (bytesRead < utfLength)
{
int rc = Read(buffer, bytesRead, utfLength - bytesRead);
if (rc == 0)
{
throw new IOException("premature end of stream");
}
bytesRead += rc;
}
int count = 0;
int index = 0;
byte a = 0;
while (count < utfLength)
{
if ((result[index] = (char) buffer[count++]) < 0x80)
{
index++;
}
else if (((a = (byte) result[index]) & 0xE0) == 0xC0)
{
if (count >= utfLength)
{
throw new IOException("Invalid UTF-8 encoding found, start of two byte char found at end.");
}
byte b = buffer[count++];
if ((b & 0xC0) != 0x80)
{
throw new IOException("Invalid UTF-8 encoding found, byte two does not start with 0x80.");
}
result[index++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
}
else if ((a & 0xF0) == 0xE0)
{
if (count + 1 >= utfLength)
{
throw new IOException("Invalid UTF-8 encoding found, start of three byte char found at end.");
}
byte b = buffer[count++];
byte c = buffer[count++];
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80))
{
throw new IOException("Invalid UTF-8 encoding found, byte two does not start with 0x80.");
}
result[index++] = (char) (((a & 0x0F) << 12) |
((b & 0x3F) << 6) | (c & 0x3F));
}
else
{
throw new IOException("Invalid UTF-8 encoding found, aborting.");
}
}
return new String(result, 0, index);
}