read method

Future<List<int>> read(
  1. int count
)

Reads a specific number of bytes from the stream.

count the number of bytes to read

Returns a Future that completes with the requested bytes, or fewer if stream ends

Implementation

Future<List<int>> read(int count) async {
  List<int> result = [];
  int remaining = count;

  await for (List<int> chunk in _stream) {
    if (remaining <= 0) break;

    if (chunk.length <= remaining) {
      result.addAll(chunk);
      remaining -= chunk.length;
    } else {
      result.addAll(chunk.take(remaining));
      remaining = 0;
    }
  }

  return result;
}