readString method

String readString({
  1. bool explicitLength = true,
})

Read a string encoded into the stream. Strings are encoded with a varuint integer length written first followed by length number of utf8 encoded bytes.

Implementation

String readString({bool explicitLength = true}) {
  int length = explicitLength ? readVarUint() : buffer.lengthInBytes;
  late Uint8List bytes;
  if (kIsWeb) {
    // This is to workaround SharedArrayBuffer no longer working with methods
    // like Uint8List.view. Details here:
    // https://dart-review.googlesource.com/c/sdk/+/343940/12/sdk/lib/js_interop/js_interop.dart
    // and here https://github.com/dart-lang/sdk/issues/56455
    bytes = Uint8List(length);
    for (int index = 0; index < length; index++) {
      bytes[index] = buffer.getUint8(readIndex++);
    }
  } else {
    bytes = Uint8List.view(
        buffer.buffer, buffer.offsetInBytes + readIndex, length);
    readIndex += length;
  }
  return _utf8Decoder.convert(bytes);
}