| 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 class ByteBuffer implements List { |
| 6 factory ByteBuffer(int length) native "ByteBuffer_allocate"; |
| 7 |
| 8 int getInt8(int byteOffset) native "ByteBuffer_getInt8"; |
| 9 void setInt8(int byteOffset, int value) native "ByteBuffer_setInt8"; |
| 10 |
| 11 int getUint8(int byteOffset) native "ByteBuffer_getUint8"; |
| 12 void setUint8(int byteOffset, int value) native "ByteBuffer_setUint8"; |
| 13 |
| 14 int getInt16(int byteOffset) native "ByteBuffer_getInt16"; |
| 15 void setInt16(int byteOffset, int value) native "ByteBuffer_setInt16"; |
| 16 |
| 17 int getUint16(int byteOffset) native "ByteBuffer_getUint16"; |
| 18 void setUint16(int byteOffset, int value) native "ByteBuffer_setUint16"; |
| 19 |
| 20 int getInt32(int byteOffset) native "ByteBuffer_getInt32"; |
| 21 void setInt32(int byteOffset, int value) native "ByteBuffer_setInt32"; |
| 22 |
| 23 int getUint32(int byteOffset) native "ByteBuffer_getUint32"; |
| 24 void setUint32(int byteOffset, int value) native "ByteBuffer_setUint32"; |
| 25 |
| 26 int getInt64(int byteOffset) native "ByteBuffer_getInt64"; |
| 27 void setInt64(int byteOffset, int value) native "ByteBuffer_setInt64"; |
| 28 |
| 29 int getUint64(int byteOffset) native "ByteBuffer_getUint64"; |
| 30 void setUint64(int byteOffset, int value) native "ByteBuffer_setUint64"; |
| 31 |
| 32 double getFloat32(int byteOffset) native "ByteBuffer_getFloat32"; |
| 33 void setFloat32(int byteOffset, double value) native "ByteBuffer_setFloat32"; |
| 34 |
| 35 double getFloat64(int byteOffset) native "ByteBuffer_getFloat64"; |
| 36 void setFloat64(int byteOffset, double value) native "ByteBuffer_setFloat64"; |
| 37 |
| 38 // Iterable interface |
| 39 |
| 40 Iterator iterator() { |
| 41 return new ByteBufferIterator(this); |
| 42 } |
| 43 |
| 44 // Collection interface |
| 45 |
| 46 int get length() native "ByteBuffer_getLength"; |
| 47 |
| 48 bool every(bool f(int element)) { |
| 49 return Collections.every(this, f); |
| 50 } |
| 51 |
| 52 Collection filter(bool f(int element)) { |
| 53 return Collections.filter(this, new GrowableObjectArray(), f); |
| 54 } |
| 55 |
| 56 void forEach(f(int element)) { |
| 57 Collections.forEach(this, f); |
| 58 } |
| 59 |
| 60 bool isEmpty() { |
| 61 return this.length === 0; |
| 62 } |
| 63 |
| 64 bool some(bool f(int element)) { |
| 65 return Collections.some(this, f); |
| 66 } |
| 67 |
| 68 // List interface |
| 69 |
| 70 int operator[](int index) native "ByteBuffer_getUint32"; |
| 71 |
| 72 void operator[]=(int index, int value) native "ByteBuffer_setUint8"; |
| 73 |
| 74 void set length(int newLength) { |
| 75 throw const UnsupportedOperationException("Cannot add to a byte buffer"); |
| 76 } |
| 77 |
| 78 void add(int element) { |
| 79 throw const UnsupportedOperationException("Cannot add to a byte buffer"); |
| 80 } |
| 81 |
| 82 void addLast(int element) { |
| 83 add(element); |
| 84 } |
| 85 |
| 86 void addAll(Collection elements) { |
| 87 throw const UnsupportedOperationException("Cannot add to a byte buffer"); |
| 88 } |
| 89 |
| 90 void sort(int compare(int a, b)) { |
| 91 DualPivotQuicksort.sort(this, compare); |
| 92 } |
| 93 |
| 94 int indexOf(int element, [int start = 0]) { |
| 95 return Arrays.indexOf(this, element, start, this.length); |
| 96 } |
| 97 |
| 98 int lastIndexOf(int element, [int start = null]) { |
| 99 if (start === null) start = length - 1; |
| 100 return Arrays.lastIndexOf(this, element, start); |
| 101 } |
| 102 |
| 103 void clear() { |
| 104 throw const UnsupportedOperationException("Cannot clear a byte buffer"); |
| 105 } |
| 106 |
| 107 int removeLast() { |
| 108 throw const UnsupportedOperationException( |
| 109 "Cannot remove from a byte buffer"); |
| 110 } |
| 111 |
| 112 int last() { |
| 113 return this[length - 1]; |
| 114 } |
| 115 } |
| 116 |
| 117 |
| 118 class ByteBufferIterator implements Iterator { |
| 119 ByteBufferIterator(List byteBuffer) |
| 120 : _byteBuffer = byteBuffer, _length = byteBuffer.length, _pos = 0 { |
| 121 assert(byteBuffer is ByteBuffer); |
| 122 } |
| 123 |
| 124 bool hasNext() { |
| 125 return _length > _pos; |
| 126 } |
| 127 |
| 128 int next() { |
| 129 if (!hasNext()) { |
| 130 throw const NoMoreElementsException(); |
| 131 } |
| 132 return _byteBuffer[_pos++]; |
| 133 } |
| 134 |
| 135 final List _byteBuffer; |
| 136 final int _length; // Cache byte buffer length for faster access. |
| 137 int _pos; |
| 138 } |
| OLD | NEW |