From 3500154d2acd1c3f92d78dd27bedfa2fe0212827 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Fri, 14 Jan 2011 14:52:16 -0600 Subject: [PATCH] writer's buffer resize no longer dies when item byteLength > 2x buffer length --- lib/writer.js | 12 ++++-------- test/unit/writer-tests.js | 8 ++++++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/writer.js b/lib/writer.js index 03e3552..520b6b1 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -10,16 +10,12 @@ p._remaining = function() { 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 p._ensure = function(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.offset += len; this.buffer[this.offset] = 0; //add null terminator - return this; + return this; } p.addChar = function(char) { diff --git a/test/unit/writer-tests.js b/test/unit/writer-tests.js index 32972de..8a385ff 100644 --- a/test/unit/writer-tests.js +++ b/test/unit/writer-tests.js @@ -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]) +})