Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: sdk/lib/convert/byte_conversion.dart

Issue 203603008: Introduce class Sink<T>. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/convert/ascii.dart ('k') | sdk/lib/convert/chunked_conversion.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.convert; 5 part of dart.convert;
6 6
7 /** 7 /**
8 * The [ByteConversionSink] provides an interface for converters to 8 * The [ByteConversionSink] provides an interface for converters to
9 * efficiently transmit byte data. 9 * efficiently transmit byte data.
10 * 10 *
11 * Instead of limiting the interface to one non-chunked list of bytes it 11 * Instead of limiting the interface to one non-chunked list of bytes it
12 * accepts its input in chunks (themselves being lists of bytes). 12 * accepts its input in chunks (themselves being lists of bytes).
13 * 13 *
14 * This abstract class will likely get more methods over time. Implementers are 14 * This abstract class will likely get more methods over time. Implementers are
15 * urged to extend or mix in [ByteConversionSinkBase] to ensure that their 15 * urged to extend or mix in [ByteConversionSinkBase] to ensure that their
16 * class covers the newly added methods. 16 * class covers the newly added methods.
17 */ 17 */
18 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> { 18 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> {
19 ByteConversionSink(); 19 ByteConversionSink();
20 factory ByteConversionSink.withCallback(void callback(List<int> accumulated)) 20 factory ByteConversionSink.withCallback(void callback(List<int> accumulated))
21 = _ByteCallbackSink; 21 = _ByteCallbackSink;
22 factory ByteConversionSink.from(ChunkedConversionSink<List<int>> sink) 22 factory ByteConversionSink.from(Sink<List<int>> sink)
23 = _ByteAdapterSink; 23 = _ByteAdapterSink;
24 24
25 /** 25 /**
26 * Adds the next [chunk] to `this`. 26 * Adds the next [chunk] to `this`.
27 * 27 *
28 * Adds the bytes defined by [start] and [end]-exclusive to `this`. 28 * Adds the bytes defined by [start] and [end]-exclusive to `this`.
29 * 29 *
30 * If [isLast] is `true` closes `this`. 30 * If [isLast] is `true` closes `this`.
31 * 31 *
32 * Contrary to `add` the given [chunk] must not be held onto. Once the method 32 * Contrary to `add` the given [chunk] must not be held onto. Once the method
(...skipping 14 matching lines...) Expand all
47 void add(List<int> chunk); 47 void add(List<int> chunk);
48 void close(); 48 void close();
49 49
50 void addSlice(List<int> chunk, int start, int end, bool isLast) { 50 void addSlice(List<int> chunk, int start, int end, bool isLast) {
51 add(chunk.sublist(start, end)); 51 add(chunk.sublist(start, end));
52 if (isLast) close(); 52 if (isLast) close();
53 } 53 }
54 } 54 }
55 55
56 /** 56 /**
57 * This class adapts a simple [ChunkedConversionSink] to a [ByteConversionSink]. 57 * This class adapts a simple [Sink] to a [ByteConversionSink].
58 * 58 *
59 * All additional methods of the [ByteConversionSink] (compared to the 59 * All additional methods of the [ByteConversionSink] (compared to the
60 * ChunkedConversionSink) are redirected to the `add` method. 60 * ChunkedConversionSink) are redirected to the `add` method.
61 */ 61 */
62 class _ByteAdapterSink extends ByteConversionSinkBase { 62 class _ByteAdapterSink extends ByteConversionSinkBase {
63 final ChunkedConversionSink<List<int>> _sink; 63 final Sink<List<int>> _sink;
64 64
65 _ByteAdapterSink(this._sink); 65 _ByteAdapterSink(this._sink);
66 66
67 void add(List<int> chunk) => _sink.add(chunk); 67 void add(List<int> chunk) => _sink.add(chunk);
68 void close() => _sink.close(); 68 void close() => _sink.close();
69 } 69 }
70 70
71 /** 71 /**
72 * This class accumulates all chunks into one list of bytes 72 * This class accumulates all chunks into one list of bytes
73 * and invokes a callback when the sink is closed. 73 * and invokes a callback when the sink is closed.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 v |= v >> 8; 109 v |= v >> 8;
110 v |= v >> 16; 110 v |= v >> 16;
111 v++; 111 v++;
112 return v; 112 return v;
113 } 113 }
114 114
115 void close() { 115 void close() {
116 _callback(_buffer.sublist(0, _bufferIndex)); 116 _callback(_buffer.sublist(0, _bufferIndex));
117 } 117 }
118 } 118 }
OLDNEW
« no previous file with comments | « sdk/lib/convert/ascii.dart ('k') | sdk/lib/convert/chunked_conversion.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698