apply optimization to Stream as well
diff --git a/lib/src/body.dart b/lib/src/body.dart
index dbd3873..cb38a63 100644
--- a/lib/src/body.dart
+++ b/lib/src/body.dart
@@ -59,6 +59,9 @@
} else if (body is List) {
contentLength = body.length;
stream = Stream.value(body.cast());
+ } else if (body is Stream<List<int>>) {
+ // Avoid performance overhead from an unnecessary cast.
+ stream = body;
} else if (body is Stream) {
stream = body.cast();
} else {
diff --git a/test/response_test.dart b/test/response_test.dart
index 794ed67..e46f783 100644
--- a/test/response_test.dart
+++ b/test/response_test.dart
@@ -42,15 +42,20 @@
expect(await response.read().single, same(bytes));
});
+ test('supports a Stream<List<int>> body without copying', () async {
+ var bytes = Stream.value(<int>[1, 2, 3, 4]);
+ var response = Response.ok(bytes);
+
+ expect(response.read(), same(bytes));
+ });
+
test('Copies a dynamic list of int elements', () async {
var bytes = <dynamic>[1, 2, 3, 4];
var response = Response.ok(bytes);
expect(response.contentLength, 4);
- expect(
- await response.read().single,
- isA<List<int>>()
- .having((List<int> values) => values, 'values', [1, 2, 3, 4]));
+ expect(await response.read().single,
+ isA<List<int>>().having((values) => values, 'values', [1, 2, 3, 4]));
});
group('new Response.internalServerError without a body', () {