readString method

  1. @override
String readString({
  1. int? size,
  2. bool utf8 = true,
})
override

Read a null-terminated string, or if len is provided, that number of bytes returned as a string.

Implementation

@override
String readString({int? size, bool utf8 = true}) {
  if (size == null) {
    final codes = <int>[];
    if (isEOS) {
      return '';
    }
    while (!isEOS) {
      final c = readByte();
      if (c == 0) {
        break;
      }
      codes.add(c);
    }
    return utf8 ? Utf8Decoder().convert(codes) : String.fromCharCodes(codes);
  }

  final s = readBytes(size);
  final bytes = s.toUint8List();
  try {
    final str =
        utf8 ? Utf8Decoder().convert(bytes) : String.fromCharCodes(bytes);
    return str;
  } catch (err) {
    // If the string is not a valid UTF8 string, decode it as character codes.
    return String.fromCharCodes(bytes);
  }
}