Browse Source

read a buffer, like slicing while iterating pos

patch-2
Ryan X. Charles 10 years ago
parent
commit
8b3ad7ac85
  1. 6
      lib/bufferreader.js
  2. 14
      test/bufferreader.js
  3. 0
      test/script.js

6
lib/bufferreader.js

@ -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;

14
test/bufferreader.js

@ -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() {

0
test/test.script.js → test/script.js

Loading…
Cancel
Save