read a buffer, like slicing while iterating pos
This commit is contained in:
parent
572582938f
commit
8b3ad7ac85
|
@ -22,6 +22,12 @@ BufferReader.prototype.eof = function eof() {
|
||||||
return this.pos >= this.buf.length;
|
return this.pos >= this.buf.length;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BufferReader.prototype.buffer = function(len) {
|
||||||
|
var buf = this.buf.slice(this.pos, this.pos + len);
|
||||||
|
this.pos = this.pos + len;
|
||||||
|
return buf;
|
||||||
|
};
|
||||||
|
|
||||||
BufferReader.prototype.read = function() {
|
BufferReader.prototype.read = function() {
|
||||||
var buf = this.buf.slice(this.pos);
|
var buf = this.buf.slice(this.pos);
|
||||||
this.pos = this.buf.length;
|
this.pos = this.buf.length;
|
||||||
|
|
|
@ -35,6 +35,20 @@ describe('BufferReader', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#buffer', function() {
|
||||||
|
|
||||||
|
it('should return a buffer of this length', function() {
|
||||||
|
var buf = new Buffer(10);
|
||||||
|
buf.fill(0);
|
||||||
|
var br = new BufferReader(buf);
|
||||||
|
var buf2 = br.buffer(2);
|
||||||
|
buf2.length.should.equal(2);
|
||||||
|
br.eof().should.equal(false);
|
||||||
|
br.pos.should.equal(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('read', function() {
|
describe('read', function() {
|
||||||
|
|
||||||
it('should return the same buffer', function() {
|
it('should return the same buffer', function() {
|
||||||
|
|
Loading…
Reference in New Issue