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