node-postgres/test/unit/buffer-list.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2010-10-09 12:00:37 +08:00
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'));
};
2010-10-11 06:40:11 +08:00
BufferList.prototype.addChar = function(char, first) {
return this.add(Buffer(char,'utf8'), first);
2010-10-11 06:40:11 +08:00
};
2010-10-09 12:00:37 +08:00
BufferList.prototype.join = function(appendLength, char) {
var length = this.getByteLength();
if(appendLength) {
this.addInt32(length+4, true);
return this.join(false, char);
}
if(char) {
2010-10-11 06:40:11 +08:00
this.addChar(char, true);
2010-10-09 12:00:37 +08:00
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();
};
module.exports = BufferList;