readAsString function

Future<String> readAsString(
  1. Stream<List<int>> stream, {
  2. Encoding encoding = utf8,
  3. int? maxLength,
})

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);
}