3
3
4
4
@TestOn ('browser' )
5
5
6
+ import 'dart:async' ;
6
7
import 'dart:convert' ;
7
8
import 'dart:html' as html;
8
9
import 'dart:typed_data' ;
9
10
10
- import 'package:async/async.dart' ;
11
11
import 'package:aws_common/aws_common.dart' ;
12
12
import 'package:aws_common/web.dart' ;
13
13
import 'package:test/test.dart' ;
@@ -19,6 +19,7 @@ void main() {
19
19
const testStringContent = 'I ❤️ Amplify, œ 小新' ;
20
20
const testContentType = 'text/plain' ;
21
21
final testBytes = utf8.encode (testStringContent);
22
+ final testBytesUtf16 = testStringContent.codeUnits;
22
23
final testBlob = html.Blob ([testBytes], testContentType);
23
24
final testFile = html.File (
24
25
[testBlob],
@@ -183,6 +184,14 @@ void main() {
183
184
expect (bytesBuffer.takeBytes (), equals (testBytes));
184
185
});
185
186
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
+
186
195
test (
187
196
'returns streams over the underlying file pointed by the path' ,
188
197
() async {
@@ -205,3 +214,21 @@ void main() {
205
214
});
206
215
});
207
216
}
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