Skip to content

Commit

Permalink
Avoid unnecessary Uint8List instantiations
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal committed Nov 14, 2023
1 parent c74941f commit 33ddefe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion dio/lib/src/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class ResponseBody {
this.statusMessage,
this.isRedirect = false,
Map<String, List<String>>? headers,
}) : stream = Stream.value(Uint8List.fromList(bytes)),
}) : stream = Stream.value(
bytes is Uint8List ? bytes : Uint8List.fromList(bytes),
),
headers = headers ?? {};

/// Whether this response is a redirect.
Expand Down
7 changes: 5 additions & 2 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ class IOHttpClientAdapter implements HttpClientAdapter {
}

int dataLength = 0;
final stream = responseStream.transform<Uint8List>(

/// Casting the stream to Uint8List is safe due to [_HttpClientResponse]
/// working on a Uint8List.
final stream = responseStream.cast<Uint8List>().transform<Uint8List>(
StreamTransformer.fromHandlers(
handleData: (data, sink) {
if (cancellation != null) {
Expand All @@ -226,7 +229,7 @@ class IOHttpClientAdapter implements HttpClientAdapter {
dataLength += data.length,
responseStream.contentLength,
);
sink.add(Uint8List.fromList(data));
sink.add(data);
},
),
);
Expand Down

0 comments on commit 33ddefe

Please sign in to comment.