allow bufferreader.read to specify length

...to be more symmetric with the write function of bufferwriter
This commit is contained in:
Ryan X. Charles 2014-09-17 15:41:30 -07:00
parent 7918f53f12
commit 6e1dfd3003
2 changed files with 13 additions and 1 deletions

View File

@ -28,7 +28,9 @@ BufferReader.prototype.buffer = function(len) {
return buf;
};
BufferReader.prototype.read = function() {
BufferReader.prototype.read = function(len) {
if (len)
return this.buffer(len);
var buf = this.buf.slice(this.pos);
this.pos = this.buf.length;
return buf;

View File

@ -58,6 +58,16 @@ describe('BufferReader', function() {
br.read().toString('hex').should.equal(buf.toString('hex'));
});
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.read(2);
buf2.length.should.equal(2);
br.eof().should.equal(false);
br.pos.should.equal(2);
});
});
describe('#readUInt8', function() {