| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * Utility class that holds a number of byte buffers and can deliver | 6 * Utility class that holds a number of byte buffers and can deliver |
| 7 * the bytes either one by one or in chunks. | 7 * the bytes either one by one or in chunks. |
| 8 */ | 8 */ |
| 9 class _BufferList { | 9 class _BufferList { |
| 10 _BufferList() { | 10 _BufferList() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 if (offset != 0) _index = offset; | 23 if (offset != 0) _index = offset; |
| 24 } | 24 } |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Returns the first buffer from the list. This returns the whole | 27 * Returns the first buffer from the list. This returns the whole |
| 28 * buffer and does not remove the buffer from the list. Use | 28 * buffer and does not remove the buffer from the list. Use |
| 29 * [index] to determine the index of the first byte in the buffer. | 29 * [index] to determine the index of the first byte in the buffer. |
| 30 */ | 30 */ |
| 31 List<int> get first() => _buffers.first(); | 31 List<int> get first() => _buffers.first(); |
| 32 | 32 |
| 33 /* Returns the current index of the next byte. This will always be | 33 /** |
| 34 * Returns the current index of the next byte. This will always be |
| 34 * an index into the first buffer as when the index is advanced past | 35 * an index into the first buffer as when the index is advanced past |
| 35 * the end of a buffer it is removed from the list. | 36 * the end of a buffer it is removed from the list. |
| 36 */ | 37 */ |
| 37 int get index() => _index; | 38 int get index() => _index; |
| 38 | 39 |
| 39 /** | 40 /** |
| 40 * Peek at the next available byte. | 41 * Peek at the next available byte. |
| 41 */ | 42 */ |
| 42 int peek() => _buffers.first()[_index]; | 43 int peek() => _buffers.first()[_index]; |
| 43 | 44 |
| 44 /* | 45 /** |
| 45 * Returns the next available byte removing it from the buffers. | 46 * Returns the next available byte removing it from the buffers. |
| 46 */ | 47 */ |
| 47 int next() { | 48 int next() { |
| 48 int value = _buffers.first()[_index++]; | 49 int value = _buffers.first()[_index++]; |
| 49 _length--; | 50 _length--; |
| 50 if (_index == _buffers.first().length) { | 51 if (_index == _buffers.first().length) { |
| 51 _buffers.removeFirst(); | 52 _buffers.removeFirst(); |
| 52 _index = 0; | 53 _index = 0; |
| 53 } | 54 } |
| 54 return value; | 55 return value; |
| 55 } | 56 } |
| 56 | 57 |
| 57 /** | 58 /** |
| 59 * Read [count] bytes from the buffer list. If the number of bytes |
| 60 * requested is not available null will be returned. |
| 61 */ |
| 62 List<int> readBytes(int count) { |
| 63 List<int> result; |
| 64 if (_length == 0 || _length < count) return null; |
| 65 if (_index == 0 && _buffers.first().length == count) { |
| 66 result = _buffers.first(); |
| 67 _buffers.removeFirst(); |
| 68 _index = 0; |
| 69 _length -= count; |
| 70 return result; |
| 71 } else { |
| 72 int firstRemaining = _buffers.first().length - _index; |
| 73 if (firstRemaining >= count) { |
| 74 result = _buffers.first().getRange(_index, count); |
| 75 _index += count; |
| 76 _length -= count; |
| 77 return result; |
| 78 } else { |
| 79 result = new List<int>(count); |
| 80 int remaining = count; |
| 81 while (remaining > 0) { |
| 82 int bytesInFirst = _buffers.first().length - _index; |
| 83 if (bytesInFirst <= remaining) { |
| 84 result.setRange(count - remaining, |
| 85 bytesInFirst, |
| 86 _buffers.first(), |
| 87 _index); |
| 88 _buffers.removeFirst(); |
| 89 _index = 0; |
| 90 _length -= bytesInFirst; |
| 91 remaining -= bytesInFirst; |
| 92 } else { |
| 93 result.setRange(count - remaining, |
| 94 remaining, |
| 95 _buffers.first(), |
| 96 _index); |
| 97 _index = remaining; |
| 98 _length -= remaining; |
| 99 remaining = 0; |
| 100 assert(_index < _buffers.first().length); |
| 101 } |
| 102 } |
| 103 return result; |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 /** |
| 58 * Remove a number of bytes from the buffer list. Currently the | 109 * Remove a number of bytes from the buffer list. Currently the |
| 59 * number of bytes to remove must be confined to the first buffer. | 110 * number of bytes to remove must be confined to the first buffer. |
| 60 */ | 111 */ |
| 61 void removeBytes(int count) { | 112 void removeBytes(int count) { |
| 62 int firstRemaining = first.length - _index; | 113 int firstRemaining = first.length - _index; |
| 63 assert(count <= firstRemaining); | 114 assert(count <= firstRemaining); |
| 64 if (count == firstRemaining) { | 115 if (count == firstRemaining) { |
| 65 _buffers.removeFirst(); | 116 _buffers.removeFirst(); |
| 66 _index = 0; | 117 _index = 0; |
| 67 } else { | 118 } else { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 88 void clear() { | 139 void clear() { |
| 89 _index = 0; | 140 _index = 0; |
| 90 _length = 0; | 141 _length = 0; |
| 91 _buffers = new Queue(); | 142 _buffers = new Queue(); |
| 92 } | 143 } |
| 93 | 144 |
| 94 int _length; // Total number of bytes remaining in the buffers. | 145 int _length; // Total number of bytes remaining in the buffers. |
| 95 Queue<List<int>> _buffers; // List of data buffers. | 146 Queue<List<int>> _buffers; // List of data buffers. |
| 96 int _index; // Index of the next byte in the first buffer. | 147 int _index; // Index of the next byte in the first buffer. |
| 97 } | 148 } |
| OLD | NEW |