writer's buffer resize no longer dies when item byteLength > 2x buffer length

This commit is contained in:
Brian Carlson 2011-01-14 14:52:16 -06:00
parent f5fce3542b
commit 3500154d2a
2 changed files with 10 additions and 10 deletions

View File

@ -10,16 +10,12 @@ p._remaining = function() {
return this.buffer.length - this.offset; return this.buffer.length - this.offset;
} }
p._resize = function() {
var oldBuffer = this.buffer;
this.buffer = Buffer(oldBuffer.length + this.size);
oldBuffer.copy(this.buffer);
}
//resizes internal buffer if not enough size left //resizes internal buffer if not enough size left
p._ensure = function(size) { p._ensure = function(size) {
if(this._remaining() < size) { if(this._remaining() < size) {
this._resize() var oldBuffer = this.buffer;
this.buffer = Buffer(oldBuffer.length + size);
oldBuffer.copy(this.buffer);
} }
} }
@ -46,7 +42,7 @@ p.addCString = function(string) {
this.buffer.write(string, this.offset); this.buffer.write(string, this.offset);
this.offset += len; this.offset += len;
this.buffer[this.offset] = 0; //add null terminator this.buffer[this.offset] = 0; //add null terminator
return this; return this;
} }
p.addChar = function(char) { p.addChar = function(char) {

View File

@ -149,5 +149,9 @@ test('clearing', function() {
}) })
test("resizing to much larger", function() {
var subject = new Writer(2);
var string = "!!!!!!!!";
var result = subject.addCString(string).flush();
assert.equalBuffers(result, [33, 33, 33, 33, 33, 33, 33, 33, 0])
})