OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 ByteReader = function(arrayBuffer) { | 5 ByteReader = function(arrayBuffer, opt_offset, opt_length) { |
6 this.buf_ = arrayBuffer; | 6 opt_offset = opt_offset || 0; |
7 this.view_ = new DataView(arrayBuffer); | 7 opt_length = opt_length || (arrayBuffer.byteLength - opt_offset); |
| 8 this.view_ = new DataView(arrayBuffer, opt_offset, opt_length); |
8 this.pos_ = 0; | 9 this.pos_ = 0; |
9 this.seekStack_ = []; | 10 this.seekStack_ = []; |
10 this.setByteOrder(ByteReader.BIG_ENDIAN); | 11 this.setByteOrder(ByteReader.BIG_ENDIAN); |
11 }; | 12 }; |
12 | 13 |
13 // Static const and methods. | 14 // Static const and methods. |
14 | 15 |
15 ByteReader.LITTLE_ENDIAN = 0; // Intel, 0x1234 is [0x34, 0x12] | 16 ByteReader.LITTLE_ENDIAN = 0; // Intel, 0x1234 is [0x34, 0x12] |
16 ByteReader.BIG_ENDIAN = 1; // Motorola, 0x1234 is [0x12, 0x34] | 17 ByteReader.BIG_ENDIAN = 1; // Motorola, 0x1234 is [0x12, 0x34] |
17 | 18 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 format = 'unknown'; | 138 format = 'unknown'; |
138 } | 139 } |
139 | 140 |
140 var b64 = ByteReader.readBase64(dataView, pos, size, opt_end); | 141 var b64 = ByteReader.readBase64(dataView, pos, size, opt_end); |
141 return 'data:image/' + format + ';base64,' + b64; | 142 return 'data:image/' + format + ';base64,' + b64; |
142 }; | 143 }; |
143 | 144 |
144 // Instance methods. | 145 // Instance methods. |
145 | 146 |
146 /** | 147 /** |
| 148 * Return true if the requested number of bytes can be read from the buffer. |
| 149 */ |
| 150 ByteReader.prototype.canRead = function(size) { |
| 151 return this.pos_ + size <= this.view_.byteLength; |
| 152 }, |
| 153 |
| 154 /** |
147 * Return true if the current position is past the end of the buffer. | 155 * Return true if the current position is past the end of the buffer. |
148 */ | 156 */ |
149 ByteReader.prototype.eof = function() { | 157 ByteReader.prototype.eof = function() { |
150 return this.pos_ >= this.view_.byteLength; | 158 return this.pos_ >= this.view_.byteLength; |
151 }; | 159 }; |
152 | 160 |
153 /** | 161 /** |
154 * Return true if the current position is before the beginning of the buffer. | 162 * Return true if the current position is before the beginning of the buffer. |
155 */ | 163 */ |
156 ByteReader.prototype.bof = function() { | 164 ByteReader.prototype.bof = function() { |
157 return this.pos_ < 0; | 165 return this.pos_ < 0; |
158 }; | 166 }; |
159 | 167 |
160 /** | 168 /** |
161 * Return true if the current position is oustide the buffer. | 169 * Return true if the current position is outside the buffer. |
162 */ | 170 */ |
163 ByteReader.prototype.beof = function() { | 171 ByteReader.prototype.beof = function() { |
164 return this.pos_ >= this.view_.byteLength || this.pos_ < 0; | 172 return this.pos_ >= this.view_.byteLength || this.pos_ < 0; |
165 }; | 173 }; |
166 | 174 |
167 /** | 175 /** |
168 * Set the expected byte ordering for future reads. | 176 * Set the expected byte ordering for future reads. |
169 */ | 177 */ |
170 ByteReader.prototype.setByteOrder = function(order) { | 178 ByteReader.prototype.setByteOrder = function(order) { |
171 this.littleEndian_ = order == ByteReader.LITTLE_ENDIAN; | 179 this.littleEndian_ = order == ByteReader.LITTLE_ENDIAN; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 * Read as an array of numbers. | 264 * Read as an array of numbers. |
257 * | 265 * |
258 * Adjusts the current position on success. Throws an exception if the | 266 * Adjusts the current position on success. Throws an exception if the |
259 * read would go past the end of the buffer. | 267 * read would go past the end of the buffer. |
260 */ | 268 */ |
261 ByteReader.prototype.readSlice = function(size, opt_end, | 269 ByteReader.prototype.readSlice = function(size, opt_end, |
262 opt_arrayConstructor) { | 270 opt_arrayConstructor) { |
263 this.validateRead(width, opt_end); | 271 this.validateRead(width, opt_end); |
264 | 272 |
265 var arrayConstructor = opt_arrayConstructor || Uint8Array; | 273 var arrayConstructor = opt_arrayConstructor || Uint8Array; |
266 var slice = new arrayConstructor(this.buf_, this.pos_, size); | 274 var slice = new arrayConstructor( |
| 275 this.view_.buffer, this.view_.byteOffset + this.pos, size); |
267 this.pos_ += size; | 276 this.pos_ += size; |
268 | 277 |
269 return slice; | 278 return slice; |
270 }; | 279 }; |
271 | 280 |
272 /** | 281 /** |
273 * Read as a sequence of bytes, returning them as a single base64 encoded | 282 * Read as a sequence of bytes, returning them as a single base64 encoded |
274 * string. | 283 * string. |
275 * | 284 * |
276 * Adjusts the current position on success. Throws an exception if the | 285 * Adjusts the current position on success. Throws an exception if the |
(...skipping 25 matching lines...) Expand all Loading... |
302 | 311 |
303 var newPos; | 312 var newPos; |
304 if (opt_seekStart == ByteReader.SEEK_CUR) { | 313 if (opt_seekStart == ByteReader.SEEK_CUR) { |
305 newPos = this.pos_ + pos; | 314 newPos = this.pos_ + pos; |
306 } else if (opt_seekStart == ByteReader.SEEK_END) { | 315 } else if (opt_seekStart == ByteReader.SEEK_END) { |
307 newPos = opt_end + pos; | 316 newPos = opt_end + pos; |
308 } else { | 317 } else { |
309 newPos = pos; | 318 newPos = pos; |
310 } | 319 } |
311 | 320 |
312 if (newPos < 0 || newPos >= this.view_.byteLength) | 321 if (newPos < 0 || newPos > this.view_.byteLength) |
313 throw new Error('Seek outside of buffer: ' + (newPos - opt_end)); | 322 throw new Error('Seek outside of buffer: ' + (newPos - opt_end)); |
314 | 323 |
315 this.pos_ = newPos; | 324 this.pos_ = newPos; |
316 }; | 325 }; |
317 | 326 |
318 /** | 327 /** |
319 * Seek to a given position relative to opt_seekStart, saving the current | 328 * Seek to a given position relative to opt_seekStart, saving the current |
320 * position. | 329 * position. |
321 * | 330 * |
322 * Recover the current position with a call to seekPop. | 331 * Recover the current position with a call to seekPop. |
(...skipping 11 matching lines...) Expand all Loading... |
334 ByteReader.prototype.popSeek = function() { | 343 ByteReader.prototype.popSeek = function() { |
335 this.seek(this.seekStack_.pop()); | 344 this.seek(this.seekStack_.pop()); |
336 }; | 345 }; |
337 | 346 |
338 /** | 347 /** |
339 * Return the current read position. | 348 * Return the current read position. |
340 */ | 349 */ |
341 ByteReader.prototype.tell = function() { | 350 ByteReader.prototype.tell = function() { |
342 return this.pos_; | 351 return this.pos_; |
343 }; | 352 }; |
OLD | NEW |