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:io'; | 9 import 'dart:io'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
11 | 11 |
12 import 'utils.dart'; | 12 import 'utils.dart'; |
13 | 13 |
14 /// A stream of chunks of bytes representing a single piece of data. | 14 /// A stream of chunks of bytes representing a single piece of data. |
15 class ByteStream extends StreamView<List<int>> { | 15 class ByteStream extends StreamView<List<int>> { |
16 ByteStream(Stream<List<int>> stream) | 16 ByteStream(Stream<List<int>> stream) |
17 : super(stream); | 17 : super(stream); |
18 | 18 |
19 /// Returns a single-subscription byte stream that will emit the given bytes | 19 /// Returns a single-subscription byte stream that will emit the given bytes |
20 /// in a single chunk. | 20 /// in a single chunk. |
21 factory ByteStream.fromBytes(List<int> bytes) => | 21 factory ByteStream.fromBytes(List<int> bytes) => |
22 new ByteStream(streamFromIterable([bytes])); | 22 new ByteStream(streamFromIterable([bytes])); |
23 | 23 |
24 /// Collects the data of this stream in a [Uint8List]. | 24 /// Collects the data of this stream in a [Uint8List]. |
25 Future<Uint8List> toBytes() { | 25 Future<Uint8List> toBytes() { |
26 /// TODO(nweiz): use BufferList when issue 6409 is fixed. | 26 return fold(new BytesBuilder(), (builder, chunk) => builder..add(chunk)) |
27 return fold(<int>[], (buffer, chunk) { | 27 .then((builder) => builder.takeBytes()); |
nweiz
2013/11/01 18:31:19
Indent this two more spaces.
Anders Johnsen
2013/11/04 07:13:34
Done: https://codereview.chromium.org/57653002
| |
28 buffer.addAll(chunk); | |
29 return buffer; | |
30 }).then(toUint8List); | |
31 } | 28 } |
32 | 29 |
33 /// Collect the data of this stream in a [String], decoded according to | 30 /// Collect the data of this stream in a [String], decoded according to |
34 /// [encoding], which defaults to `UTF8`. | 31 /// [encoding], which defaults to `UTF8`. |
35 Future<String> bytesToString([Encoding encoding=UTF8]) => | 32 Future<String> bytesToString([Encoding encoding=UTF8]) => |
36 toBytes().then((bytes) => encoding.decode(bytes)); | 33 toBytes().then((bytes) => encoding.decode(bytes)); |
37 | 34 |
38 Stream<String> toStringStream([Encoding encoding=UTF8]) => | 35 Stream<String> toStringStream([Encoding encoding=UTF8]) => |
39 transform(encoding.decoder); | 36 transform(encoding.decoder); |
40 } | 37 } |
OLD | NEW |