Skip to content

Commit 6f29cee

Browse files
fix: properly handle 16-bit code units (#4717)
1 parent 39c136b commit 6f29cee

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

packages/aws_common/lib/src/io/aws_file_platform_html.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AWSFilePlatform extends AWSFile {
6363
super.name,
6464
super.contentType,
6565
}) : _stream = null,
66-
_inputBlob = Blob([data], contentType),
66+
_inputBlob = null,
6767
_inputFile = null,
6868
_size = data.length,
6969
super.protected(

packages/aws_common/test/io/aws_file_html_test.dart

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
@TestOn('browser')
55

6+
import 'dart:async';
67
import 'dart:convert';
78
import 'dart:html' as html;
89
import 'dart:typed_data';
910

10-
import 'package:async/async.dart';
1111
import 'package:aws_common/aws_common.dart';
1212
import 'package:aws_common/web.dart';
1313
import 'package:test/test.dart';
@@ -19,6 +19,7 @@ void main() {
1919
const testStringContent = 'I ❤️ Amplify, œ 小新';
2020
const testContentType = 'text/plain';
2121
final testBytes = utf8.encode(testStringContent);
22+
final testBytesUtf16 = testStringContent.codeUnits;
2223
final testBlob = html.Blob([testBytes], testContentType);
2324
final testFile = html.File(
2425
[testBlob],
@@ -183,6 +184,14 @@ void main() {
183184
expect(bytesBuffer.takeBytes(), equals(testBytes));
184185
});
185186

187+
test('returns streams over the underlying bytes (utf16)', () async {
188+
final awsFile = AWSFile.fromData(testBytesUtf16);
189+
190+
final collectedBytes = await collectBytes(awsFile.openRead());
191+
192+
expect(collectedBytes, equals(testBytesUtf16));
193+
});
194+
186195
test(
187196
'returns streams over the underlying file pointed by the path',
188197
() async {
@@ -205,3 +214,21 @@ void main() {
205214
});
206215
});
207216
}
217+
218+
/// Collects an asynchronous sequence of byte lists into a single list of bytes.
219+
///
220+
/// Similar to collectBytes from package:async, but works for any List<int>,
221+
/// where as collectBytes from package:async only supports [Uint8List].
222+
Future<List<int>> collectBytes(Stream<List<int>> source) {
223+
final bytes = <int>[];
224+
final completer = Completer<List<int>>.sync();
225+
source.listen(
226+
bytes.addAll,
227+
onError: completer.completeError,
228+
onDone: () {
229+
completer.complete(bytes);
230+
},
231+
cancelOnError: true,
232+
);
233+
return completer.future;
234+
}

0 commit comments

Comments
 (0)