OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library byte_stream; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 import 'dart:scalarlist'; | |
10 | |
11 import 'utils.dart'; | |
12 | |
13 /// A stream of bytes representing a single chunked piece of data. | |
Bob Nystrom
2013/01/08 23:50:49
Clarify that it's a stream of chunks of bytes, and
nweiz
2013/01/09 00:52:11
Done.
| |
14 class ByteStream extends StreamView<List<int>> { | |
15 static var _id = 0; | |
16 var id = _id++; | |
Bob Nystrom
2013/01/08 23:50:49
Debug code?
nweiz
2013/01/09 00:52:11
Oops, removed.
| |
17 | |
18 ByteStream(Stream<List<int>> stream) | |
19 : super(stream); | |
20 | |
21 /// Returns a single-subscription byte stream that will emit the given bytes | |
22 /// in a single chunk. | |
23 factory ByteStream.fromBytes(List<int> bytes) => | |
24 new ByteStream(streamFromIterable([bytes])); | |
25 | |
26 /// Collects the data of this stream in a [Uint8List]. | |
27 Future<Uint8List> toBytes() { | |
28 /// TODO(nweiz): use BufferList when issue 6409 is fixed. | |
29 return reduce(<int>[], (buffer, chunk) { | |
30 buffer.addAll(chunk); | |
31 return buffer; | |
32 }).then(toUint8List); | |
33 } | |
34 | |
35 /// Collect the data of this stream in a [String], decoded according to | |
36 /// [encoding], which defaults to `Encoding.UTF_8`. | |
37 Future<String> bytesToString([Encoding encoding=Encoding.UTF_8]) => | |
38 toBytes().then((bytes) => decodeString(bytes, encoding)); | |
39 } | |
OLD | NEW |