| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Utility class that holds a number of byte buffers and can deliver |
| 7 * the bytes either one by one or in chunks. |
| 8 */ |
| 9 class _BufferList { |
| 10 _BufferList() { |
| 11 clear(); |
| 12 } |
| 13 |
| 14 /** |
| 15 * Adds a new buffer to the list possibly with an offset of the |
| 16 * first byte of interest. The offset can only be specified if the |
| 17 * buffer list is empty. |
| 18 */ |
| 19 void add(List<int> buffer, [int offset = 0]) { |
| 20 assert(offset == 0 || _buffers.isEmpty()); |
| 21 _buffers.addLast(buffer); |
| 22 _length += buffer.length; |
| 23 if (offset != 0) _index = offset; |
| 24 } |
| 25 |
| 26 /** |
| 27 * Returns the first buffer from the list. This returns the whole |
| 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. |
| 30 */ |
| 31 List<int> get first() => _buffers.first(); |
| 32 |
| 33 /* 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 * the end of a buffer it is removed from the list. |
| 36 */ |
| 37 int get index() => _index; |
| 38 |
| 39 /** |
| 40 * Peek at the next available byte. |
| 41 */ |
| 42 int peek() => _buffers.first()[_index]; |
| 43 |
| 44 /* |
| 45 * Returns the next available byte removing it from the buffers. |
| 46 */ |
| 47 int next() { |
| 48 int value = _buffers.first()[_index++]; |
| 49 _length--; |
| 50 if (_index == _buffers.first().length) { |
| 51 _buffers.removeFirst(); |
| 52 _index = 0; |
| 53 } |
| 54 return value; |
| 55 } |
| 56 |
| 57 /** |
| 58 * Remove a number of bytes from the buffer list. Currently the |
| 59 * number of bytes to remove must be confined to the first buffer. |
| 60 */ |
| 61 void removeBytes(int count) { |
| 62 int firstRemaining = first.length - _index; |
| 63 assert(count <= firstRemaining); |
| 64 if (count == firstRemaining) { |
| 65 _buffers.removeFirst(); |
| 66 _index = 0; |
| 67 } else { |
| 68 _index += count; |
| 69 } |
| 70 _length -= count; |
| 71 } |
| 72 |
| 73 |
| 74 /** |
| 75 * Returns the total number of bytes remaining in the buffers. |
| 76 */ |
| 77 int get length() => _length; |
| 78 |
| 79 /** |
| 80 * Returns whether the buffer list is empty that is has no bytes |
| 81 * available. |
| 82 */ |
| 83 bool isEmpty() => _buffers.isEmpty(); |
| 84 |
| 85 /** |
| 86 * Clears the content of the buffer list. |
| 87 */ |
| 88 void clear() { |
| 89 _index = 0; |
| 90 _length = 0; |
| 91 _buffers = new Queue(); |
| 92 } |
| 93 |
| 94 int _length; // Total number of bytes remaining in the buffers. |
| 95 Queue<List<int>> _buffers; // List of data buffers. |
| 96 int _index; // Index of the next byte in the first buffer. |
| 97 } |
| OLD | NEW |