Permalink
Browse files

Merge pull request #6 from bkw/addSkipMethodRebased

Add skip method rebased
  • Loading branch information...
2 parents eda77c7 + efab368 commit ad9474a58751c06fab36057739397c80bb350ef1 @felixge committed Oct 13, 2012
Showing with 20 additions and 0 deletions.
  1. +5 −0 README.md
  2. +7 −0 lib/Reader.js
  3. +8 −0 test/unit/test-Reader.js
View
@@ -96,6 +96,11 @@ omitted, a null terminated string is assumed.
Returns the next `bytes` as a buffer.
+### reader.skip(bytes)
+
+Skips `bytes` bytes of the buffer.
+
+
## Writer API
The Writer has not been implemented yet.
View
@@ -172,3 +172,10 @@ Reader.prototype.buffer = function(bytes) {
this._buffer.copy(ret, 0, this._offset - bytes, this._offset);
return ret;
};
+
+Reader.prototype.skip = function(bytes) {
+ if (bytes > this.bytesAhead() || bytes < 0) {
+ this.emit('error', new Error('tried to skip outsite of the buffer'));
+ }
+ this._offset += bytes;
+};
View
@@ -247,6 +247,14 @@ test('Read Methods', {
var origResult = reader.buffer(1);
reader.write(new Buffer([2]));
assert.deepEqual(origResult, new Buffer([1]));
+ },
+
+ 'skip' : function() {
+ var reader = new Reader(new Buffer([1, 2, 3, 4, 5]));
+ reader.skip(2);
+ assert.deepEqual(reader.buffer(3), new Buffer([3, 4, 5]));
+ assert.throws(function() { reader.skip(1); });
+ assert.throws(function() { reader.skip(-1); });
}
});

0 comments on commit ad9474a

Please sign in to comment.