Chromium Code Reviews| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 int next() { | 47 int next() { |
| 48 int value = _buffers.first()[_index++]; | 48 int value = _buffers.first()[_index++]; |
| 49 _length--; | 49 _length--; |
| 50 if (_index == _buffers.first().length) { | 50 if (_index == _buffers.first().length) { |
| 51 _buffers.removeFirst(); | 51 _buffers.removeFirst(); |
| 52 _index = 0; | 52 _index = 0; |
| 53 } | 53 } |
| 54 return value; | 54 return value; |
| 55 } | 55 } |
| 56 | 56 |
| 57 /* | |
|
Mads Ager (google)
2011/12/06 14:05:02
Do you want to use DartDoc style here: '/**'?
Hal
Søren Gjesse
2011/12/07 07:58:29
I am not realy sure. This is an internal class. Ma
| |
| 58 * Read [count] bytes from the buffer list. If the number of bytes | |
| 59 * requested is not available null will be returned. | |
| 60 */ | |
| 61 List<int> readBytes(int count) { | |
| 62 List<int> result; | |
| 63 if (_length == 0 || _length < count) return null; | |
| 64 if (_index == 0 && _buffers.first().length == count) { | |
| 65 result = _buffers.first(); | |
| 66 _buffers.removeFirst(); | |
| 67 _index = 0; | |
| 68 _length -= count; | |
| 69 return result; | |
| 70 } else { | |
| 71 int firstRemaining = _buffers.first().length - _index; | |
| 72 if (firstRemaining >= count) { | |
| 73 result = _buffers.first().getRange(_index, count); | |
| 74 _index += count; | |
| 75 _length -= count; | |
| 76 return result; | |
| 77 } else { | |
| 78 result = new List<int>(count); | |
| 79 int remaining = count; | |
| 80 while (remaining > 0) { | |
| 81 int bytesInFirst = _buffers.first().length - _index; | |
| 82 if (bytesInFirst <= remaining) { | |
| 83 result.setRange(count - remaining, | |
| 84 bytesInFirst, | |
| 85 _buffers.first(), | |
| 86 _index); | |
| 87 _buffers.removeFirst(); | |
| 88 _index = 0; | |
| 89 _length -= bytesInFirst; | |
| 90 remaining -= bytesInFirst; | |
| 91 } else { | |
| 92 result.setRange(count - remaining, | |
| 93 remaining, | |
| 94 _buffers.first(), | |
| 95 _index); | |
| 96 _index = remaining; | |
| 97 _length -= remaining; | |
| 98 remaining = 0; | |
| 99 assert(_index < _buffers.first().length); | |
| 100 } | |
| 101 } | |
| 102 return result; | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 | |
| 57 /** | 107 /** |
| 58 * Remove a number of bytes from the buffer list. Currently the | 108 * Remove a number of bytes from the buffer list. Currently the |
| 59 * number of bytes to remove must be confined to the first buffer. | 109 * number of bytes to remove must be confined to the first buffer. |
| 60 */ | 110 */ |
| 61 void removeBytes(int count) { | 111 void removeBytes(int count) { |
| 62 int firstRemaining = first.length - _index; | 112 int firstRemaining = first.length - _index; |
| 63 assert(count <= firstRemaining); | 113 assert(count <= firstRemaining); |
| 64 if (count == firstRemaining) { | 114 if (count == firstRemaining) { |
| 65 _buffers.removeFirst(); | 115 _buffers.removeFirst(); |
| 66 _index = 0; | 116 _index = 0; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 88 void clear() { | 138 void clear() { |
| 89 _index = 0; | 139 _index = 0; |
| 90 _length = 0; | 140 _length = 0; |
| 91 _buffers = new Queue(); | 141 _buffers = new Queue(); |
| 92 } | 142 } |
| 93 | 143 |
| 94 int _length; // Total number of bytes remaining in the buffers. | 144 int _length; // Total number of bytes remaining in the buffers. |
| 95 Queue<List<int>> _buffers; // List of data buffers. | 145 Queue<List<int>> _buffers; // List of data buffers. |
| 96 int _index; // Index of the next byte in the first buffer. | 146 int _index; // Index of the next byte in the first buffer. |
| 97 } | 147 } |
| OLD | NEW |