diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index 7ade7f8569..238467b27d 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -57,11 +57,10 @@ abstract class AssetBundle { /// Throws an exception if the asset is not found. /// /// The returned [ByteData] can be converted to a [Uint8List] (a list of bytes) - /// using [ByteData.buffer] to obtain a [ByteBuffer], and then - /// [ByteBuffer.asUint8List] to obtain the byte list. Lists of bytes can be - /// used with APIs that accept [Uint8List] objects, such as - /// [decodeImageFromList], as well as any API that accepts a [List], such - /// as [File.writeAsBytes] or [Utf8Codec.decode] (accessible via [utf8]). + /// using [Uint8List.sublistView]. Lists of bytes can be used with APIs that + /// accept [Uint8List] objects, such as [decodeImageFromList], as well as any + /// API that accepts a [List], such as [File.writeAsBytes] or + /// [Utf8Codec.decode] (accessible via [utf8]). Future load(String key); /// Retrieve a binary resource from the asset bundle as an immutable @@ -70,7 +69,7 @@ abstract class AssetBundle { /// Throws an exception if the asset is not found. Future loadBuffer(String key) async { final ByteData data = await load(key); - return ui.ImmutableBuffer.fromUint8List(data.buffer.asUint8List()); + return ui.ImmutableBuffer.fromUint8List(Uint8List.sublistView(data)); } /// Retrieve a string from the asset bundle. @@ -91,7 +90,7 @@ abstract class AssetBundle { // 50 KB of data should take 2-3 ms to parse on a Moto G4, and about 400 μs // on a Pixel 4. if (data.lengthInBytes < 50 * 1024) { - return utf8.decode(data.buffer.asUint8List()); + return utf8.decode(Uint8List.sublistView(data)); } // For strings larger than 50 KB, run the computation in an isolate to // avoid causing main thread jank. @@ -99,7 +98,7 @@ abstract class AssetBundle { } static String _utf8decode(ByteData data) { - return utf8.decode(data.buffer.asUint8List()); + return utf8.decode(Uint8List.sublistView(data)); } /// Retrieve a string from the asset bundle, parse it with the given function, @@ -161,7 +160,7 @@ class NetworkAssetBundle extends AssetBundle { ]); } final Uint8List bytes = await consolidateHttpClientResponseBytes(response); - return bytes.buffer.asByteData(); + return ByteData.sublistView(bytes); } // TODO(ianh): Once the underlying network logic learns about caching, we @@ -308,7 +307,7 @@ abstract class CachingAssetBundle extends AssetBundle { @override Future loadBuffer(String key) async { final ByteData data = await load(key); - return ui.ImmutableBuffer.fromUint8List(data.buffer.asUint8List()); + return ui.ImmutableBuffer.fromUint8List(Uint8List.sublistView(data)); } } @@ -316,10 +315,10 @@ abstract class CachingAssetBundle extends AssetBundle { class PlatformAssetBundle extends CachingAssetBundle { @override Future load(String key) { - final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path); + final Uint8List encoded = utf8.encode(Uri(path: Uri.encodeFull(key)).path); final Future? future = ServicesBinding.instance.defaultBinaryMessenger.send( 'flutter/assets', - encoded.buffer.asByteData(), + ByteData.sublistView(encoded), )?.then((ByteData? asset) { if (asset == null) { throw FlutterError.fromParts([ @@ -342,7 +341,7 @@ class PlatformAssetBundle extends CachingAssetBundle { Future loadBuffer(String key) async { if (kIsWeb) { final ByteData bytes = await load(key); - return ui.ImmutableBuffer.fromUint8List(bytes.buffer.asUint8List()); + return ui.ImmutableBuffer.fromUint8List(Uint8List.sublistView(bytes)); } bool debugUsePlatformChannel = false; assert(() { @@ -358,7 +357,7 @@ class PlatformAssetBundle extends CachingAssetBundle { }()); if (debugUsePlatformChannel) { final ByteData bytes = await load(key); - return ui.ImmutableBuffer.fromUint8List(bytes.buffer.asUint8List()); + return ui.ImmutableBuffer.fromUint8List(Uint8List.sublistView(bytes)); } try { return await ui.ImmutableBuffer.fromAsset(key); diff --git a/packages/flutter/lib/src/services/message_codecs.dart b/packages/flutter/lib/src/services/message_codecs.dart index fcd90731ca..2b103d9624 100644 --- a/packages/flutter/lib/src/services/message_codecs.dart +++ b/packages/flutter/lib/src/services/message_codecs.dart @@ -50,7 +50,7 @@ class StringCodec implements MessageCodec { if (message == null) { return null; } - return utf8.decoder.convert(message.buffer.asUint8List(message.offsetInBytes, message.lengthInBytes)); + return utf8.decode(Uint8List.sublistView(message)); } @override @@ -58,8 +58,7 @@ class StringCodec implements MessageCodec { if (message == null) { return null; } - final Uint8List encoded = utf8.encoder.convert(message); - return encoded.buffer.asByteData(); + return ByteData.sublistView(utf8.encode(message)); } } @@ -415,7 +414,7 @@ class StandardMessageCodec implements MessageCodec { if (char <= 0x7f) { asciiBytes[i] = char; } else { - utf8Bytes = utf8.encoder.convert(value.substring(i)); + utf8Bytes = utf8.encode(value.substring(i)); utf8Offset = i; break; } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index a6c55db080..eab287bddb 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -6176,7 +6176,7 @@ class RawImage extends LeafRenderObjectWidget { /// @override /// Future load(String key) async { /// if (key == 'resources/test') { -/// return ByteData.view(Uint8List.fromList(utf8.encode('Hello World!')).buffer); +/// return ByteData.sublistView(utf8.encode('Hello World!')); /// } /// return ByteData(0); /// } diff --git a/packages/flutter/test/services/asset_bundle_test.dart b/packages/flutter/test/services/asset_bundle_test.dart index 5f7b7c738c..c6946425d2 100644 --- a/packages/flutter/test/services/asset_bundle_test.dart +++ b/packages/flutter/test/services/asset_bundle_test.dart @@ -16,17 +16,16 @@ class TestAssetBundle extends CachingAssetBundle { Future load(String key) async { loadCallCount[key] = (loadCallCount[key] ?? 0) + 1; if (key == 'AssetManifest.json') { - return ByteData.view(Uint8List.fromList(const Utf8Encoder().convert('{"one": ["one"]}')).buffer); + return ByteData.sublistView(utf8.encode('{"one": ["one"]}')); } if (key == 'AssetManifest.bin') { - return const StandardMessageCodec().encodeMessage({ - 'one': [] - })!; + return const StandardMessageCodec() + .encodeMessage({'one': []})!; } if (key == 'counter') { - return ByteData.view(Uint8List.fromList(const Utf8Encoder().convert(loadCallCount[key]!.toString())).buffer); + return ByteData.sublistView(utf8.encode(loadCallCount[key]!.toString())); } if (key == 'one') { diff --git a/packages/flutter/test/services/binding_test.dart b/packages/flutter/test/services/binding_test.dart index 1aff214478..3f3439e62a 100644 --- a/packages/flutter/test/services/binding_test.dart +++ b/packages/flutter/test/services/binding_test.dart @@ -89,7 +89,7 @@ void main() { int flutterAssetsCallCount = 0; binding.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async { flutterAssetsCallCount += 1; - return Uint8List.fromList('test_asset_data'.codeUnits).buffer.asByteData(); + return ByteData.sublistView(utf8.encode('test_asset_data')); }); await rootBundle.loadString('test_asset'); diff --git a/packages/flutter/test/services/channel_buffers_test.dart b/packages/flutter/test/services/channel_buffers_test.dart index 09eeec03ea..2c48f10cd3 100644 --- a/packages/flutter/test/services/channel_buffers_test.dart +++ b/packages/flutter/test/services/channel_buffers_test.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'dart:convert'; -import 'dart:typed_data'; import 'dart:ui' as ui; import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; @@ -14,15 +13,11 @@ class TestChannelBuffersFlutterBinding extends BindingBase with SchedulerBinding void main() { ByteData makeByteData(String str) { - final List list = utf8.encode(str); - final ByteBuffer buffer = list is Uint8List ? list.buffer : Uint8List.fromList(list).buffer; - return ByteData.view(buffer); + return ByteData.sublistView(utf8.encode(str)); } String getString(ByteData data) { - final ByteBuffer buffer = data.buffer; - final List list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); - return utf8.decode(list); + return utf8.decode(Uint8List.sublistView(data)); } test('does drain channel buffers', () async { diff --git a/packages/flutter/test/services/default_binary_messenger_test.dart b/packages/flutter/test/services/default_binary_messenger_test.dart index 690493d46a..72702f5208 100644 --- a/packages/flutter/test/services/default_binary_messenger_test.dart +++ b/packages/flutter/test/services/default_binary_messenger_test.dart @@ -4,7 +4,6 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:typed_data'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -13,10 +12,7 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); ByteData makeByteData(String str) { - final List list = utf8.encode(str); - final ByteBuffer buffer = - list is Uint8List ? list.buffer : Uint8List.fromList(list).buffer; - return ByteData.view(buffer); + return ByteData.sublistView(utf8.encode(str)); } test('default binary messenger calls callback once', () async {