|
110 | 110 | * @const |
111 | 111 | * @expose |
112 | 112 | */ |
113 | | - ByteBuffer.VERSION = "2.1.0"; |
| 113 | + ByteBuffer.VERSION = "2.1.1"; |
114 | 114 |
|
115 | 115 | /** |
116 | 116 | * Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required. |
|
382 | 382 | b.array = this.array; |
383 | 383 | b.view = this.view; |
384 | 384 | b.offset = this.offset; |
| 385 | + b.markedOffset = this.markedOffset; |
385 | 386 | b.length = this.length; |
386 | 387 | return b; |
387 | 388 | }; |
|
400 | 401 | var dst = new Uint8Array(b.array); |
401 | 402 | dst.set(src); |
402 | 403 | b.offset = this.offset; |
| 404 | + b.markedOffset = this.markedOffset; |
403 | 405 | b.length = this.length; |
404 | 406 | return b; |
405 | 407 | }; |
|
452 | 454 | var dstView = new Uint8Array(dst); |
453 | 455 | dstView.set(srcView.subarray(this.offset, this.length)); |
454 | 456 | this.array = dst; |
| 457 | + if (this.markedOffset >= this.offset) { |
| 458 | + this.markedOffset -= this.offset; |
| 459 | + } else { |
| 460 | + this.markedOffset = -1; |
| 461 | + } |
455 | 462 | this.offset = 0; |
456 | 463 | this.length = this.array.byteLength; |
457 | 464 | return this; |
|
466 | 473 | * @expose |
467 | 474 | */ |
468 | 475 | ByteBuffer.prototype.destroy = function() { |
469 | | - if (this.array == null) return this; // Already destroyed |
470 | | - this.array = null; |
471 | | - this.view = null; |
472 | | - this.offset = 0; |
473 | | - this.length = 0; |
| 476 | + if (this.array !== null) { |
| 477 | + this.array = null; |
| 478 | + this.view = null; |
| 479 | + this.offset = 0; |
| 480 | + this.markedOffset = -1; |
| 481 | + this.length = 0; |
| 482 | + } |
474 | 483 | return this; |
475 | 484 | }; |
476 | 485 |
|
477 | 486 | /** |
478 | 487 | * Reverses the backing array and adapts offset and length to retain the same relative position on the reversed |
479 | | - * data in inverse order. Example: "00<01 02>03 04".reverse() = "04 03<02 01>00". |
| 488 | + * data in inverse order. Example: "00<01 02>03 04".reverse() = "04 03<02 01>00". Also clears the marked |
| 489 | + * offset. |
480 | 490 | * @returns {!ByteBuffer} this |
481 | 491 | * @throws {Error} If the buffer is already destroyed |
482 | 492 | * @expose |
483 | 493 | */ |
484 | 494 | ByteBuffer.prototype.reverse = function() { |
485 | | - if (this.array == null) { |
| 495 | + if (this.array === null) { |
486 | 496 | throw(new Error(this+" cannot be reversed: Already destroyed")); |
487 | 497 | } |
488 | 498 | Array.prototype.reverse.call(new Uint8Array(this.array)); |
489 | 499 | var o = this.offset; |
490 | 500 | this.offset = this.array.byteLength - this.length; |
| 501 | + this.markedOffset = -1; |
491 | 502 | this.length = this.array.byteLength - o; |
492 | 503 | this.view = new DataView(this.array); |
493 | 504 | return this; |
|
507 | 518 | if (!(src instanceof ByteBuffer)) { |
508 | 519 | src = ByteBuffer.wrap(src); |
509 | 520 | } |
510 | | - if (src.array == null) { |
| 521 | + if (src.array === null) { |
511 | 522 | throw(new Error(src+" cannot be appended to "+this+": Already destroyed")); |
512 | 523 | } |
513 | 524 | var n = src.length - src.offset; |
|
538 | 549 | if (!(src instanceof ByteBuffer)) { |
539 | 550 | src = ByteBuffer.wrap(src); |
540 | 551 | } |
541 | | - if (src.array == null) { |
| 552 | + if (src.array === null) { |
542 | 553 | throw(src+" cannot be prepended to "+this+": Already destroyed"); |
543 | 554 | } |
544 | 555 | var n = src.length - src.offset; |
|
824 | 835 | */ |
825 | 836 | ByteBuffer.prototype.readFloat32 = function(offset) { |
826 | 837 | offset = typeof offset !== 'undefined' ? offset : (this.offset+=4)-4; |
827 | | - if (this.array == null || offset+4 > this.array.byteLength) { |
| 838 | + if (this.array === null || offset+4 > this.array.byteLength) { |
828 | 839 | throw(new Error("Cannot read float32 from "+this+" at "+offset+": Capacity overflow")); |
829 | 840 | } |
830 | 841 | return this.view.getFloat32(offset, this.littleEndian); |
|
873 | 884 | */ |
874 | 885 | ByteBuffer.prototype.readFloat64 = function(offset) { |
875 | 886 | offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8; |
876 | | - if (this.array == null || offset+8 > this.array.byteLength) { |
| 887 | + if (this.array === null || offset+8 > this.array.byteLength) { |
877 | 888 | throw(new Error("Cannot read float64 from "+this+" at "+offset+": Capacity overflow")); |
878 | 889 | } |
879 | 890 | return this.view.getFloat64(offset, this.littleEndian); |
|
933 | 944 | */ |
934 | 945 | ByteBuffer.prototype.readInt64 = function(offset) { |
935 | 946 | offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8; |
936 | | - if (this.array == null || offset+8 > this.array.byteLength) { |
| 947 | + if (this.array === null || offset+8 > this.array.byteLength) { |
937 | 948 | this.offset -= 8; |
938 | 949 | throw(new Error("Cannot read int64 from "+this+" at "+offset+": Capacity overflow")); |
939 | 950 | } |
|
977 | 988 | */ |
978 | 989 | ByteBuffer.prototype.readUint64 = function(offset) { |
979 | 990 | offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8; |
980 | | - if (this.array == null || offset+8 > this.array.byteLength) { |
| 991 | + if (this.array === null || offset+8 > this.array.byteLength) { |
981 | 992 | this.offset -= 8; |
982 | 993 | throw(new Error("Cannot read int64 from "+this+" at "+offset+": Capacity overflow")); |
983 | 994 | } |
|
1968 | 1979 | * @expose |
1969 | 1980 | */ |
1970 | 1981 | ByteBuffer.prototype.toColumns = function(wrap) { |
1971 | | - if (this.array == null) return "DESTROYED"; |
| 1982 | + if (this.array === null) return "DESTROYED"; |
1972 | 1983 | wrap = typeof wrap !== 'undefined' ? parseInt(wrap, 10) : 16; |
1973 | 1984 | if (wrap < 1) wrap = 16; |
1974 | 1985 |
|
|
2056 | 2067 | view = this.view, |
2057 | 2068 | i, k; |
2058 | 2069 | if (!debug) { |
2059 | | - if (this.array == null) return ""; |
| 2070 | + if (this.array === null) return ""; |
2060 | 2071 | for (i=this.offset, k=this.length; i<k; i++) { |
2061 | 2072 | val = view.getUint8(i).toString(16).toUpperCase(); |
2062 | 2073 | if (val.length < 2) val = "0"+val; |
2063 | 2074 | out += val; |
2064 | 2075 | } |
2065 | 2076 | return out; |
2066 | 2077 | } else { |
2067 | | - if (this.array == null) return "DESTROYED"; |
| 2078 | + if (this.array === null) return "DESTROYED"; |
2068 | 2079 | if (this.offset == 0 && this.length == 0) { |
2069 | 2080 | out += "|"; |
2070 | 2081 | } else if (this.length == 0) { |
|
2098 | 2109 | * @expose |
2099 | 2110 | */ |
2100 | 2111 | ByteBuffer.prototype.toBase64 = function() { |
2101 | | - if (this.array == null || this.offset >= this.length) return ""; |
| 2112 | + if (this.array === null || this.offset >= this.length) return ""; |
2102 | 2113 | return ByteBuffer.encode64(this); |
2103 | 2114 | }; |
2104 | 2115 |
|
|
2108 | 2119 | * @expose |
2109 | 2120 | */ |
2110 | 2121 | ByteBuffer.prototype.toUTF8 = function() { |
2111 | | - if (this.array == null || this.offset >= this.length) return ""; |
| 2122 | + if (this.array === null || this.offset >= this.length) return ""; |
2112 | 2123 | return this.readUTF8StringBytes(this.length - this.offset, this.offset)["string"]; |
2113 | 2124 | }; |
2114 | 2125 |
|
|
0 commit comments