Skip to content

Commit 40f0b93

Browse files
authored
[cross_file] fixed readAsString decoding in-memory bytes as UTF-16 (flutter#12479)
`XFile.fromData(utf8.encode('😀')).readAsString()` hands back mojibake The bytes branch uses `String.fromCharCodes` which just widens each byte into its own UTF-16 code unit so anything outside ASCII comes out mangled and the `encoding` parameter the method accepts never gets used at all. The file-backed branch right below it passes `encoding` through fine, and web already does `readAsBytes().then(encoding.decode)` so this is mostly just making native do what web has been doing all along. I made the method `async` while I was in there and that bit is deliberate rather than cosmetic: `encoding.decode` can throw on bad input where `fromCharCodes` never could and without `async` that would come out synchronously instead of as a failed future, which isn't what you get from either the web version or the `_file.readAsString` path. fixes flutter#165120 AI usgae: I used antigravity (with gemini 3.7 flash)
1 parent 9f595f3 commit 40f0b93

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎packages/cross_file/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.3.5+5
2+
3+
* Fixes native `readAsString` returning mojibake for `XFile.fromData`, which
4+
decoded the bytes as UTF-16 code units instead of using the `encoding`
5+
argument.
6+
17
## 0.3.5+4
28

39
* Adds a runnable `main` entry point and an additional `XFile.fromData`

‎packages/cross_file/lib/src/types/io.dart‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ class XFile extends XFileBase {
112112

113113
@override
114114
Future<String> readAsString({Encoding encoding = utf8}) {
115-
if (_bytes != null) {
116-
// TODO(kevmoo): Remove ignore and fix when the MIN Dart SDK is 3.3
117-
// ignore: unnecessary_non_null_assertion
118-
return Future<String>.value(String.fromCharCodes(_bytes!));
115+
final Uint8List? bytes = _bytes;
116+
if (bytes != null) {
117+
return Future<String>.sync(() => encoding.decode(bytes));
119118
}
120119
return _file.readAsString(encoding: encoding);
121120
}

‎packages/cross_file/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: cross_file
22
description: An abstraction to allow working with files across multiple platforms.
33
repository: https://github.com/flutter/packages/tree/main/packages/cross_file
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+cross_file%22
5-
version: 0.3.5+4
5+
version: 0.3.5+5
66

77
environment:
88
sdk: ^3.10.0

‎packages/cross_file/test/x_file_io_test.dart‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ void main() {
8282
test('Can be read as a string', () async {
8383
expect(await file.readAsString(), equals(expectedStringContents));
8484
});
85+
86+
test('Can be read as a string with multi-byte characters', () async {
87+
const contents = 'Hello, world! I ❤ ñ! 空手 😀';
88+
final multiByteFile = XFile.fromData(utf8.encode(contents));
89+
expect(await multiByteFile.readAsString(), equals(contents));
90+
});
91+
92+
test('Can be read as a string with a non-default encoding', () async {
93+
const contents = 'Une soirée à Genève';
94+
final latin1File = XFile.fromData(latin1.encode(contents));
95+
expect(await latin1File.readAsString(encoding: latin1), equals(contents));
96+
});
97+
98+
test('Reading undecodable data fails the future', () async {
99+
final malformedFile = XFile.fromData(Uint8List.fromList(<int>[0xc3, 0x28]));
100+
await expectLater(malformedFile.readAsString(), throwsA(isA<FormatException>()));
101+
});
102+
85103
test('Can be read as bytes', () async {
86104
expect(await file.readAsBytes(), equals(bytes));
87105
});

0 commit comments

Comments
 (0)