Chromium Code Reviews| Index: runtime/lib/byte_buffer.dart |
| diff --git a/runtime/lib/byte_buffer.dart b/runtime/lib/byte_buffer.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..76b58d2159f70ca9dbf1d618aa34869993363cc7 |
| --- /dev/null |
| +++ b/runtime/lib/byte_buffer.dart |
| @@ -0,0 +1,138 @@ |
| +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +class ByteBuffer implements List { |
| + factory ByteBuffer(int length) native "ByteBuffer_allocate"; |
|
Ivan Posva
2011/11/10 18:06:41
Please make all natives be private methods that ar
cshapiro
2011/11/11 00:37:53
Done.
|
| + |
| + int getInt8(int offset) native "ByteBuffer_getInt8"; |
| + void setInt8(int offset, int value) native "ByteBuffer_setInt8"; |
| + |
| + int getUint8(int offset) native "ByteBuffer_getUint8"; |
| + void setUint8(int offset, int value) native "ByteBuffer_setUint8"; |
| + |
| + int getInt16(int offset) native "ByteBuffer_getInt16"; |
| + void setInt16(int offset, int value) native "ByteBuffer_setInt16"; |
| + |
| + int getUint16(int offset) native "ByteBuffer_getUint16"; |
| + void setUint16(int offset, int value) native "ByteBuffer_setUint16"; |
| + |
| + int getInt32(int offset) native "ByteBuffer_getInt32"; |
| + void setInt32(int offset, int value) native "ByteBuffer_setInt32"; |
| + |
| + int getUint32(int offset) native "ByteBuffer_getUint32"; |
| + void setUint32(int offset, int value) native "ByteBuffer_setUint32"; |
| + |
| + int getInt64(int offset) native "ByteBuffer_getInt64"; |
| + void setInt64(int offset, int value) native "ByteBuffer_setInt64"; |
| + |
| + int getUint64(int offset) native "ByteBuffer_getUint64"; |
| + void setUint64(int offset, int value) native "ByteBuffer_setUint64"; |
| + |
| + double getFloat32(int offset) native "ByteBuffer_getFloat32"; |
| + void setFloat32(int offset, double value) native "ByteBuffer_setFloat32"; |
| + |
| + double getFloat64(int offset) native "ByteBuffer_getFloat64"; |
| + void setFloat64(int offset, double value) native "ByteBuffer_setFloat64"; |
| + |
| + // Iterable interface |
| + |
| + Iterator iterator() { |
| + return new ByteBufferIterator(this); |
| + } |
| + |
| + // Collection interface |
| + |
| + int get length() native "ByteBuffer_getLength"; |
| + |
| + bool every(bool f(int element)) { |
| + return Collections.every(this, f); |
| + } |
| + |
| + Collection filter(bool f(int element)) { |
| + return Collections.filter(this, new GrowableObjectArray(), f); |
| + } |
| + |
| + void forEach(f(int element)) { |
| + Collections.forEach(this, f); |
| + } |
| + |
| + bool isEmpty() { |
| + return this.length === 0; |
| + } |
| + |
| + bool some(bool f(int element)) { |
| + return Collections.some(this, f); |
| + } |
| + |
| + // List interface |
| + |
| + int operator[](int index) native "ByteBuffer_getUint32"; |
| + |
| + void operator[]=(int index, int value) native "ByteBuffer_setUint8"; |
| + |
| + void set length(int newLength) { |
| + throw const UnsupportedOperationException("Cannot add to a byte buffer"); |
| + } |
| + |
| + void add(int element) { |
| + throw const UnsupportedOperationException("Cannot add to a byte buffer"); |
| + } |
| + |
| + void addLast(int element) { |
| + add(element); |
| + } |
| + |
| + void addAll(Collection elements) { |
| + throw const UnsupportedOperationException("Cannot add to a byte buffer"); |
| + } |
| + |
| + void sort(int compare(int a, b)) { |
| + DualPivotQuicksort.sort(this, compare); |
| + } |
| + |
| + int indexOf(int element, [int start = 0]) { |
| + return Arrays.indexOf(this, element, start, this.length); |
| + } |
| + |
| + int lastIndexOf(int element, [int start = null]) { |
| + if (start === null) start = length - 1; |
| + return Arrays.lastIndexOf(this, element, start); |
| + } |
| + |
| + void clear() { |
| + throw const UnsupportedOperationException("Cannot clear a byte buffer"); |
| + } |
| + |
| + int removeLast() { |
| + throw const UnsupportedOperationException( |
| + "Cannot remove from a byte buffer"); |
| + } |
| + |
| + int last() { |
| + return this[length - 1]; |
| + } |
| +} |
| + |
| + |
| +class ByteBufferIterator implements Iterator { |
| + ByteBufferIterator(List byteBuffer) |
| + : _byteBuffer = byteBuffer, _length = byteBuffer.length, _pos = 0 { |
| + assert(byteBuffer is ByteBuffer); |
| + } |
| + |
| + bool hasNext() { |
| + return _length > _pos; |
| + } |
| + |
| + int next() { |
| + if (!hasNext()) { |
| + throw const NoMoreElementsException(); |
| + } |
| + return _byteBuffer[_pos++]; |
| + } |
| + |
| + final List _byteBuffer; |
| + final int _length; // Cache byte buffer length for faster access. |
| + int _pos; |
| +} |