moved bufferlist to helper file

This commit is contained in:
brianc 2010-09-29 02:28:23 -05:00
parent eb698f8619
commit 871f529fea
2 changed files with 50 additions and 50 deletions

View File

@ -34,3 +34,53 @@ stringToHex = function(string) {
hexToString = function(hexArray) {
return new Buffer(hexArray).toString('utf8');
}
var 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;
};

View File

@ -1,55 +1,5 @@
require(__dirname + "/test-helper");
var 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;
};
assert.equalBuffers = function(actual, expected) {
if(actual.length != expected.length) {