OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 10 matching lines...) Expand all Loading... |
21 _buffers.addLast(buffer); | 21 _buffers.addLast(buffer); |
22 _length += buffer.length; | 22 _length += buffer.length; |
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 /** | 33 /** |
34 * Returns the current index of the next byte. This will always be | 34 * Returns the current index of the next byte. This will always be |
35 * 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 |
36 * the end of a buffer it is removed from the list. | 36 * the end of a buffer it is removed from the list. |
37 */ | 37 */ |
38 int get index => _index; | 38 int get index => _index; |
39 | 39 |
40 /** | 40 /** |
41 * Peek at the next available byte. | 41 * Peek at the next available byte. |
42 */ | 42 */ |
43 int peek() => _buffers.first()[_index]; | 43 int peek() => _buffers.first[_index]; |
44 | 44 |
45 /** | 45 /** |
46 * Returns the next available byte removing it from the buffers. | 46 * Returns the next available byte removing it from the buffers. |
47 */ | 47 */ |
48 int next() { | 48 int next() { |
49 int value = _buffers.first()[_index++]; | 49 int value = _buffers.first[_index++]; |
50 _length--; | 50 _length--; |
51 if (_index == _buffers.first().length) { | 51 if (_index == _buffers.first.length) { |
52 _buffers.removeFirst(); | 52 _buffers.removeFirst(); |
53 _index = 0; | 53 _index = 0; |
54 } | 54 } |
55 return value; | 55 return value; |
56 } | 56 } |
57 | 57 |
58 /** | 58 /** |
59 * Read [count] bytes from the buffer list. If the number of bytes | 59 * Read [count] bytes from the buffer list. If the number of bytes |
60 * requested is not available null will be returned. | 60 * requested is not available null will be returned. |
61 */ | 61 */ |
62 List<int> readBytes(int count) { | 62 List<int> readBytes(int count) { |
63 List<int> result; | 63 List<int> result; |
64 if (_length == 0 || _length < count) return null; | 64 if (_length == 0 || _length < count) return null; |
65 if (_index == 0 && _buffers.first().length == count) { | 65 if (_index == 0 && _buffers.first.length == count) { |
66 result = _buffers.first(); | 66 result = _buffers.first; |
67 _buffers.removeFirst(); | 67 _buffers.removeFirst(); |
68 _index = 0; | 68 _index = 0; |
69 _length -= count; | 69 _length -= count; |
70 return result; | 70 return result; |
71 } else { | 71 } else { |
72 int firstRemaining = _buffers.first().length - _index; | 72 int firstRemaining = _buffers.first.length - _index; |
73 if (firstRemaining >= count) { | 73 if (firstRemaining >= count) { |
74 result = _buffers.first().getRange(_index, count); | 74 result = _buffers.first.getRange(_index, count); |
75 _index += count; | 75 _index += count; |
76 _length -= count; | 76 _length -= count; |
77 if (_index == _buffers.first().length) { | 77 if (_index == _buffers.first.length) { |
78 _buffers.removeFirst(); | 78 _buffers.removeFirst(); |
79 _index = 0; | 79 _index = 0; |
80 } | 80 } |
81 return result; | 81 return result; |
82 } else { | 82 } else { |
83 result = new Uint8List(count); | 83 result = new Uint8List(count); |
84 int remaining = count; | 84 int remaining = count; |
85 while (remaining > 0) { | 85 while (remaining > 0) { |
86 int bytesInFirst = _buffers.first().length - _index; | 86 int bytesInFirst = _buffers.first.length - _index; |
87 if (bytesInFirst <= remaining) { | 87 if (bytesInFirst <= remaining) { |
88 result.setRange(count - remaining, | 88 result.setRange(count - remaining, |
89 bytesInFirst, | 89 bytesInFirst, |
90 _buffers.first(), | 90 _buffers.first, |
91 _index); | 91 _index); |
92 _buffers.removeFirst(); | 92 _buffers.removeFirst(); |
93 _index = 0; | 93 _index = 0; |
94 _length -= bytesInFirst; | 94 _length -= bytesInFirst; |
95 remaining -= bytesInFirst; | 95 remaining -= bytesInFirst; |
96 } else { | 96 } else { |
97 result.setRange(count - remaining, | 97 result.setRange(count - remaining, |
98 remaining, | 98 remaining, |
99 _buffers.first(), | 99 _buffers.first, |
100 _index); | 100 _index); |
101 _index = remaining; | 101 _index = remaining; |
102 _length -= remaining; | 102 _length -= remaining; |
103 remaining = 0; | 103 remaining = 0; |
104 assert(_index < _buffers.first().length); | 104 assert(_index < _buffers.first.length); |
105 } | 105 } |
106 } | 106 } |
107 return result; | 107 return result; |
108 } | 108 } |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 /** | 112 /** |
113 * Remove a number of bytes from the buffer list. Currently the | 113 * Remove a number of bytes from the buffer list. Currently the |
114 * number of bytes to remove must be confined to the first buffer. | 114 * number of bytes to remove must be confined to the first buffer. |
(...skipping 28 matching lines...) Expand all Loading... |
143 void clear() { | 143 void clear() { |
144 _index = 0; | 144 _index = 0; |
145 _length = 0; | 145 _length = 0; |
146 _buffers = new Queue(); | 146 _buffers = new Queue(); |
147 } | 147 } |
148 | 148 |
149 int _length; // Total number of bytes remaining in the buffers. | 149 int _length; // Total number of bytes remaining in the buffers. |
150 Queue<List<int>> _buffers; // List of data buffers. | 150 Queue<List<int>> _buffers; // List of data buffers. |
151 int _index; // Index of the next byte in the first buffer. | 151 int _index; // Index of the next byte in the first buffer. |
152 } | 152 } |
OLD | NEW |