read a buffer, like slicing while iterating pos

This commit is contained in:
Ryan X. Charles 2014-09-01 16:40:31 -07:00
parent 572582938f
commit 8b3ad7ac85
3 changed files with 20 additions and 0 deletions

View File

@ -22,6 +22,12 @@ BufferReader.prototype.eof = function eof() {
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() {
var buf = this.buf.slice(this.pos);
this.pos = this.buf.length;

View File

@ -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() {
it('should return the same buffer', function() {