readAsString function
Reads the entire stream as a string using the given Encoding.
maxLength
the 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 = <int>[];
await for (final data in stream) {
if (maxLength != null && (data.length + result.length) > maxLength)
throw PayloadException("Over $maxLength (${data.length} + ${result.length})");
result.addAll(data);
}
return encoding.decode(result);
}