| 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 * |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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. |
| 74 * | 74 * |
| 75 * This class can be used to terminate a chunked conversion. | 75 * This class can be used to terminate a chunked conversion. |
| 76 */ | 76 */ |
| 77 class _ByteCallbackSink extends ByteConversionSinkBase { | 77 class _ByteCallbackSink extends ByteConversionSinkBase { |
| 78 static const _INITIAL_BUFFER_SIZE = 1024; | 78 static const _INITIAL_BUFFER_SIZE = 1024; |
| 79 | 79 |
| 80 final _ChunkedConversionCallback<List<int>> _callback; | 80 final _ChunkedConversionCallback<List<int>> _callback; |
| 81 // TODO(11971, floitsch): use Uint8List instead of normal lists. | 81 List<int> _buffer = new Uint8List(_INITIAL_BUFFER_SIZE); |
| 82 List<int> _buffer = new List<int>(_INITIAL_BUFFER_SIZE); | |
| 83 int _bufferIndex = 0; | 82 int _bufferIndex = 0; |
| 84 | 83 |
| 85 _ByteCallbackSink(void callback(List<int> accumulated)) | 84 _ByteCallbackSink(void callback(List<int> accumulated)) |
| 86 : this._callback = callback; | 85 : this._callback = callback; |
| 87 | 86 |
| 88 void add(Iterable<int> chunk) { | 87 void add(Iterable<int> chunk) { |
| 89 int freeCount = _buffer.length - _bufferIndex; | 88 int freeCount = _buffer.length - _bufferIndex; |
| 90 if (chunk.length > freeCount) { | 89 if (chunk.length > freeCount) { |
| 91 // Grow the buffer. | 90 // Grow the buffer. |
| 92 int oldLength = _buffer.length; | 91 int oldLength = _buffer.length; |
| 93 int newLength = _roundToPowerOf2(chunk.length + oldLength) * 2; | 92 int newLength = _roundToPowerOf2(chunk.length + oldLength) * 2; |
| 94 // TODO(11971, floitsch): use Uint8List instead of normal lists. | 93 List<int> grown = new Uint8List(newLength); |
| 95 List<int> grown = new List<int>(newLength); | |
| 96 grown.setRange(0, _buffer.length, _buffer); | 94 grown.setRange(0, _buffer.length, _buffer); |
| 97 _buffer = grown; | 95 _buffer = grown; |
| 98 } | 96 } |
| 99 _buffer.setRange(_bufferIndex, _bufferIndex + chunk.length, chunk); | 97 _buffer.setRange(_bufferIndex, _bufferIndex + chunk.length, chunk); |
| 100 _bufferIndex += chunk.length; | 98 _bufferIndex += chunk.length; |
| 101 } | 99 } |
| 102 | 100 |
| 103 static int _roundToPowerOf2(int v) { | 101 static int _roundToPowerOf2(int v) { |
| 104 assert(v > 0); | 102 assert(v > 0); |
| 105 v--; | 103 v--; |
| 106 v |= v >> 1; | 104 v |= v >> 1; |
| 107 v |= v >> 2; | 105 v |= v >> 2; |
| 108 v |= v >> 4; | 106 v |= v >> 4; |
| 109 v |= v >> 8; | 107 v |= v >> 8; |
| 110 v |= v >> 16; | 108 v |= v >> 16; |
| 111 v++; | 109 v++; |
| 112 return v; | 110 return v; |
| 113 } | 111 } |
| 114 | 112 |
| 115 void close() { | 113 void close() { |
| 116 _callback(_buffer.sublist(0, _bufferIndex)); | 114 _callback(_buffer.sublist(0, _bufferIndex)); |
| 117 } | 115 } |
| 118 } | 116 } |
| OLD | NEW |