Skip to content

Commit f676ba4

Browse files
committed
Adapt marked offsets
1 parent 1677a7b commit f676ba4

File tree

9 files changed

+147
-103
lines changed

9 files changed

+147
-103
lines changed

ByteBuffer.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
* @const
111111
* @expose
112112
*/
113-
ByteBuffer.VERSION = "2.1.0";
113+
ByteBuffer.VERSION = "2.1.1";
114114

115115
/**
116116
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -382,6 +382,7 @@
382382
b.array = this.array;
383383
b.view = this.view;
384384
b.offset = this.offset;
385+
b.markedOffset = this.markedOffset;
385386
b.length = this.length;
386387
return b;
387388
};
@@ -400,6 +401,7 @@
400401
var dst = new Uint8Array(b.array);
401402
dst.set(src);
402403
b.offset = this.offset;
404+
b.markedOffset = this.markedOffset;
403405
b.length = this.length;
404406
return b;
405407
};
@@ -452,6 +454,11 @@
452454
var dstView = new Uint8Array(dst);
453455
dstView.set(srcView.subarray(this.offset, this.length));
454456
this.array = dst;
457+
if (this.markedOffset >= this.offset) {
458+
this.markedOffset -= this.offset;
459+
} else {
460+
this.markedOffset = -1;
461+
}
455462
this.offset = 0;
456463
this.length = this.array.byteLength;
457464
return this;
@@ -466,28 +473,32 @@
466473
* @expose
467474
*/
468475
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+
}
474483
return this;
475484
};
476485

477486
/**
478487
* 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.
480490
* @returns {!ByteBuffer} this
481491
* @throws {Error} If the buffer is already destroyed
482492
* @expose
483493
*/
484494
ByteBuffer.prototype.reverse = function() {
485-
if (this.array == null) {
495+
if (this.array === null) {
486496
throw(new Error(this+" cannot be reversed: Already destroyed"));
487497
}
488498
Array.prototype.reverse.call(new Uint8Array(this.array));
489499
var o = this.offset;
490500
this.offset = this.array.byteLength - this.length;
501+
this.markedOffset = -1;
491502
this.length = this.array.byteLength - o;
492503
this.view = new DataView(this.array);
493504
return this;
@@ -507,7 +518,7 @@
507518
if (!(src instanceof ByteBuffer)) {
508519
src = ByteBuffer.wrap(src);
509520
}
510-
if (src.array == null) {
521+
if (src.array === null) {
511522
throw(new Error(src+" cannot be appended to "+this+": Already destroyed"));
512523
}
513524
var n = src.length - src.offset;
@@ -538,7 +549,7 @@
538549
if (!(src instanceof ByteBuffer)) {
539550
src = ByteBuffer.wrap(src);
540551
}
541-
if (src.array == null) {
552+
if (src.array === null) {
542553
throw(src+" cannot be prepended to "+this+": Already destroyed");
543554
}
544555
var n = src.length - src.offset;
@@ -824,7 +835,7 @@
824835
*/
825836
ByteBuffer.prototype.readFloat32 = function(offset) {
826837
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) {
828839
throw(new Error("Cannot read float32 from "+this+" at "+offset+": Capacity overflow"));
829840
}
830841
return this.view.getFloat32(offset, this.littleEndian);
@@ -873,7 +884,7 @@
873884
*/
874885
ByteBuffer.prototype.readFloat64 = function(offset) {
875886
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) {
877888
throw(new Error("Cannot read float64 from "+this+" at "+offset+": Capacity overflow"));
878889
}
879890
return this.view.getFloat64(offset, this.littleEndian);
@@ -933,7 +944,7 @@
933944
*/
934945
ByteBuffer.prototype.readInt64 = function(offset) {
935946
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) {
937948
this.offset -= 8;
938949
throw(new Error("Cannot read int64 from "+this+" at "+offset+": Capacity overflow"));
939950
}
@@ -977,7 +988,7 @@
977988
*/
978989
ByteBuffer.prototype.readUint64 = function(offset) {
979990
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) {
981992
this.offset -= 8;
982993
throw(new Error("Cannot read int64 from "+this+" at "+offset+": Capacity overflow"));
983994
}
@@ -1968,7 +1979,7 @@
19681979
* @expose
19691980
*/
19701981
ByteBuffer.prototype.toColumns = function(wrap) {
1971-
if (this.array == null) return "DESTROYED";
1982+
if (this.array === null) return "DESTROYED";
19721983
wrap = typeof wrap !== 'undefined' ? parseInt(wrap, 10) : 16;
19731984
if (wrap < 1) wrap = 16;
19741985

@@ -2056,15 +2067,15 @@
20562067
view = this.view,
20572068
i, k;
20582069
if (!debug) {
2059-
if (this.array == null) return "";
2070+
if (this.array === null) return "";
20602071
for (i=this.offset, k=this.length; i<k; i++) {
20612072
val = view.getUint8(i).toString(16).toUpperCase();
20622073
if (val.length < 2) val = "0"+val;
20632074
out += val;
20642075
}
20652076
return out;
20662077
} else {
2067-
if (this.array == null) return "DESTROYED";
2078+
if (this.array === null) return "DESTROYED";
20682079
if (this.offset == 0 && this.length == 0) {
20692080
out += "|";
20702081
} else if (this.length == 0) {
@@ -2098,7 +2109,7 @@
20982109
* @expose
20992110
*/
21002111
ByteBuffer.prototype.toBase64 = function() {
2101-
if (this.array == null || this.offset >= this.length) return "";
2112+
if (this.array === null || this.offset >= this.length) return "";
21022113
return ByteBuffer.encode64(this);
21032114
};
21042115

@@ -2108,7 +2119,7 @@
21082119
* @expose
21092120
*/
21102121
ByteBuffer.prototype.toUTF8 = function() {
2111-
if (this.array == null || this.offset >= this.length) return "";
2122+
if (this.array === null || this.offset >= this.length) return "";
21122123
return this.readUTF8StringBytes(this.length - this.offset, this.offset)["string"];
21132124
};
21142125

0 commit comments

Comments
 (0)