buffer method

Future<void> buffer()

Reads the request stream and buffers its bytes in memory.

This is typically called early to prevent repeated reads of the request stream and avoid "Stream already listened" errors.

If buffering fails, an empty buffer is stored.

Example:

await form.buffer();

Implementation

Future<void> buffer() async {
  if (_bodyBuffer != null) return;

  try {
    final bytes = await _request.fold<BytesBuilder>(
      BytesBuilder(copy: false),
      (builder, chunk) => builder..add(chunk),
    );
    _bodyBuffer = bytes.takeBytes();
  } catch (e) {
    // print("Error buffering request stream: $e");
    _bodyBuffer = Uint8List(0);
  }
}