readUTF8EncodedString2 method
Reads a UTF-8 encoded string of specified length.
Uses optimized decoding strategies based on string length:
- Small strings (≤64 bytes): Uses sublist for efficiency
- Large strings: Uses range conversion to avoid copying
stringLength The number of bytes to read and decode
Returns the decoded UTF-8 string
Implementation
String readUTF8EncodedString2(int stringLength) {
assert(stringLength > 0);
if (_bufferPosition + stringLength <= _bufferData.length) {
int startPos = _bufferPosition;
_bufferPosition += stringLength;
//_log.info("Reading utf8 $stringLength bytes ending at $_bufferPosition");
// Use cached decoder and avoid creating intermediate lists for small strings
if (stringLength <= 64) {
// For small strings, use sublist view which is more efficient
String result = _utf8Decoder.convert(_bufferData.sublist(startPos, startPos + stringLength));
//_log.info("String found $result");
return result;
} else {
// For larger strings, use range conversion to avoid copying
String result = _utf8Decoder.convert(_bufferData, startPos, startPos + stringLength);
//_log.info("String found $result");
return result;
}
}
throw Exception("Cannot read utf8 string with $stringLength length at position $_bufferPosition of data with ${_bufferData.length} bytes");
}