From a7a787840b87def9d16c10c009c548ddfcd86337 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 27 Nov 2023 07:55:09 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Check=20`Uint8List`=20befo?= =?UTF-8?q?re=20doing=20byte=20conversions=20(#2045)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prevent `Uint8List.fromList` if it already is `Uint8List`. ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [ ] I have added the required tests to prove the fix/feature I'm adding - [ ] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package --------- Signed-off-by: Alex Li Co-authored-by: Peter Leibiger --- dio/CHANGELOG.md | 1 + dio/lib/src/adapter.dart | 4 +++- dio/lib/src/adapters/browser_adapter.dart | 4 +++- plugins/native_dio_adapter/CHANGELOG.md | 1 + .../native_dio_adapter/lib/src/conversion_layer_adapter.dart | 4 +++- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 767fda138..0f6117b10 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -9,6 +9,7 @@ See the [Migration Guide][] for the complete breaking changes list.** - Allow case-sensitive header keys with the `preserveHeaderCase` flag through options. - Fix `receiveTimeout` for the `IOHttpClientAdapter`. - Fix `receiveTimeout` for the `download` method of `DioForNative`. +- Improve the stream byte conversion. ## 5.3.4 diff --git a/dio/lib/src/adapter.dart b/dio/lib/src/adapter.dart index 5b8ebd3e5..7d3b589a3 100644 --- a/dio/lib/src/adapter.dart +++ b/dio/lib/src/adapter.dart @@ -75,7 +75,9 @@ class ResponseBody { this.statusMessage, this.isRedirect = false, Map>? 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. diff --git a/dio/lib/src/adapters/browser_adapter.dart b/dio/lib/src/adapters/browser_adapter.dart index cae27d089..b388e42ec 100644 --- a/dio/lib/src/adapters/browser_adapter.dart +++ b/dio/lib/src/adapters/browser_adapter.dart @@ -278,7 +278,9 @@ class BrowserHttpClientAdapter implements HttpClientAdapter { } final completer = Completer(); final sink = ByteConversionSink.withCallback( - (bytes) => completer.complete(Uint8List.fromList(bytes)), + (bytes) => completer.complete( + bytes is Uint8List ? bytes : Uint8List.fromList(bytes), + ), ); requestStream.listen( sink.add, diff --git a/plugins/native_dio_adapter/CHANGELOG.md b/plugins/native_dio_adapter/CHANGELOG.md index 53d3386b3..0d16186d4 100644 --- a/plugins/native_dio_adapter/CHANGELOG.md +++ b/plugins/native_dio_adapter/CHANGELOG.md @@ -5,6 +5,7 @@ - Adds `createCronetEngine` and `createCupertinoConfiguration` to deprecate `cronetEngine` and `cupertinoConfiguration` for the `NativeAdapter`, to avoid platform exceptions. +- Improve the request stream byte conversion. ## 1.1.1 diff --git a/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart b/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart index 0e1904f0b..c153ae8fd 100644 --- a/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart +++ b/plugins/native_dio_adapter/lib/src/conversion_layer_adapter.dart @@ -56,7 +56,9 @@ class ConversionLayerAdapter implements HttpClientAdapter { if (requestStream != null) { final completer = Completer(); final sink = ByteConversionSink.withCallback( - (bytes) => completer.complete(Uint8List.fromList(bytes)), + (bytes) => completer.complete( + bytes is Uint8List ? bytes : Uint8List.fromList(bytes), + ), ); requestStream.listen( sink.add,