readAsString function
Reads the entire stream as a string using the given Encoding.
maxLengththe maximal allowed length. If omitted, no limitation at all. If specified and the input is more than allowed, PayloadException will be thrown.
Implementation
Future<String> readAsString(Stream<List<int>> stream,
{Encoding encoding = utf8, int? maxLength}) async {
final result = BytesBuilder(copy: false);
await for (final chunk in stream) {
int newlen;
if (maxLength != null
&& (newlen = chunk.length + result.length) > maxLength)
throw PayloadException("Over $maxLength ($newlen)");
result.add(chunk);
}
return encoding.decode(result.takeBytes());
}