| 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_ = []; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 * Seek relative to the end of the buffer. | 42 * Seek relative to the end of the buffer. |
| 43 * @const | 43 * @const |
| 44 * @type {number} | 44 * @type {number} |
| 45 */ | 45 */ |
| 46 ByteReader.SEEK_END = 2; | 46 ByteReader.SEEK_END = 2; |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * 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). |
| 50 * | 50 * |
| 51 * Static utility function. | 51 * Static utility function. |
| 52 * | |
| 53 * @param {number} pos //TODO(JSDOC). | |
| 54 * @param {number} size //TODO(JSDOC). | |
| 55 * @param {number} end //TODO(JSDOC). | |
| 56 */ | 52 */ |
| 57 ByteReader.validateRead = function(pos, size, end) { | 53 ByteReader.validateRead = function(pos, size, end) { |
| 58 if (pos < 0 || pos >= end) | 54 if (pos < 0 || pos >= end) |
| 59 throw new Error('Invalid read position'); | 55 throw new Error('Invalid read position'); |
| 60 | 56 |
| 61 if (pos + size > end) | 57 if (pos + size > end) |
| 62 throw new Error('Read past end of buffer'); | 58 throw new Error('Read past end of buffer'); |
| 63 }; | 59 }; |
| 64 | 60 |
| 65 /** | 61 /** |
| 66 * Read as a sequence of characters, returning them as a single string. | 62 * Read as a sequence of characters, returning them as a single string. |
| 67 * | 63 * |
| 68 * This is a static utility function. There is a member function with the | 64 * This is a static utility function. There is a member function with the |
| 69 * same name which side-effects the current read position. | 65 * same name which side-effects the current read position. |
| 70 * | |
| 71 * @param {DataView} dataView //TODO(JSDOC). | |
| 72 * @param {number} pos //TODO(JSDOC). | |
| 73 * @param {number} size //TODO(JSDOC). | |
| 74 * @param {number=} opt_end //TODO(JSDOC). | |
| 75 * @return {string} //TODO(JSDOC). | 66 * @return {string} //TODO(JSDOC). |
| 76 */ | 67 */ |
| 77 ByteReader.readString = function(dataView, pos, size, opt_end) { | 68 ByteReader.readString = function(dataView, pos, size, opt_end) { |
| 78 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); | 69 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); |
| 79 | 70 |
| 80 var codes = []; | 71 var codes = []; |
| 81 | 72 |
| 82 for (var i = 0; i < size; ++i) | 73 for (var i = 0; i < size; ++i) |
| 83 codes.push(dataView.getUint8(pos + i)); | 74 codes.push(dataView.getUint8(pos + i)); |
| 84 | 75 |
| 85 return String.fromCharCode.apply(null, codes); | 76 return String.fromCharCode.apply(null, codes); |
| 86 }; | 77 }; |
| 87 | 78 |
| 88 /** | 79 /** |
| 89 * Read as a sequence of characters, returning them as a single string. | 80 * Read as a sequence of characters, returning them as a single string. |
| 90 * | 81 * |
| 91 * This is a static utility function. There is a member function with the | 82 * This is a static utility function. There is a member function with the |
| 92 * same name which side-effects the current read position. | 83 * same name which side-effects the current read position. |
| 93 * | |
| 94 * @param {DataView} dataView //TODO(JSDOC). | |
| 95 * @param {number} pos //TODO(JSDOC). | |
| 96 * @param {number} size //TODO(JSDOC). | |
| 97 * @param {number=} opt_end //TODO(JSDOC). | |
| 98 * @return {string} //TODO(JSDOC). | 84 * @return {string} //TODO(JSDOC). |
| 99 */ | 85 */ |
| 100 ByteReader.readNullTerminatedString = function(dataView, pos, size, opt_end) { | 86 ByteReader.readNullTerminatedString = function(dataView, pos, size, opt_end) { |
| 101 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); | 87 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); |
| 102 | 88 |
| 103 var codes = []; | 89 var codes = []; |
| 104 | 90 |
| 105 for (var i = 0; i < size; ++i) { | 91 for (var i = 0; i < size; ++i) { |
| 106 var code = dataView.getUint8(pos + i); | 92 var code = dataView.getUint8(pos + i); |
| 107 if (code == 0) break; | 93 if (code == 0) break; |
| 108 codes.push(code); | 94 codes.push(code); |
| 109 } | 95 } |
| 110 | 96 |
| 111 return String.fromCharCode.apply(null, codes); | 97 return String.fromCharCode.apply(null, codes); |
| 112 }; | 98 }; |
| 113 | 99 |
| 114 /** | 100 /** |
| 115 * Read as a sequence of UTF16 characters, returning them as a single string. | 101 * Read as a sequence of UTF16 characters, returning them as a single string. |
| 116 * | 102 * |
| 117 * This is a static utility function. There is a member function with the | 103 * This is a static utility function. There is a member function with the |
| 118 * same name which side-effects the current read position. | 104 * same name which side-effects the current read position. |
| 119 * | |
| 120 * @param {DataView} dataView //TODO(JSDOC). | |
| 121 * @param {number} pos //TODO(JSDOC). | |
| 122 * @param {boolean} bom //TODO(JSDOC). | |
| 123 * @param {number} size //TODO(JSDOC). | |
| 124 * @param {number=} opt_end //TODO(JSDOC). | |
| 125 * @return {string} //TODO(JSDOC). | 105 * @return {string} //TODO(JSDOC). |
| 126 */ | 106 */ |
| 127 ByteReader.readNullTerminatedStringUTF16 = function( | 107 ByteReader.readNullTerminatedStringUTF16 = function( |
| 128 dataView, pos, bom, size, opt_end) { | 108 dataView, pos, bom, size, opt_end) { |
| 129 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); | 109 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); |
| 130 | 110 |
| 131 var littleEndian = false; | 111 var littleEndian = false; |
| 132 var start = 0; | 112 var start = 0; |
| 133 | 113 |
| 134 if (bom) { | 114 if (bom) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 ByteReader.base64Alphabet_ = | 135 ByteReader.base64Alphabet_ = |
| 156 ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'). | 136 ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'). |
| 157 split(''); | 137 split(''); |
| 158 | 138 |
| 159 /** | 139 /** |
| 160 * Read as a sequence of bytes, returning them as a single base64 encoded | 140 * Read as a sequence of bytes, returning them as a single base64 encoded |
| 161 * string. | 141 * string. |
| 162 * | 142 * |
| 163 * This is a static utility function. There is a member function with the | 143 * This is a static utility function. There is a member function with the |
| 164 * same name which side-effects the current read position. | 144 * same name which side-effects the current read position. |
| 165 * | |
| 166 * @param {DataView} dataView //TODO(JSDOC). | |
| 167 * @param {number} pos //TODO(JSDOC). | |
| 168 * @param {number} size //TODO(JSDOC). | |
| 169 * @param {number=} opt_end //TODO(JSDOC). | |
| 170 * @return {string} //TODO(JSDOC). | 145 * @return {string} //TODO(JSDOC). |
| 171 */ | 146 */ |
| 172 ByteReader.readBase64 = function(dataView, pos, size, opt_end) { | 147 ByteReader.readBase64 = function(dataView, pos, size, opt_end) { |
| 173 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); | 148 ByteReader.validateRead(pos, size, opt_end || dataView.byteLength); |
| 174 | 149 |
| 175 var rv = []; | 150 var rv = []; |
| 176 var chars = []; | 151 var chars = []; |
| 177 var padding = 0; | 152 var padding = 0; |
| 178 | 153 |
| 179 for (var i = 0; i < size; /* incremented inside */) { | 154 for (var i = 0; i < size; /* incremented inside */) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 205 rv[rv.length - 2] = '='; | 180 rv[rv.length - 2] = '='; |
| 206 | 181 |
| 207 return rv.join(''); | 182 return rv.join(''); |
| 208 }; | 183 }; |
| 209 | 184 |
| 210 /** | 185 /** |
| 211 * Read as an image encoded in a data url. | 186 * Read as an image encoded in a data url. |
| 212 * | 187 * |
| 213 * This is a static utility function. There is a member function with the | 188 * This is a static utility function. There is a member function with the |
| 214 * same name which side-effects the current read position. | 189 * same name which side-effects the current read position. |
| 215 * | |
| 216 * @param {DataView} dataView //TODO(JSDOC). | |
| 217 * @param {number} pos //TODO(JSDOC). | |
| 218 * @param {number} size //TODO(JSDOC). | |
| 219 * @param {number=} opt_end //TODO(JSDOC). | |
| 220 * @return {string} //TODO(JSDOC). | 190 * @return {string} //TODO(JSDOC). |
| 221 */ | 191 */ |
| 222 ByteReader.readImage = function(dataView, pos, size, opt_end) { | 192 ByteReader.readImage = function(dataView, pos, size, opt_end) { |
| 223 opt_end = opt_end || dataView.byteLength; | 193 opt_end = opt_end || dataView.byteLength; |
| 224 ByteReader.validateRead(pos, size, opt_end); | 194 ByteReader.validateRead(pos, size, opt_end); |
| 225 | 195 |
| 226 // Two bytes is enough to identify the mime type. | 196 // Two bytes is enough to identify the mime type. |
| 227 var prefixToMime = { | 197 var prefixToMime = { |
| 228 '\x89P' : 'png', | 198 '\x89P' : 'png', |
| 229 '\xFF\xD8' : 'jpeg', | 199 '\xFF\xD8' : 'jpeg', |
| 230 'BM' : 'bmp', | 200 'BM' : 'bmp', |
| 231 'GI' : 'gif' | 201 'GI' : 'gif' |
| 232 }; | 202 }; |
| 233 | 203 |
| 234 var prefix = ByteReader.readString(dataView, pos, 2, opt_end); | 204 var prefix = ByteReader.readString(dataView, pos, 2, opt_end); |
| 235 var mime = prefixToMime[prefix] || | 205 var mime = prefixToMime[prefix] || |
| 236 dataView.getUint16(pos, false).toString(16); // For debugging. | 206 dataView.getUint16(pos, false).toString(16); // For debugging. |
| 237 | 207 |
| 238 var b64 = ByteReader.readBase64(dataView, pos, size, opt_end); | 208 var b64 = ByteReader.readBase64(dataView, pos, size, opt_end); |
| 239 return 'data:image/' + mime + ';base64,' + b64; | 209 return 'data:image/' + mime + ';base64,' + b64; |
| 240 }; | 210 }; |
| 241 | 211 |
| 242 // Instance methods. | 212 // Instance methods. |
| 243 | 213 |
| 244 /** | 214 /** |
| 245 * Return true if the requested number of bytes can be read from the buffer. | 215 * Return true if the requested number of bytes can be read from the buffer. |
| 246 * | |
| 247 * @param {number} size //TODO(JSDOC). | |
| 248 * @return {boolean} //TODO(JSDOC). | 216 * @return {boolean} //TODO(JSDOC). |
| 249 */ | 217 */ |
| 250 ByteReader.prototype.canRead = function(size) { | 218 ByteReader.prototype.canRead = function(size) { |
| 251 return this.pos_ + size <= this.view_.byteLength; | 219 return this.pos_ + size <= this.view_.byteLength; |
| 252 }; | 220 }; |
| 253 | 221 |
| 254 /** | 222 /** |
| 255 * Return true if the current position is past the end of the buffer. | 223 * Return true if the current position is past the end of the buffer. |
| 256 * @return {boolean} //TODO(JSDOC). | 224 * @return {boolean} //TODO(JSDOC). |
| 257 */ | 225 */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 270 /** | 238 /** |
| 271 * Return true if the current position is outside the buffer. | 239 * Return true if the current position is outside the buffer. |
| 272 * @return {boolean} //TODO(JSDOC). | 240 * @return {boolean} //TODO(JSDOC). |
| 273 */ | 241 */ |
| 274 ByteReader.prototype.beof = function() { | 242 ByteReader.prototype.beof = function() { |
| 275 return this.pos_ >= this.view_.byteLength || this.pos_ < 0; | 243 return this.pos_ >= this.view_.byteLength || this.pos_ < 0; |
| 276 }; | 244 }; |
| 277 | 245 |
| 278 /** | 246 /** |
| 279 * Set the expected byte ordering for future reads. | 247 * Set the expected byte ordering for future reads. |
| 280 * @param {number} order //TODO(JSDOC). | |
| 281 */ | 248 */ |
| 282 ByteReader.prototype.setByteOrder = function(order) { | 249 ByteReader.prototype.setByteOrder = function(order) { |
| 283 this.littleEndian_ = order == ByteReader.LITTLE_ENDIAN; | 250 this.littleEndian_ = order == ByteReader.LITTLE_ENDIAN; |
| 284 }; | 251 }; |
| 285 | 252 |
| 286 /** | 253 /** |
| 287 * Throw an error if the reader is at an invalid position, or if a read a read | 254 * Throw an error if the reader is at an invalid position, or if a read a read |
| 288 * of |size| would put it in one. | 255 * of |size| would put it in one. |
| 289 * | 256 * |
| 290 * You may optionally pass opt_end to override what is considered to be the | 257 * You may optionally pass opt_end to override what is considered to be the |
| 291 * end of the buffer. | 258 * end of the buffer. |
| 292 * | |
| 293 * @param {number} size //TODO(JSDOC). | |
| 294 * @param {number=} opt_end //TODO(JSDOC). | |
| 295 */ | 259 */ |
| 296 ByteReader.prototype.validateRead = function(size, opt_end) { | 260 ByteReader.prototype.validateRead = function(size, opt_end) { |
| 297 if (typeof opt_end == 'undefined') | 261 if (typeof opt_end == 'undefined') |
| 298 opt_end = this.view_.byteLength; | 262 opt_end = this.view_.byteLength; |
| 299 | 263 |
| 300 ByteReader.validateRead(this.view_, this.pos_, size, opt_end); | 264 ByteReader.validateRead(this.view_, this.pos_, size, opt_end); |
| 301 }; | 265 }; |
| 302 | 266 |
| 303 /** | 267 /** |
| 304 * @param {number} width //TODO(JSDOC). | 268 * @param {number} width //TODO(JSDOC). |
| (...skipping 30 matching lines...) Expand all Loading... |
| 335 var rv = this.view_[method](this.pos_, this.littleEndian_); | 299 var rv = this.view_[method](this.pos_, this.littleEndian_); |
| 336 this.pos_ += width; | 300 this.pos_ += width; |
| 337 return rv; | 301 return rv; |
| 338 }; | 302 }; |
| 339 | 303 |
| 340 /** | 304 /** |
| 341 * Read as a sequence of characters, returning them as a single string. | 305 * Read as a sequence of characters, returning them as a single string. |
| 342 * | 306 * |
| 343 * Adjusts the current position on success. Throws an exception if the | 307 * Adjusts the current position on success. Throws an exception if the |
| 344 * read would go past the end of the buffer. | 308 * read would go past the end of the buffer. |
| 345 * | |
| 346 * @param {number} size //TODO(JSDOC). | |
| 347 * @param {number=} opt_end //TODO(JSDOC). | |
| 348 * @return {string} //TODO(JSDOC). | 309 * @return {string} //TODO(JSDOC). |
| 349 */ | 310 */ |
| 350 ByteReader.prototype.readString = function(size, opt_end) { | 311 ByteReader.prototype.readString = function(size, opt_end) { |
| 351 var rv = ByteReader.readString(this.view_, this.pos_, size, opt_end); | 312 var rv = ByteReader.readString(this.view_, this.pos_, size, opt_end); |
| 352 this.pos_ += size; | 313 this.pos_ += size; |
| 353 return rv; | 314 return rv; |
| 354 }; | 315 }; |
| 355 | 316 |
| 356 | 317 |
| 357 /** | 318 /** |
| 358 * Read as a sequence of characters, returning them as a single string. | 319 * Read as a sequence of characters, returning them as a single string. |
| 359 * | 320 * |
| 360 * Adjusts the current position on success. Throws an exception if the | 321 * Adjusts the current position on success. Throws an exception if the |
| 361 * read would go past the end of the buffer. | 322 * read would go past the end of the buffer. |
| 362 * | |
| 363 * @param {number} size //TODO(JSDOC). | |
| 364 * @param {number=} opt_end //TODO(JSDOC). | |
| 365 * @return {string} //TODO(JSDOC). | 323 * @return {string} //TODO(JSDOC). |
| 366 */ | 324 */ |
| 367 ByteReader.prototype.readNullTerminatedString = function(size, opt_end) { | 325 ByteReader.prototype.readNullTerminatedString = function(size, opt_end) { |
| 368 var rv = ByteReader.readNullTerminatedString(this.view_, | 326 var rv = ByteReader.readNullTerminatedString(this.view_, |
| 369 this.pos_, | 327 this.pos_, |
| 370 size, | 328 size, |
| 371 opt_end); | 329 opt_end); |
| 372 this.pos_ += rv.length; | 330 this.pos_ += rv.length; |
| 373 | 331 |
| 374 if (rv.length < size) { | 332 if (rv.length < size) { |
| 375 // If we've stopped reading because we found '0' but didn't hit size limit | 333 // If we've stopped reading because we found '0' but didn't hit size limit |
| 376 // then we should skip additional '0' character | 334 // then we should skip additional '0' character |
| 377 this.pos_++; | 335 this.pos_++; |
| 378 } | 336 } |
| 379 | 337 |
| 380 return rv; | 338 return rv; |
| 381 }; | 339 }; |
| 382 | 340 |
| 383 | 341 |
| 384 /** | 342 /** |
| 385 * Read as a sequence of UTF16 characters, returning them as a single string. | 343 * Read as a sequence of UTF16 characters, returning them as a single string. |
| 386 * | 344 * |
| 387 * Adjusts the current position on success. Throws an exception if the | 345 * Adjusts the current position on success. Throws an exception if the |
| 388 * read would go past the end of the buffer. | 346 * read would go past the end of the buffer. |
| 389 * | |
| 390 * @param {boolean} bom //TODO(JSDOC). | |
| 391 * @param {number} size //TODO(JSDOC). | |
| 392 * @param {number=} opt_end //TODO(JSDOC). | |
| 393 * @return {string} //TODO(JSDOC). | 347 * @return {string} //TODO(JSDOC). |
| 394 */ | 348 */ |
| 395 ByteReader.prototype.readNullTerminatedStringUTF16 = | 349 ByteReader.prototype.readNullTerminatedStringUTF16 = |
| 396 function(bom, size, opt_end) { | 350 function(bom, size, opt_end) { |
| 397 var rv = ByteReader.readNullTerminatedStringUTF16( | 351 var rv = ByteReader.readNullTerminatedStringUTF16( |
| 398 this.view_, this.pos_, bom, size, opt_end); | 352 this.view_, this.pos_, bom, size, opt_end); |
| 399 | 353 |
| 400 if (bom) { | 354 if (bom) { |
| 401 // If the BOM word was present advance the position. | 355 // If the BOM word was present advance the position. |
| 402 this.pos_ += 2; | 356 this.pos_ += 2; |
| 403 } | 357 } |
| 404 | 358 |
| 405 this.pos_ += rv.length; | 359 this.pos_ += rv.length; |
| 406 | 360 |
| 407 if (rv.length < size) { | 361 if (rv.length < size) { |
| 408 // If we've stopped reading because we found '0' but didn't hit size limit | 362 // If we've stopped reading because we found '0' but didn't hit size limit |
| 409 // then we should skip additional '0' character | 363 // then we should skip additional '0' character |
| 410 this.pos_ += 2; | 364 this.pos_ += 2; |
| 411 } | 365 } |
| 412 | 366 |
| 413 return rv; | 367 return rv; |
| 414 }; | 368 }; |
| 415 | 369 |
| 416 | 370 |
| 417 /** | 371 /** |
| 418 * Read as an array of numbers. | 372 * Read as an array of numbers. |
| 419 * | 373 * |
| 420 * Adjusts the current position on success. Throws an exception if the | 374 * Adjusts the current position on success. Throws an exception if the |
| 421 * read would go past the end of the buffer. | 375 * read would go past the end of the buffer. |
| 422 * | |
| 423 * @param {number} size //TODO(JSDOC). | |
| 424 * @param {number=} opt_end //TODO(JSDOC). | |
| 425 * @param {function(new:Array.<*>)=} opt_arrayConstructor //TODO(JSDOC). | |
| 426 * @return {Array.<*>} //TODO(JSDOC). | 376 * @return {Array.<*>} //TODO(JSDOC). |
| 427 */ | 377 */ |
| 428 ByteReader.prototype.readSlice = function(size, opt_end, | 378 ByteReader.prototype.readSlice = function(size, opt_end, |
| 429 opt_arrayConstructor) { | 379 opt_arrayConstructor) { |
| 430 this.validateRead(size, opt_end); | 380 this.validateRead(size, opt_end); |
| 431 | 381 |
| 432 var arrayConstructor = opt_arrayConstructor || Uint8Array; | 382 var arrayConstructor = opt_arrayConstructor || Uint8Array; |
| 433 var slice = new arrayConstructor( | 383 var slice = new arrayConstructor( |
| 434 this.view_.buffer, this.view_.byteOffset + this.pos, size); | 384 this.view_.buffer, this.view_.byteOffset + this.pos, size); |
| 435 this.pos_ += size; | 385 this.pos_ += size; |
| 436 | 386 |
| 437 return slice; | 387 return slice; |
| 438 }; | 388 }; |
| 439 | 389 |
| 440 /** | 390 /** |
| 441 * Read as a sequence of bytes, returning them as a single base64 encoded | 391 * Read as a sequence of bytes, returning them as a single base64 encoded |
| 442 * string. | 392 * string. |
| 443 * | 393 * |
| 444 * Adjusts the current position on success. Throws an exception if the | 394 * Adjusts the current position on success. Throws an exception if the |
| 445 * read would go past the end of the buffer. | 395 * read would go past the end of the buffer. |
| 446 * | |
| 447 * @param {number} size //TODO(JSDOC). | |
| 448 * @param {number=} opt_end //TODO(JSDOC). | |
| 449 * @return {string} //TODO(JSDOC). | 396 * @return {string} //TODO(JSDOC). |
| 450 */ | 397 */ |
| 451 ByteReader.prototype.readBase64 = function(size, opt_end) { | 398 ByteReader.prototype.readBase64 = function(size, opt_end) { |
| 452 var rv = ByteReader.readBase64(this.view_, this.pos_, size, opt_end); | 399 var rv = ByteReader.readBase64(this.view_, this.pos_, size, opt_end); |
| 453 this.pos_ += size; | 400 this.pos_ += size; |
| 454 return rv; | 401 return rv; |
| 455 }; | 402 }; |
| 456 | 403 |
| 457 /** | 404 /** |
| 458 * Read an image returning it as a data url. | 405 * Read an image returning it as a data url. |
| 459 * | 406 * |
| 460 * Adjusts the current position on success. Throws an exception if the | 407 * Adjusts the current position on success. Throws an exception if the |
| 461 * read would go past the end of the buffer. | 408 * read would go past the end of the buffer. |
| 462 * | |
| 463 * @param {number} size //TODO(JSDOC). | |
| 464 * @param {number=} opt_end //TODO(JSDOC). | |
| 465 * @return {string} //TODO(JSDOC). | 409 * @return {string} //TODO(JSDOC). |
| 466 */ | 410 */ |
| 467 ByteReader.prototype.readImage = function(size, opt_end) { | 411 ByteReader.prototype.readImage = function(size, opt_end) { |
| 468 var rv = ByteReader.readImage(this.view_, this.pos_, size, opt_end); | 412 var rv = ByteReader.readImage(this.view_, this.pos_, size, opt_end); |
| 469 this.pos_ += size; | 413 this.pos_ += size; |
| 470 return rv; | 414 return rv; |
| 471 }; | 415 }; |
| 472 | 416 |
| 473 /** | 417 /** |
| 474 * Seek to a give position relative to opt_seekStart. | 418 * Seek to a give position relative to opt_seekStart. |
| 475 * | |
| 476 * @param {number} pos //TODO(JSDOC). | |
| 477 * @param {number=} opt_seekStart //TODO(JSDOC). | |
| 478 * @param {number=} opt_end //TODO(JSDOC). | |
| 479 */ | 419 */ |
| 480 ByteReader.prototype.seek = function(pos, opt_seekStart, opt_end) { | 420 ByteReader.prototype.seek = function(pos, opt_seekStart, opt_end) { |
| 481 opt_end = opt_end || this.view_.byteLength; | 421 opt_end = opt_end || this.view_.byteLength; |
| 482 | 422 |
| 483 var newPos; | 423 var newPos; |
| 484 if (opt_seekStart == ByteReader.SEEK_CUR) { | 424 if (opt_seekStart == ByteReader.SEEK_CUR) { |
| 485 newPos = this.pos_ + pos; | 425 newPos = this.pos_ + pos; |
| 486 } else if (opt_seekStart == ByteReader.SEEK_END) { | 426 } else if (opt_seekStart == ByteReader.SEEK_END) { |
| 487 newPos = opt_end + pos; | 427 newPos = opt_end + pos; |
| 488 } else { | 428 } else { |
| 489 newPos = pos; | 429 newPos = pos; |
| 490 } | 430 } |
| 491 | 431 |
| 492 if (newPos < 0 || newPos > this.view_.byteLength) | 432 if (newPos < 0 || newPos > this.view_.byteLength) |
| 493 throw new Error('Seek outside of buffer: ' + (newPos - opt_end)); | 433 throw new Error('Seek outside of buffer: ' + (newPos - opt_end)); |
| 494 | 434 |
| 495 this.pos_ = newPos; | 435 this.pos_ = newPos; |
| 496 }; | 436 }; |
| 497 | 437 |
| 498 /** | 438 /** |
| 499 * Seek to a given position relative to opt_seekStart, saving the current | 439 * Seek to a given position relative to opt_seekStart, saving the current |
| 500 * position. | 440 * position. |
| 501 * | 441 * |
| 502 * Recover the current position with a call to seekPop. | 442 * Recover the current position with a call to seekPop. |
| 503 * | |
| 504 * @param {number} pos //TODO(JSDOC). | |
| 505 * @param {number=} opt_seekStart //TODO(JSDOC). | |
| 506 */ | 443 */ |
| 507 ByteReader.prototype.pushSeek = function(pos, opt_seekStart) { | 444 ByteReader.prototype.pushSeek = function(pos, opt_seekStart) { |
| 508 var oldPos = this.pos_; | 445 var oldPos = this.pos_; |
| 509 this.seek(pos, opt_seekStart); | 446 this.seek(pos, opt_seekStart); |
| 510 // Alter the seekStack_ after the call to seek(), in case it throws. | 447 // Alter the seekStack_ after the call to seek(), in case it throws. |
| 511 this.seekStack_.push(oldPos); | 448 this.seekStack_.push(oldPos); |
| 512 }; | 449 }; |
| 513 | 450 |
| 514 /** | 451 /** |
| 515 * Undo a previous seekPush. | 452 * Undo a previous seekPush. |
| 516 */ | 453 */ |
| 517 ByteReader.prototype.popSeek = function() { | 454 ByteReader.prototype.popSeek = function() { |
| 518 this.seek(this.seekStack_.pop()); | 455 this.seek(this.seekStack_.pop()); |
| 519 }; | 456 }; |
| 520 | 457 |
| 521 /** | 458 /** |
| 522 * Return the current read position. | 459 * Return the current read position. |
| 523 * @return {number} //TODO(JSDOC). | 460 * @return {number} //TODO(JSDOC). |
| 524 */ | 461 */ |
| 525 ByteReader.prototype.tell = function() { | 462 ByteReader.prototype.tell = function() { |
| 526 return this.pos_; | 463 return this.pos_; |
| 527 }; | 464 }; |
| OLD | NEW |