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 interface ByteArray extends List default _InternalByteArray { | 5 interface ByteArray extends List default _InternalByteArray { |
6 ByteArray(int length); | 6 ByteArray(int length); |
7 | 7 |
8 int get length(); | 8 int get length(); |
9 | 9 |
10 int getInt8(int byteOffset); | 10 int getInt8(int byteOffset); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 return Collections.some(this, f); | 89 return Collections.some(this, f); |
90 } | 90 } |
91 | 91 |
92 // List interface | 92 // List interface |
93 | 93 |
94 int operator[](int index) { | 94 int operator[](int index) { |
95 return getUint8(index); | 95 return getUint8(index); |
96 } | 96 } |
97 | 97 |
98 void operator[]=(int index, int value) { | 98 void operator[]=(int index, int value) { |
99 setUint8(index, value); | 99 if (value is int && value >= 0 && value <= 255) { |
| 100 setUint8(index, value); |
| 101 } else { |
| 102 throw const IllegalArgumentException( |
| 103 "Value is not an integer in the byte range"); |
| 104 } |
100 } | 105 } |
101 | 106 |
102 void set length(int newLength) { | 107 void set length(int newLength) { |
103 throw const UnsupportedOperationException("Cannot add to a byte array"); | 108 throw const UnsupportedOperationException("Cannot add to a byte array"); |
104 } | 109 } |
105 | 110 |
106 void add(int element) { | 111 void add(int element) { |
107 throw const UnsupportedOperationException("Cannot add to a byte array"); | 112 throw const UnsupportedOperationException("Cannot add to a byte array"); |
108 } | 113 } |
109 | 114 |
(...skipping 24 matching lines...) Expand all Loading... |
134 | 139 |
135 int removeLast() { | 140 int removeLast() { |
136 throw const UnsupportedOperationException( | 141 throw const UnsupportedOperationException( |
137 "Cannot remove from a byte array"); | 142 "Cannot remove from a byte array"); |
138 } | 143 } |
139 | 144 |
140 int last() { | 145 int last() { |
141 return this[length - 1]; | 146 return this[length - 1]; |
142 } | 147 } |
143 | 148 |
| 149 void setRange(int start, int length, List<int> from, [int startFrom = 0]) { |
| 150 if (length < 0) { |
| 151 throw new IllegalArgumentException("negative length $length"); |
| 152 } |
| 153 if (from is ByteArray) { |
| 154 _setRange(start, length, from, startFrom); |
| 155 } else { |
| 156 Arrays.copy(from, startFrom, this, start, length); |
| 157 } |
| 158 } |
| 159 |
| 160 List getRange(int start, int length) { |
| 161 if (length == 0) return []; |
| 162 Arrays.rangeCheck(this, start, length); |
| 163 ByteArray list = new ByteArray(length); |
| 164 list._setRange(0, length, this, start); |
| 165 return list; |
| 166 } |
| 167 |
| 168 |
144 // Implementation | 169 // Implementation |
145 | 170 |
146 int _length() native "ByteArray_getLength"; | 171 int _length() native "ByteArray_getLength"; |
| 172 |
| 173 void _setRange(int start, int length, ByteArray from, int startFrom) |
| 174 native "ByteArray_setRange"; |
| 175 |
147 } | 176 } |
148 | 177 |
149 | 178 |
150 class _ByteArrayIterator implements Iterator { | 179 class _ByteArrayIterator implements Iterator { |
151 _ByteArrayIterator(List byteArray) | 180 _ByteArrayIterator(List byteArray) |
152 : _byteArray = byteArray, _length = byteArray.length, _pos = 0 { | 181 : _byteArray = byteArray, _length = byteArray.length, _pos = 0 { |
153 assert(byteArray is ByteArray); | 182 assert(byteArray is ByteArray); |
154 } | 183 } |
155 | 184 |
156 bool hasNext() { | 185 bool hasNext() { |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 | 509 |
481 void _setFloat32(int byteOffset, double value) | 510 void _setFloat32(int byteOffset, double value) |
482 native "ExternalByteArray_setFloat32"; | 511 native "ExternalByteArray_setFloat32"; |
483 | 512 |
484 double _getFloat64(int byteOffset) | 513 double _getFloat64(int byteOffset) |
485 native "ExternalByteArray_getFloat64"; | 514 native "ExternalByteArray_getFloat64"; |
486 | 515 |
487 void _setFloat64(int byteOffset, double value) | 516 void _setFloat64(int byteOffset, double value) |
488 native "ExternalByteArray_setFloat64"; | 517 native "ExternalByteArray_setFloat64"; |
489 } | 518 } |
OLD | NEW |