nextString method

String nextString({
  1. Encoding encoding = utf8,
})

Extracts an arbitrary length UTF-8 string from the range.

Extracts an uint32 which indicates the number of following bytes that make up the string value. The string does not have to be, and probably isn't, null terminated.

Returns the string value obtained from interpreting the bytes using the encoding. The bytes making up the length and the string are removed from the range.

This method is similar to nextBinary, except the contents are interpreted as UTF-8 and returned as a String, instead of a range of binary bytes.

Throws a KeyBad if there are insufficient bytes in the range. Throws an exception if the bytes are not correct for the encoding.

Implementation

String nextString({Encoding encoding = utf8}) {
  final length = nextUint32();

  if (_end < _begin + length) {
    throw KeyBad('data incomplete (for $length byte string)');
  }

  final rawString = _bytes.sublist(_begin, _begin + length);
  _begin += length;

  return encoding.decode(rawString);
}