buffer reader.
This commit is contained in:
parent
62a7dba556
commit
adb95e2a3a
@ -14,6 +14,10 @@ var assert = utils.assert;
|
|||||||
function BufferReader(data, zeroCopy) {
|
function BufferReader(data, zeroCopy) {
|
||||||
if (data instanceof BufferReader)
|
if (data instanceof BufferReader)
|
||||||
return data;
|
return data;
|
||||||
|
|
||||||
|
if (!(this instanceof BufferReader))
|
||||||
|
return new BufferReader(data, zeroCopy);
|
||||||
|
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.offset = 0;
|
this.offset = 0;
|
||||||
this.zeroCopy = zeroCopy;
|
this.zeroCopy = zeroCopy;
|
||||||
@ -30,11 +34,8 @@ BufferReader.prototype.end = function end() {
|
|||||||
var start = this.stack.pop();
|
var start = this.stack.pop();
|
||||||
var end = this.offset;
|
var end = this.offset;
|
||||||
|
|
||||||
if (this.stack.length === 0) {
|
if (this.stack.length === 0)
|
||||||
delete this.offset;
|
this.destroy();
|
||||||
delete this.stack;
|
|
||||||
delete this.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return end - start;
|
return end - start;
|
||||||
};
|
};
|
||||||
@ -47,11 +48,8 @@ BufferReader.prototype.endData = function endData() {
|
|||||||
var size = end - start;
|
var size = end - start;
|
||||||
var data = this.data;
|
var data = this.data;
|
||||||
|
|
||||||
if (this.stack.length === 0) {
|
if (this.stack.length === 0)
|
||||||
delete this.offset;
|
this.destroy();
|
||||||
delete this.stack;
|
|
||||||
delete this.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size === data.length)
|
if (size === data.length)
|
||||||
return data;
|
return data;
|
||||||
@ -59,6 +57,12 @@ BufferReader.prototype.endData = function endData() {
|
|||||||
return utils.slice(data, start, end);
|
return utils.slice(data, start, end);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BufferReader.prototype.destroy = function destroy() {
|
||||||
|
delete this.offset;
|
||||||
|
delete this.stack;
|
||||||
|
delete this.data;
|
||||||
|
};
|
||||||
|
|
||||||
BufferReader.prototype.readU8 = function readU8() {
|
BufferReader.prototype.readU8 = function readU8() {
|
||||||
assert(this.offset + 1 <= this.data.length);
|
assert(this.offset + 1 <= this.data.length);
|
||||||
var ret = utils.readU8(this.data, this.offset);
|
var ret = utils.readU8(this.data, this.offset);
|
||||||
@ -235,11 +239,6 @@ BufferReader.prototype.readNullString = function readNullString(enc) {
|
|||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
BufferReader.prototype.left = function left() {
|
|
||||||
assert(this.offset <= this.data.length);
|
|
||||||
return this.data.length - this.offset;
|
|
||||||
};
|
|
||||||
|
|
||||||
BufferReader.prototype.readVarint = function readVarint() {
|
BufferReader.prototype.readVarint = function readVarint() {
|
||||||
assert(this.offset + 1 <= this.data.length);
|
assert(this.offset + 1 <= this.data.length);
|
||||||
var result = utils.readVarint(this.data, this.offset);
|
var result = utils.readVarint(this.data, this.offset);
|
||||||
@ -249,6 +248,21 @@ BufferReader.prototype.readVarint = function readVarint() {
|
|||||||
return result.r;
|
return result.r;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BufferReader.prototype.left = function left() {
|
||||||
|
assert(this.offset <= this.data.length);
|
||||||
|
return this.data.length - this.offset;
|
||||||
|
};
|
||||||
|
|
||||||
|
BufferReader.prototype.getSize = function getSize() {
|
||||||
|
return this.data.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
BufferReader.prototype.seek = function seek(off) {
|
||||||
|
assert(this.offset + off <= this.data.length);
|
||||||
|
this.offset += off;
|
||||||
|
return off;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user