Skip to content

Commit

Permalink
Added writeBufferNT functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshGlazebrook committed May 9, 2016
1 parent 0df4869 commit 00d9baa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ returns this
returns this

### SmartBuffer.writeBufferNT( value, [offset] )
> `Buffer` **Buffer value to write**
> `Number` **The position to insert this Buffer's content at**
returns this


## Utility Functions

Expand Down
15 changes: 15 additions & 0 deletions lib/smart-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ var SmartBuffer = (function () {
return this;
};

/**
* Writes a null terminated Buffer to the underlying buffer.
* @param value {Buffer} The buffer to write.
* @param offset {Number} The offset to write the Buffer to.
* @returns {*}
*/
SmartBuffer.prototype.writeBufferNT = function (value, offset) {
var len = value.length;
this._ensureWritable(len, offset);
value.copy(this.buff, offset || this._writeOffset);
this.buff[this._writeOffset + len] = 0x00;
this._writeOffset += len + 1;
return this;
};


/**
* Resets the Endless Buffer.
Expand Down
12 changes: 12 additions & 0 deletions test/smart-buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ describe('Reading/Writing To/From SmartBuffer', function () {
it('Should return a length of 3 for the three bytes after the first null in the buffer after reading to end.', function () {
assert.equal(read2.length, 3);
});
});

describe('Null Terminated Buffer Writing', function () {
var buff = new SmartBuffer();
buff.writeBufferNT(new Buffer([0x01, 0x02, 0x03, 0x04]));

var read1 = buff.readBufferNT();

it('Should read the correct null terminated buffer data.', function () {
assert.equal(read1.length, 4);
});

})

});
Expand Down

0 comments on commit 00d9baa

Please sign in to comment.