From 5d006bc593133d1338ba3902fa7b14ca98f3baa7 Mon Sep 17 00:00:00 2001 From: brianc Date: Fri, 8 Oct 2010 23:00:37 -0500 Subject: [PATCH] moved buffer list to it's own file --- test/buffer-list.js | 58 ++++++++++++++++++++++++++++++++++++++++++++ test/test-helper.js | 59 --------------------------------------------- 2 files changed, 58 insertions(+), 59 deletions(-) create mode 100644 test/buffer-list.js diff --git a/test/buffer-list.js b/test/buffer-list.js new file mode 100644 index 00000000..78606cff --- /dev/null +++ b/test/buffer-list.js @@ -0,0 +1,58 @@ +BufferList = function() { + this.buffers = []; +}; + +BufferList.prototype.add = function(buffer, front) { + this.buffers[front ? "unshift" : "push"](buffer); + return this; +}; + +BufferList.prototype.addInt16 = function(val, front) { + return this.add(Buffer([(val >>> 8),(val >>> 0)]),front); +}; + +BufferList.prototype.getByteLength = function(initial) { + return this.buffers.reduce(function(previous, current){ + return previous + current.length; + },initial || 0); +}; + +BufferList.prototype.addInt32 = function(val, first) { + return this.add(Buffer([ + (val >>> 24), + (val >>> 16), + (val >>> 8), + (val >>> 0) + ]),first); +}; + +BufferList.prototype.addCString = function(val) { + return this.add(Buffer(val + '\0','utf8')); +}; + +BufferList.prototype.join = function(appendLength, char) { + var length = this.getByteLength(); + if(appendLength) { + this.addInt32(length+4, true); + return this.join(false, char); + } + if(char) { + this.buffers.unshift(Buffer(char,'utf8')); + length++; + } + var result = Buffer(length); + var index = 0; + this.buffers.forEach(function(buffer) { + buffer.copy(result, index, 0); + index += buffer.length; + }); + return result; +}; + +BufferList.concat = function() { + var total = new BufferList(); + for(var i = 0; i < arguments.length; i++) { + total.add(arguments[i]); + } + return total.join(); +}; diff --git a/test/test-helper.js b/test/test-helper.js index 2e9c13db..76ce641a 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -43,65 +43,6 @@ hexToString = function(hexArray) { return new Buffer(hexArray).toString('utf8'); } -BufferList = function() { - this.buffers = []; -}; - -BufferList.prototype.add = function(buffer, front) { - this.buffers[front ? "unshift" : "push"](buffer); - return this; -}; - -BufferList.prototype.addInt16 = function(val, front) { - return this.add(Buffer([(val >>> 8),(val >>> 0)]),front); -}; - -BufferList.prototype.getByteLength = function(initial) { - return this.buffers.reduce(function(previous, current){ - return previous + current.length; - },initial || 0); -}; - -BufferList.prototype.addInt32 = function(val, first) { - return this.add(Buffer([ - (val >>> 24), - (val >>> 16), - (val >>> 8), - (val >>> 0) - ]),first); -}; - -BufferList.prototype.addCString = function(val) { - return this.add(Buffer(val + '\0','utf8')); -}; - -BufferList.prototype.join = function(appendLength, char) { - var length = this.getByteLength(); - if(appendLength) { - this.addInt32(length+4, true); - return this.join(false, char); - } - if(char) { - this.buffers.unshift(Buffer(char,'utf8')); - length++; - } - var result = Buffer(length); - var index = 0; - this.buffers.forEach(function(buffer) { - buffer.copy(result, index, 0); - index += buffer.length; - }); - return result; -}; - -BufferList.concat = function() { - var total = new BufferList(); - for(var i = 0; i < arguments.length; i++) { - total.add(arguments[i]); - } - return total.join(); -}; - MemoryStream = function() { EventEmitter.call(this); this.packets = [];