Line data Source code
1 : // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 2 : // for details. All rights reserved. Use of this source code is governed by a 3 : // BSD-style license that can be found in the LICENSE file. 4 : 5 : import 'dart:async'; 6 : import 'dart:typed_data'; 7 : import 'cancelable_operation.dart'; 8 : 9 : /// Collects an asynchronous sequence of byte lists into a single list of bytes. 10 : /// 11 : /// If the [source] stream emits an error event, 12 : /// the collection fails and the returned future completes with the same error. 13 : /// 14 : /// If any of the input data are not valid bytes, they will be truncated to 15 : /// an eight-bit unsigned value in the resulting list. 16 0 : Future<Uint8List> collectBytes(Stream<List<int>> source) { 17 0 : return _collectBytes(source, (_, result) => result); 18 : } 19 : 20 : /// Collects an asynchronous sequence of byte lists into a single list of bytes. 21 : /// 22 : /// Returns a [CancelableOperation] that provides the result future and a way 23 : /// to cancel the collection early. 24 : /// 25 : /// If the [source] stream emits an error event, 26 : /// the collection fails and the returned future completes with the same error. 27 : /// 28 : /// If any of the input data are not valid bytes, they will be truncated to 29 : /// an eight-bit unsigned value in the resulting list. 30 0 : CancelableOperation<Uint8List> collectBytesCancelable( 31 : Stream<List<int>> source) { 32 0 : return _collectBytes( 33 : source, 34 0 : (subscription, result) => CancelableOperation.fromFuture(result, 35 0 : onCancel: subscription.cancel)); 36 : } 37 : 38 : /// Generalization over [collectBytes] and [collectBytesCancelable]. 39 : /// 40 : /// Performs all the same operations, but the final result is created 41 : /// by the [result] function, which has access to the stream subscription 42 : /// so it can cancel the operation. 43 0 : T _collectBytes<T>(Stream<List<int>> source, 44 : T Function(StreamSubscription<List<int>>, Future<Uint8List>) result) { 45 0 : var byteLists = <List<int>>[]; 46 : var length = 0; 47 0 : var completer = Completer<Uint8List>.sync(); 48 0 : var subscription = source.listen( 49 0 : (bytes) { 50 0 : byteLists.add(bytes); 51 0 : length += bytes.length; 52 : }, 53 0 : onError: completer.completeError, 54 0 : onDone: () { 55 0 : completer.complete(_collect(length, byteLists)); 56 : }, 57 : cancelOnError: true); 58 0 : return result(subscription, completer.future); 59 : } 60 : 61 : // Join a lists of bytes with a known total length into a single [Uint8List]. 62 0 : Uint8List _collect(int length, List<List<int>> byteLists) { 63 0 : var result = Uint8List(length); 64 : var i = 0; 65 0 : for (var byteList in byteLists) { 66 0 : var end = i + byteList.length; 67 0 : result.setRange(i, end, byteList); 68 : i = end; 69 : } 70 : return result; 71 : }