| OLD | NEW | 
|   1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file |   1 // Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file | 
|   2 // for details. All rights reserved. Use of this source code is governed by a |   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. |   3 // BSD-style license that can be found in the LICENSE file. | 
|   4  |   4  | 
|   5 library byte_stream; |   5 library byte_stream; | 
|   6  |   6  | 
|   7 import 'dart:async'; |   7 import 'dart:async'; | 
|   8 import 'dart:convert'; |   8 import 'dart:convert'; | 
|   9 import 'dart:typed_data'; |   9 import 'dart:typed_data'; | 
|  10  |  10  | 
|  11 import 'utils.dart'; |  11 import 'utils.dart'; | 
|  12  |  12  | 
|  13 /// A stream of chunks of bytes representing a single piece of data. |  13 /// A stream of chunks of bytes representing a single piece of data. | 
|  14 class ByteStream extends StreamView<List<int>> { |  14 class ByteStream extends StreamView<List<int>> { | 
|  15   ByteStream(Stream<List<int>> stream) |  15   ByteStream(Stream<List<int>> stream) | 
|  16       : super(stream); |  16       : super(stream); | 
|  17  |  17  | 
|  18   /// Returns a single-subscription byte stream that will emit the given bytes |  18   /// Returns a single-subscription byte stream that will emit the given bytes | 
|  19   /// in a single chunk. |  19   /// in a single chunk. | 
|  20   factory ByteStream.fromBytes(List<int> bytes) => |  20   factory ByteStream.fromBytes(List<int> bytes) => | 
|  21       new ByteStream(streamFromIterable([bytes])); |  21       new ByteStream(streamFromIterable([bytes])); | 
|  22  |  22  | 
|  23   /// Collects the data of this stream in a [Uint8List]. |  23   /// Collects the data of this stream in a [Uint8List]. | 
|  24   Future<Uint8List> toBytes() { |  24   Future<Uint8List> toBytes() { | 
|  25     var completer = new Completer(); |  25     var completer = new Completer(); | 
|  26     var sink = new ByteConversionSink.withCallback(completer.complete); |  26     var sink = new ByteConversionSink.withCallback((bytes) => | 
 |  27         completer.complete(new Uint8List.fromList(bytes))); | 
|  27     listen(sink.add, onError: completer.completeError, onDone: sink.close, |  28     listen(sink.add, onError: completer.completeError, onDone: sink.close, | 
|  28         cancelOnError: true); |  29         cancelOnError: true); | 
|  29     return completer.future; |  30     return completer.future; | 
|  30   } |  31   } | 
|  31  |  32  | 
|  32   /// Collect the data of this stream in a [String], decoded according to |  33   /// Collect the data of this stream in a [String], decoded according to | 
|  33   /// [encoding], which defaults to `UTF8`. |  34   /// [encoding], which defaults to `UTF8`. | 
|  34   Future<String> bytesToString([Encoding encoding=UTF8]) => |  35   Future<String> bytesToString([Encoding encoding=UTF8]) => | 
|  35       encoding.decodeStream(this); |  36       encoding.decodeStream(this); | 
|  36  |  37  | 
|  37   Stream<String> toStringStream([Encoding encoding=UTF8]) => |  38   Stream<String> toStringStream([Encoding encoding=UTF8]) => | 
|  38       transform(encoding.decoder); |  39       transform(encoding.decoder); | 
|  39 } |  40 } | 
| OLD | NEW |