| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 function ByteReader(arrayBuffer, opt_offset, opt_length) { | 5 function ByteReader(arrayBuffer, opt_offset, opt_length) { |
| 6 opt_offset = opt_offset || 0; | 6 opt_offset = opt_offset || 0; |
| 7 opt_length = opt_length || (arrayBuffer.byteLength - opt_offset); | 7 opt_length = opt_length || (arrayBuffer.byteLength - opt_offset); |
| 8 this.view_ = new DataView(arrayBuffer, opt_offset, opt_length); | 8 this.view_ = new DataView(arrayBuffer, opt_offset, opt_length); |
| 9 this.pos_ = 0; | 9 this.pos_ = 0; |
| 10 this.seekStack_ = []; | 10 this.seekStack_ = []; |
| 11 this.setByteOrder(ByteReader.BIG_ENDIAN); | 11 this.setByteOrder(ByteReader.BIG_ENDIAN); |
| 12 } | 12 } |
| 13 | 13 |
| 14 // Static const and methods. | 14 // Static const and methods. |
| 15 | 15 |
| 16 ByteReader.LITTLE_ENDIAN = 0; // Intel, 0x1234 is [0x34, 0x12] | 16 /** |
| 17 ByteReader.BIG_ENDIAN = 1; // Motorola, 0x1234 is [0x12, 0x34] | 17 * Intel, 0x1234 is [0x34, 0x12] |
| 18 * @const |
| 19 * @type {number} |
| 20 */ |
| 21 ByteReader.LITTLE_ENDIAN = 0; |
| 22 /** |
| 23 * Motorola, 0x1234 is [0x12, 0x34] |
| 24 * @const |
| 25 * @type {number} |
| 26 */ |
| 27 ByteReader.BIG_ENDIAN = 1; |
| 18 | 28 |
| 19 ByteReader.SEEK_BEG = 0; // Seek relative to the beginning of the buffer. | 29 /** |
| 20 ByteReader.SEEK_CUR = 1; // Seek relative to the current position. | 30 * Seek relative to the beginning of the buffer. |
| 21 ByteReader.SEEK_END = 2; // Seek relative to the end of the buffer. | 31 * @const |
| 32 * @type {number} |
| 33 */ |
| 34 ByteReader.SEEK_BEG = 0; |
| 35 /** |
| 36 * Seek relative to the current position. |
| 37 * @const |
| 38 * @type {number} |
| 39 */ |
| 40 ByteReader.SEEK_CUR = 1; |
| 41 /** |
| 42 * Seek relative to the end of the buffer. |
| 43 * @const |
| 44 * @type {number} |
| 45 */ |
| 46 ByteReader.SEEK_END = 2; |
| 22 | 47 |
| 23 /** | 48 /** |
| 24 * Throw an error if (0 > pos >= end) or if (pos + size > end). | 49 * Throw an error if (0 > pos >= end) or if (pos + size > end). |
| 25 * | 50 * |
| 26 * Static utility function. | 51 * Static utility function. |
| 27 */ | 52 */ |
| 28 ByteReader.validateRead = function(pos, size, end) { | 53 ByteReader.validateRead = function(pos, size, end) { |
| 29 if (pos < 0 || pos >= end) | 54 if (pos < 0 || pos >= end) |
| 30 throw new Error('Invalid read position'); | 55 throw new Error('Invalid read position'); |
| 31 | 56 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 117 |
| 93 for (var i = start; i < size; i += 2) { | 118 for (var i = start; i < size; i += 2) { |
| 94 var code = dataView.getUint16(pos + i, littleEndian); | 119 var code = dataView.getUint16(pos + i, littleEndian); |
| 95 if (code == 0) break; | 120 if (code == 0) break; |
| 96 codes.push(code); | 121 codes.push(code); |
| 97 } | 122 } |
| 98 | 123 |
| 99 return String.fromCharCode.apply(null, codes); | 124 return String.fromCharCode.apply(null, codes); |
| 100 }; | 125 }; |
| 101 | 126 |
| 127 /** |
| 128 * @const |
| 129 * @type {Array.<string>} |
| 130 * @private |
| 131 */ |
| 102 ByteReader.base64Alphabet_ = | 132 ByteReader.base64Alphabet_ = |
| 103 ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'). | 133 ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'). |
| 104 split(''); | 134 split(''); |
| 105 | 135 |
| 106 /** | 136 /** |
| 107 * Read as a sequence of bytes, returning them as a single base64 encoded | 137 * Read as a sequence of bytes, returning them as a single base64 encoded |
| 108 * string. | 138 * string. |
| 109 * | 139 * |
| 110 * This is a static utility function. There is a member function with the | 140 * This is a static utility function. There is a member function with the |
| 111 * same name which side-effects the current read position. | 141 * same name which side-effects the current read position. |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 * You may optionally pass opt_end to override what is considered to be the | 248 * You may optionally pass opt_end to override what is considered to be the |
| 219 * end of the buffer. | 249 * end of the buffer. |
| 220 */ | 250 */ |
| 221 ByteReader.prototype.validateRead = function(size, opt_end) { | 251 ByteReader.prototype.validateRead = function(size, opt_end) { |
| 222 if (typeof opt_end == 'undefined') | 252 if (typeof opt_end == 'undefined') |
| 223 opt_end = this.view_.byteLength; | 253 opt_end = this.view_.byteLength; |
| 224 | 254 |
| 225 ByteReader.validateRead(this.view_, this.pos_, size, opt_end); | 255 ByteReader.validateRead(this.view_, this.pos_, size, opt_end); |
| 226 }; | 256 }; |
| 227 | 257 |
| 258 /** |
| 259 * @param {number} width //TODO(JSDOC). |
| 260 * @param {boolean=} opt_signed //TODO(JSDOC). |
| 261 * @param {number=} opt_end //TODO(JSDOC). |
| 262 * @return {string} //TODO(JSDOC). |
| 263 */ |
| 228 ByteReader.prototype.readScalar = function(width, opt_signed, opt_end) { | 264 ByteReader.prototype.readScalar = function(width, opt_signed, opt_end) { |
| 229 var method = opt_signed ? 'getInt' : 'getUint'; | 265 var method = opt_signed ? 'getInt' : 'getUint'; |
| 230 | 266 |
| 231 switch (width) { | 267 switch (width) { |
| 232 case 1: | 268 case 1: |
| 233 method += '8'; | 269 method += '8'; |
| 234 break; | 270 break; |
| 235 | 271 |
| 236 case 2: | 272 case 2: |
| 237 method += '16'; | 273 method += '16'; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 ByteReader.prototype.popSeek = function() { | 439 ByteReader.prototype.popSeek = function() { |
| 404 this.seek(this.seekStack_.pop()); | 440 this.seek(this.seekStack_.pop()); |
| 405 }; | 441 }; |
| 406 | 442 |
| 407 /** | 443 /** |
| 408 * Return the current read position. | 444 * Return the current read position. |
| 409 */ | 445 */ |
| 410 ByteReader.prototype.tell = function() { | 446 ByteReader.prototype.tell = function() { |
| 411 return this.pos_; | 447 return this.pos_; |
| 412 }; | 448 }; |
| OLD | NEW |