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 part of dart.convert; |
| 6 |
| 7 /** |
| 8 * The [ByteConversionSink] provides an interface for converters to |
| 9 * efficiently transmit byte data. |
| 10 * |
| 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). |
| 13 */ |
| 14 abstract class ByteConversionSink extends ChunkedConversionSink<List<int>> { |
| 15 ByteConversionSink(); |
| 16 factory ByteConversionSink.withCallback(void callback(List<int> accumulated)) |
| 17 = _ByteCallbackSink; |
| 18 factory ByteConversionSink.from(ChunkedConversionSink<List<int>> sink) |
| 19 = _ByteAdapterSink; |
| 20 |
| 21 /** |
| 22 * Adds the next [chunk] to `this`. |
| 23 * |
| 24 * Adds the bytes defined by [start] and [end]-exclusive to `this`. |
| 25 * |
| 26 * If [isLast] is `true` closes `this`. |
| 27 * |
| 28 * The given [chunk] is not held onto. Once the method returns, it is safe to |
| 29 * overwrite the data in it. |
| 30 */ |
| 31 void addSlice(List<int> chunk, int start, int end, bool isLast); |
| 32 |
| 33 // TODO(floitsch): add more methods: |
| 34 // - iterateBytes. |
| 35 } |
| 36 |
| 37 /** |
| 38 * This class provides a base-class for converters that need to accept byte |
| 39 * inputs. |
| 40 */ |
| 41 abstract class ByteConversionSinkBase extends ByteConversionSink { |
| 42 |
| 43 void add(List<int> chunk); |
| 44 void close(); |
| 45 |
| 46 void addSlice(List<int> chunk, int start, int end, bool isLast) { |
| 47 if (start != 0 || end != chunk.length) { |
| 48 add(chunk.sublist(start, end)); |
| 49 } else { |
| 50 add(chunk); |
| 51 } |
| 52 if (isLast) close(); |
| 53 } |
| 54 } |
| 55 |
| 56 /** |
| 57 * This class adapts a simple [ChunkedConversionSink] to a [ByteConversionSink]. |
| 58 * |
| 59 * All additional methods of the [ByteConversionSink] (compared to the |
| 60 * ChunkedConversionSink) are redirected to the `add` method. |
| 61 */ |
| 62 class _ByteAdapterSink extends ByteConversionSinkBase { |
| 63 final ChunkedConversionSink<List<int>> _sink; |
| 64 |
| 65 _ByteAdapterSink(this._sink); |
| 66 |
| 67 void add(List<int> chunk) => _sink.add(chunk); |
| 68 void close() => _sink.close(); |
| 69 } |
| 70 |
| 71 /** |
| 72 * This class accumulates all chunks into one list of bytes |
| 73 * and invokes a callback when the sink is closed. |
| 74 * |
| 75 * This class can be used to terminate a chunked conversion. |
| 76 */ |
| 77 class _ByteCallbackSink extends ByteConversionSinkBase { |
| 78 static const _INITIAL_BUFFER_SIZE = 1024; |
| 79 |
| 80 final _ChunkedConversionCallback<List<int>> _callback; |
| 81 // TODO(11971, floitsch): use Uint8List instead of normal lists. |
| 82 List<int> _buffer = new List<int>(_INITIAL_BUFFER_SIZE); |
| 83 int _bufferIndex = 0; |
| 84 |
| 85 _ByteCallbackSink(void callback(List<int> accumulated)) |
| 86 : this._callback = callback; |
| 87 |
| 88 void add(Iterable<int> chunk) { |
| 89 int freeCount = _buffer.length - _bufferIndex; |
| 90 if (chunk.length > freeCount) { |
| 91 // Grow the buffer. |
| 92 int oldLength = _buffer.length; |
| 93 int newLength = _roundToPowerOf2(chunk.length + oldLength) * 2; |
| 94 // TODO(11971, floitsch): use Uint8List instead of normal lists. |
| 95 List<int> grown = new List<int>(newLength); |
| 96 grown.setRange(0, _buffer.length, _buffer); |
| 97 _buffer = grown; |
| 98 } |
| 99 _buffer.setRange(_bufferIndex, _bufferIndex + chunk.length, chunk); |
| 100 _bufferIndex += chunk.length; |
| 101 } |
| 102 |
| 103 static int _roundToPowerOf2(int v) { |
| 104 assert(v > 0); |
| 105 v--; |
| 106 v |= v >> 1; |
| 107 v |= v >> 2; |
| 108 v |= v >> 4; |
| 109 v |= v >> 8; |
| 110 v |= v >> 16; |
| 111 v++; |
| 112 return v; |
| 113 } |
| 114 |
| 115 void close() { |
| 116 _callback(_buffer.sublist(0, _bufferIndex)); |
| 117 } |
| 118 } |
OLD | NEW |