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

Side by Side Diff: runtime/lib/byte_buffer.dart

Issue 9195031: Add ByteArray interface and provide internal and external implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fewer templates Created 8 years, 11 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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) {
7 return _allocate(length);
8 }
9
10 int getInt8(int byteOffset) {
11 return _getInt8(byteOffset);
12 }
13 void setInt8(int byteOffset, int value) {
14 _setInt8(byteOffset, value);
15 }
16
17 int getUint8(int byteOffset) {
18 return _getUint8(byteOffset);
19 }
20 void setUint8(int byteOffset, int value) {
21 _setUint8(byteOffset, value);
22 }
23
24 int getInt16(int byteOffset) {
25 return _getInt16(byteOffset);
26 }
27 void setInt16(int byteOffset, int value) {
28 _setInt16(byteOffset, value);
29 }
30
31 int getUint16(int byteOffset) {
32 return _getInt16(byteOffset);
33 }
34 void setUint16(int byteOffset, int value) {
35 _setUint16(byteOffset, value);
36 }
37
38 int getInt32(int byteOffset) {
39 return _getInt32(byteOffset);
40 }
41 void setInt32(int byteOffset, int value) {
42 _setInt32(byteOffset, value);
43 }
44
45 int getUint32(int byteOffset) {
46 return _getUint32(byteOffset);
47 }
48 void setUint32(int byteOffset, int value) {
49 _setUint32(byteOffset, value);
50 }
51
52 int getInt64(int byteOffset) {
53 return _getInt64(byteOffset);
54 }
55 void setInt64(int byteOffset, int value) {
56 _setInt64(byteOffset, value);
57 }
58
59 int getUint64(int byteOffset) {
60 return _getUint64(byteOffset);
61 }
62 void setUint64(int byteOffset, int value) {
63 _setUint64(byteOffset, value);
64 }
65
66 double getFloat32(int byteOffset) {
67 return _getFloat32(byteOffset);
68 }
69 void setFloat32(int byteOffset, double value) {
70 _setFloat32(byteOffset, value);
71 }
72
73 double getFloat64(int byteOffset) {
74 return _getFloat64(byteOffset);
75 }
76 void setFloat64(int byteOffset, double value) {
77 _setFloat64(byteOffset, value);
78 }
79
80 // Iterable interface
81
82 Iterator iterator() {
83 return new ByteBufferIterator(this);
84 }
85
86 // Collection interface
87
88 int get length() {
89 return _length();
90 }
91
92 bool every(bool f(int element)) {
93 return Collections.every(this, f);
94 }
95
96 Collection map(f(int element)) {
97 return Collections.map(this, new GrowableObjectArray.withCapacity(length), f );
98 }
99
100 Collection filter(bool f(int element)) {
101 return Collections.filter(this, new GrowableObjectArray(), f);
102 }
103
104 void forEach(f(int element)) {
105 Collections.forEach(this, f);
106 }
107
108 bool isEmpty() {
109 return this.length === 0;
110 }
111
112 bool some(bool f(int element)) {
113 return Collections.some(this, f);
114 }
115
116 // List interface
117
118 int operator[](int index) {
119 return _getUint8(index);
120 }
121
122 void operator[]=(int index, int value) {
123 _setUint8(index, value);
124 }
125
126 void set length(int newLength) {
127 throw const UnsupportedOperationException("Cannot add to a byte buffer");
128 }
129
130 void add(int element) {
131 throw const UnsupportedOperationException("Cannot add to a byte buffer");
132 }
133
134 void addLast(int element) {
135 add(element);
136 }
137
138 void addAll(Collection elements) {
139 throw const UnsupportedOperationException("Cannot add to a byte buffer");
140 }
141
142 void sort(int compare(int a, b)) {
143 DualPivotQuicksort.sort(this, compare);
144 }
145
146 int indexOf(int element, [int start = 0]) {
147 return Arrays.indexOf(this, element, start, this.length);
148 }
149
150 int lastIndexOf(int element, [int start = null]) {
151 if (start === null) start = length - 1;
152 return Arrays.lastIndexOf(this, element, start);
153 }
154
155 void clear() {
156 throw const UnsupportedOperationException("Cannot clear a byte buffer");
157 }
158
159 int removeLast() {
160 throw const UnsupportedOperationException(
161 "Cannot remove from a byte buffer");
162 }
163
164 int last() {
165 return this[length - 1];
166 }
167
168 // Implementation
169
170 static ByteBuffer _allocate(int length) native "ByteBuffer_allocate";
171
172 int _length() native "ByteBuffer_getLength";
173
174 int _getInt8(int byteOffset) native "ByteBuffer_getInt8";
175 void _setInt8(int byteOffset, int value) native "ByteBuffer_setInt8";
176
177 int _getUint8(int byteOffset) native "ByteBuffer_getUint8";
178 void _setUint8(int byteOffset, int value) native "ByteBuffer_setUint8";
179
180 int _getInt16(int byteOffset) native "ByteBuffer_getInt16";
181 void _setInt16(int byteOffset, int value) native "ByteBuffer_setInt16";
182
183 int _getUint16(int byteOffset) native "ByteBuffer_getUint16";
184 void _setUint16(int byteOffset, int value) native "ByteBuffer_setUint16";
185
186 int _getInt32(int byteOffset) native "ByteBuffer_getInt32";
187 void _setInt32(int byteOffset, int value) native "ByteBuffer_setInt32";
188
189 int _getUint32(int byteOffset) native "ByteBuffer_getUint32";
190 void _setUint32(int byteOffset, int value) native "ByteBuffer_setUint32";
191
192 int _getInt64(int byteOffset) native "ByteBuffer_getInt64";
193 void _setInt64(int byteOffset, int value) native "ByteBuffer_setInt64";
194
195 int _getUint64(int byteOffset) native "ByteBuffer_getUint64";
196 void _setUint64(int byteOffset, int value) native "ByteBuffer_setUint64";
197
198 double _getFloat32(int byteOffset) native "ByteBuffer_getFloat32";
199 void _setFloat32(int byteOffset, double value) native "ByteBuffer_setFloat32";
200
201 double _getFloat64(int byteOffset) native "ByteBuffer_getFloat64";
202 void _setFloat64(int byteOffset, double value) native "ByteBuffer_setFloat64";
203 }
204
205
206 class ByteBufferIterator implements Iterator {
207 ByteBufferIterator(List byteBuffer)
208 : _byteBuffer = byteBuffer, _length = byteBuffer.length, _pos = 0 {
209 assert(byteBuffer is ByteBuffer);
210 }
211
212 bool hasNext() {
213 return _length > _pos;
214 }
215
216 int next() {
217 if (!hasNext()) {
218 throw const NoMoreElementsException();
219 }
220 return _byteBuffer[_pos++];
221 }
222
223 final List _byteBuffer;
224 final int _length; // Cache byte buffer length for faster access.
225 int _pos;
226 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698