readString method
Read a null-terminated string, or if size
is provided, that number of
bytes returned as a string.
Implementation
String readString({int? size, bool utf8 = true}) {
String codesToString(List<int> codes) {
try {
final str = utf8
? const Utf8Decoder().convert(codes)
: String.fromCharCodes(codes);
return str;
} catch (err) {
// If the string is not a valid UTF8 string, decode it as character
// codes.
return String.fromCharCodes(codes);
}
}
if (size == null) {
final codes = <int>[];
if (isEOS) {
return '';
}
while (!isEOS) {
final c = readByte();
if (c == 0) {
return codesToString(codes);
}
codes.add(c);
}
return codesToString(codes);
}
final s = readBytes(size);
final codes = s.toUint8List();
return codesToString(codes);
}