addStream method

  1. @override
Future addStream(
  1. Stream<List<int>> stream
)
override

Adds a stream directly the underlying response.

If this instance has access to a correspondingRequest, then it will attempt to transform the content using at most one of the response encoders.

Implementation

@override
Future addStream(Stream<List<int>> stream) async {
  if (_isClosed && isBuffered) throw ResponseContext.closed();
  if (isBuffered) {
    // A buffered response is sent as a whole by the driver; writing the
    // stream to the socket here would leave the buffer empty.
    await stream.forEach(buffer!.add);
    return;
  }
  await _commitOrThrow();

  var sink = _encoderSink;
  if (sink == null) return rawResponse.addStream(stream);

  // Feed the shared encoder, flushing after each chunk so a slow client
  // applies backpressure instead of the whole stream being buffered.
  await for (var chunk in stream) {
    sink.add(chunk);
    await rawResponse.flush();
  }
}