}
} else if (subencoding & 0x01) { // Raw
let pixels = tw * th;
- let data = sock.rQshiftBytes(pixels * 4);
+ let data = sock.rQshiftBytes(pixels * 4, false);
// Max sure the image is fully opaque
for (let i = 0;i < pixels;i++) {
data[i * 4 + 3] = 255;
if (sock.rQwait("JPEG", length-2+extra, 4)) {
return null;
}
- let data = sock.rQpeekBytes(length-2+extra);
+ let data = sock.rQpeekBytes(length-2+extra, false);
if (data.at(-2) === 0xFF && data.at(-1) !== 0x00 &&
!(data.at(-1) >= 0xD0 && data.at(-1) <= 0xD7)) {
extra -= 2;
segment[1] = type;
segment[2] = length >> 8;
segment[3] = length;
- segment.set(sock.rQshiftBytes(length-2+extra), 4);
+ segment.set(sock.rQshiftBytes(length-2+extra, false), 4);
return segment;
}
const curY = y + (height - this._lines);
- let data = sock.rQshiftBytes(bytesPerLine);
+ let data = sock.rQshiftBytes(bytesPerLine, false);
// Convert data if needed
if (depth == 8) {
return null;
}
- let data = sock.rQshiftBytes(this._len);
+ let data = sock.rQshiftBytes(this._len, false);
this._len = 0;
return data;
return false;
}
- const data = sock.rQshiftBytes(this._length);
+ const data = sock.rQshiftBytes(this._length, false);
this._inflator.setInput(data);
let str = "";
// Handle large arrays in steps to avoid long strings on the stack
for (let i = 0; i < len; i += 4096) {
- let part = this.rQshiftBytes(Math.min(4096, len - i));
+ let part = this.rQshiftBytes(Math.min(4096, len - i), false);
str += String.fromCharCode.apply(null, part);
}
return str;
}
- rQshiftBytes(len) {
+ rQshiftBytes(len, copy=true) {
this._rQi += len;
- return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
+ if (copy) {
+ return this._rQ.slice(this._rQi - len, this._rQi);
+ } else {
+ return this._rQ.subarray(this._rQi - len, this._rQi);
+ }
}
rQshiftTo(target, len) {
this._rQi += len;
}
- rQpeekBytes(len) {
- return new Uint8Array(this._rQ.buffer, this._rQi, len);
+ rQpeekBytes(len, copy=true) {
+ if (copy) {
+ return this._rQ.slice(this._rQi, this._rQi + len);
+ } else {
+ return this._rQ.subarray(this._rQi, this._rQi + len);
+ }
}
// Check to see if we must wait for 'num' bytes (default to FBU.bytes)
expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
expect(sock._rQlen - sock._rQi).to.equal(befLen - 3);
});
+ it('should return a shared array if requested', function () {
+ const befRQi = sock._rQi;
+ const shifted = sock.rQshiftBytes(3, false);
+ expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
+ expect(shifted.buffer.byteLength).to.not.equal(shifted.length);
+ });
});
describe('rQpeekBytes', function () {
sock._rQi = 1;
expect(sock.rQpeekBytes(2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
});
+
+ it('should return a shared array if requested', function () {
+ const sl = sock.rQpeekBytes(2, false);
+ expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
+ expect(sl.buffer.byteLength).to.not.equal(sl.length);
+ });
});
describe('rQwait', function () {