Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1259)

Unified Diff: runtime/bin/buffer_list.dart

Issue 8818009: Add chunked input stream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments from ager@ Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/bin/builtin_sources.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/buffer_list.dart
diff --git a/runtime/bin/buffer_list.dart b/runtime/bin/buffer_list.dart
index 540557ab00990e55d5dc49d17882d04328bc2114..7dc7c601b2e17ede22f1db7431a33f7f8cf3c33d 100644
--- a/runtime/bin/buffer_list.dart
+++ b/runtime/bin/buffer_list.dart
@@ -30,7 +30,8 @@ class _BufferList {
*/
List<int> get first() => _buffers.first();
- /* Returns the current index of the next byte. This will always be
+ /**
+ * Returns the current index of the next byte. This will always be
* an index into the first buffer as when the index is advanced past
* the end of a buffer it is removed from the list.
*/
@@ -41,7 +42,7 @@ class _BufferList {
*/
int peek() => _buffers.first()[_index];
- /*
+ /**
* Returns the next available byte removing it from the buffers.
*/
int next() {
@@ -55,6 +56,56 @@ class _BufferList {
}
/**
+ * Read [count] bytes from the buffer list. If the number of bytes
+ * requested is not available null will be returned.
+ */
+ List<int> readBytes(int count) {
+ List<int> result;
+ if (_length == 0 || _length < count) return null;
+ if (_index == 0 && _buffers.first().length == count) {
+ result = _buffers.first();
+ _buffers.removeFirst();
+ _index = 0;
+ _length -= count;
+ return result;
+ } else {
+ int firstRemaining = _buffers.first().length - _index;
+ if (firstRemaining >= count) {
+ result = _buffers.first().getRange(_index, count);
+ _index += count;
+ _length -= count;
+ return result;
+ } else {
+ result = new List<int>(count);
+ int remaining = count;
+ while (remaining > 0) {
+ int bytesInFirst = _buffers.first().length - _index;
+ if (bytesInFirst <= remaining) {
+ result.setRange(count - remaining,
+ bytesInFirst,
+ _buffers.first(),
+ _index);
+ _buffers.removeFirst();
+ _index = 0;
+ _length -= bytesInFirst;
+ remaining -= bytesInFirst;
+ } else {
+ result.setRange(count - remaining,
+ remaining,
+ _buffers.first(),
+ _index);
+ _index = remaining;
+ _length -= remaining;
+ remaining = 0;
+ assert(_index < _buffers.first().length);
+ }
+ }
+ return result;
+ }
+ }
+ }
+
+ /**
* Remove a number of bytes from the buffer list. Currently the
* number of bytes to remove must be confined to the first buffer.
*/
« no previous file with comments | « no previous file | runtime/bin/builtin_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698