OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from. | 216 * @param {jspb.ByteSource=} opt_bytes The bytes we're reading from. |
217 * @param {number=} opt_start The optional offset to start reading at. | 217 * @param {number=} opt_start The optional offset to start reading at. |
218 * @param {number=} opt_length The optional length of the block to read - | 218 * @param {number=} opt_length The optional length of the block to read - |
219 * we'll throw an assertion if we go off the end of the block. | 219 * we'll throw an assertion if we go off the end of the block. |
220 * @constructor | 220 * @constructor |
221 * @struct | 221 * @struct |
222 */ | 222 */ |
223 jspb.BinaryDecoder = function(opt_bytes, opt_start, opt_length) { | 223 jspb.BinaryDecoder = function(opt_bytes, opt_start, opt_length) { |
224 /** | 224 /** |
225 * Typed byte-wise view of the source buffer. | 225 * Typed byte-wise view of the source buffer. |
226 * @private {Uint8Array} | 226 * @private {?Uint8Array} |
227 */ | 227 */ |
228 this.bytes_ = null; | 228 this.bytes_ = null; |
229 | 229 |
230 /** | 230 /** |
231 * Start point of the block to read. | 231 * Start point of the block to read. |
232 * @private {number} | 232 * @private {number} |
233 */ | 233 */ |
234 this.start_ = 0; | 234 this.start_ = 0; |
235 | 235 |
236 /** | 236 /** |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 this.bytes_ = null; | 328 this.bytes_ = null; |
329 this.start_ = 0; | 329 this.start_ = 0; |
330 this.end_ = 0; | 330 this.end_ = 0; |
331 this.cursor_ = 0; | 331 this.cursor_ = 0; |
332 this.error_ = false; | 332 this.error_ = false; |
333 }; | 333 }; |
334 | 334 |
335 | 335 |
336 /** | 336 /** |
337 * Returns the raw buffer. | 337 * Returns the raw buffer. |
338 * @return {Uint8Array} The raw buffer. | 338 * @return {?Uint8Array} The raw buffer. |
339 */ | 339 */ |
340 jspb.BinaryDecoder.prototype.getBuffer = function() { | 340 jspb.BinaryDecoder.prototype.getBuffer = function() { |
341 return this.bytes_; | 341 return this.bytes_; |
342 }; | 342 }; |
343 | 343 |
344 | 344 |
345 /** | 345 /** |
346 * Changes the block of bytes we're decoding. | 346 * Changes the block of bytes we're decoding. |
347 * @param {!jspb.ByteSource} data The bytes we're reading from. | 347 * @param {!jspb.ByteSource} data The bytes we're reading from. |
348 * @param {number=} opt_start The optional offset to start reading at. | 348 * @param {number=} opt_start The optional offset to start reading at. |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 * @return {string} The decoded unsigned 32-bit varint as a string. | 624 * @return {string} The decoded unsigned 32-bit varint as a string. |
625 */ | 625 */ |
626 jspb.BinaryDecoder.prototype.readUnsignedVarint32String = function() { | 626 jspb.BinaryDecoder.prototype.readUnsignedVarint32String = function() { |
627 // 32-bit integers fit in JavaScript numbers without loss of precision, so | 627 // 32-bit integers fit in JavaScript numbers without loss of precision, so |
628 // string variants of 32-bit varint readers can simply delegate then convert | 628 // string variants of 32-bit varint readers can simply delegate then convert |
629 // to string. | 629 // to string. |
630 var value = this.readUnsignedVarint32(); | 630 var value = this.readUnsignedVarint32(); |
631 return value.toString(); | 631 return value.toString(); |
632 }; | 632 }; |
633 | 633 |
| 634 |
634 /** | 635 /** |
635 * Reads a 32-bit signed variant and returns its value as a string. | 636 * Reads a 32-bit signed variant and returns its value as a string. |
636 * | 637 * |
637 * @return {string} The decoded signed 32-bit varint as a string. | 638 * @return {string} The decoded signed 32-bit varint as a string. |
638 */ | 639 */ |
639 jspb.BinaryDecoder.prototype.readSignedVarint32String = function() { | 640 jspb.BinaryDecoder.prototype.readSignedVarint32String = function() { |
640 // 32-bit integers fit in JavaScript numbers without loss of precision, so | 641 // 32-bit integers fit in JavaScript numbers without loss of precision, so |
641 // string variants of 32-bit varint readers can simply delegate then convert | 642 // string variants of 32-bit varint readers can simply delegate then convert |
642 // to string. | 643 // to string. |
643 var value = this.readSignedVarint32(); | 644 var value = this.readSignedVarint32(); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 jspb.BinaryDecoder.prototype.readStringWithLength = function() { | 944 jspb.BinaryDecoder.prototype.readStringWithLength = function() { |
944 var length = this.readUnsignedVarint32(); | 945 var length = this.readUnsignedVarint32(); |
945 return this.readString(length); | 946 return this.readString(length); |
946 }; | 947 }; |
947 | 948 |
948 | 949 |
949 /** | 950 /** |
950 * Reads a block of raw bytes from the binary stream. | 951 * Reads a block of raw bytes from the binary stream. |
951 * | 952 * |
952 * @param {number} length The number of bytes to read. | 953 * @param {number} length The number of bytes to read. |
953 * @return {Uint8Array} The decoded block of bytes, or null if the length was | 954 * @return {!Uint8Array} The decoded block of bytes, or an empty block if the |
954 * invalid. | 955 * length was invalid. |
955 */ | 956 */ |
956 jspb.BinaryDecoder.prototype.readBytes = function(length) { | 957 jspb.BinaryDecoder.prototype.readBytes = function(length) { |
957 if (length < 0 || | 958 if (length < 0 || |
958 this.cursor_ + length > this.bytes_.length) { | 959 this.cursor_ + length > this.bytes_.length) { |
959 this.error_ = true; | 960 this.error_ = true; |
960 return null; | 961 goog.asserts.fail('Invalid byte length!'); |
| 962 return new Uint8Array(0); |
961 } | 963 } |
962 | 964 |
963 var result = this.bytes_.subarray(this.cursor_, this.cursor_ + length); | 965 var result = this.bytes_.subarray(this.cursor_, this.cursor_ + length); |
964 | 966 |
965 this.cursor_ += length; | 967 this.cursor_ += length; |
966 goog.asserts.assert(this.cursor_ <= this.end_); | 968 goog.asserts.assert(this.cursor_ <= this.end_); |
967 return result; | 969 return result; |
968 }; | 970 }; |
969 | 971 |
970 | 972 |
(...skipping 25 matching lines...) Expand all Loading... |
996 var d = bytes[cursor + 3]; | 998 var d = bytes[cursor + 3]; |
997 var e = bytes[cursor + 4]; | 999 var e = bytes[cursor + 4]; |
998 var f = bytes[cursor + 5]; | 1000 var f = bytes[cursor + 5]; |
999 var g = bytes[cursor + 6]; | 1001 var g = bytes[cursor + 6]; |
1000 var h = bytes[cursor + 7]; | 1002 var h = bytes[cursor + 7]; |
1001 | 1003 |
1002 this.cursor_ += 8; | 1004 this.cursor_ += 8; |
1003 | 1005 |
1004 return String.fromCharCode(a, b, c, d, e, f, g, h); | 1006 return String.fromCharCode(a, b, c, d, e, f, g, h); |
1005 }; | 1007 }; |
OLD | NEW |