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 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( |
21 = _ByteCallbackSink; | 21 void callback(List<int> accumulated)) = _ByteCallbackSink; |
22 factory ByteConversionSink.from(Sink<List<int>> sink) | 22 factory ByteConversionSink.from(Sink<List<int>> sink) = _ByteAdapterSink; |
23 = _ByteAdapterSink; | |
24 | 23 |
25 /** | 24 /** |
26 * Adds the next [chunk] to `this`. | 25 * Adds the next [chunk] to `this`. |
27 * | 26 * |
28 * Adds the bytes defined by [start] and [end]-exclusive to `this`. | 27 * Adds the bytes defined by [start] and [end]-exclusive to `this`. |
29 * | 28 * |
30 * If [isLast] is `true` closes `this`. | 29 * If [isLast] is `true` closes `this`. |
31 * | 30 * |
32 * Contrary to `add` the given [chunk] must not be held onto. Once the method | 31 * Contrary to `add` the given [chunk] must not be held onto. Once the method |
33 * returns, it is safe to overwrite the data in it. | 32 * returns, it is safe to overwrite the data in it. |
34 */ | 33 */ |
35 void addSlice(List<int> chunk, int start, int end, bool isLast); | 34 void addSlice(List<int> chunk, int start, int end, bool isLast); |
36 | 35 |
37 // TODO(floitsch): add more methods: | 36 // TODO(floitsch): add more methods: |
38 // - iterateBytes. | 37 // - iterateBytes. |
39 } | 38 } |
40 | 39 |
41 /** | 40 /** |
42 * This class provides a base-class for converters that need to accept byte | 41 * This class provides a base-class for converters that need to accept byte |
43 * inputs. | 42 * inputs. |
44 */ | 43 */ |
45 abstract class ByteConversionSinkBase extends ByteConversionSink { | 44 abstract class ByteConversionSinkBase extends ByteConversionSink { |
46 | |
47 void add(List<int> chunk); | 45 void add(List<int> chunk); |
48 void close(); | 46 void close(); |
49 | 47 |
50 void addSlice(List<int> chunk, int start, int end, bool isLast) { | 48 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
51 add(chunk.sublist(start, end)); | 49 add(chunk.sublist(start, end)); |
52 if (isLast) close(); | 50 if (isLast) close(); |
53 } | 51 } |
54 } | 52 } |
55 | 53 |
56 /** | 54 /** |
57 * This class adapts a simple [Sink] to a [ByteConversionSink]. | 55 * This class adapts a simple [Sink] to a [ByteConversionSink]. |
58 * | 56 * |
59 * All additional methods of the [ByteConversionSink] (compared to the | 57 * All additional methods of the [ByteConversionSink] (compared to the |
60 * ChunkedConversionSink) are redirected to the `add` method. | 58 * ChunkedConversionSink) are redirected to the `add` method. |
61 */ | 59 */ |
62 class _ByteAdapterSink extends ByteConversionSinkBase { | 60 class _ByteAdapterSink extends ByteConversionSinkBase { |
63 final Sink<List<int>> _sink; | 61 final Sink<List<int>> _sink; |
64 | 62 |
65 _ByteAdapterSink(this._sink); | 63 _ByteAdapterSink(this._sink); |
66 | 64 |
67 void add(List<int> chunk) { _sink.add(chunk); } | 65 void add(List<int> chunk) { |
68 void close() { _sink.close(); } | 66 _sink.add(chunk); |
| 67 } |
| 68 |
| 69 void close() { |
| 70 _sink.close(); |
| 71 } |
69 } | 72 } |
70 | 73 |
71 /** | 74 /** |
72 * This class accumulates all chunks into one list of bytes | 75 * This class accumulates all chunks into one list of bytes |
73 * and invokes a callback when the sink is closed. | 76 * and invokes a callback when the sink is closed. |
74 * | 77 * |
75 * This class can be used to terminate a chunked conversion. | 78 * This class can be used to terminate a chunked conversion. |
76 */ | 79 */ |
77 class _ByteCallbackSink extends ByteConversionSinkBase { | 80 class _ByteCallbackSink extends ByteConversionSinkBase { |
78 static const _INITIAL_BUFFER_SIZE = 1024; | 81 static const _INITIAL_BUFFER_SIZE = 1024; |
(...skipping 28 matching lines...) Expand all Loading... |
107 v |= v >> 8; | 110 v |= v >> 8; |
108 v |= v >> 16; | 111 v |= v >> 16; |
109 v++; | 112 v++; |
110 return v; | 113 return v; |
111 } | 114 } |
112 | 115 |
113 void close() { | 116 void close() { |
114 _callback(_buffer.sublist(0, _bufferIndex)); | 117 _callback(_buffer.sublist(0, _bufferIndex)); |
115 } | 118 } |
116 } | 119 } |
OLD | NEW |