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