2010-11-01 07:21:37 +08:00
|
|
|
var Writer = function() {
|
|
|
|
this.buffers = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
var p = Writer.prototype;
|
|
|
|
|
|
|
|
p.add = function(buffer) {
|
|
|
|
this.buffers.push(buffer);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
p.addInt16 = function(val, front) {
|
|
|
|
return this.add(Buffer([
|
|
|
|
(val >>> 8),
|
|
|
|
(val >>> 0)
|
|
|
|
]));
|
|
|
|
};
|
|
|
|
|
|
|
|
p.getByteLength = function(initial) {
|
2010-12-30 09:04:33 +08:00
|
|
|
var totalBufferLength = 0;
|
|
|
|
var buffers = this.buffers;
|
|
|
|
for(var i = 0, len = buffers.length; i < len; i++) {
|
|
|
|
totalBufferLength += buffers[i].length;
|
|
|
|
}
|
|
|
|
return totalBufferLength;
|
2010-11-01 07:21:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
p.addInt32 = function(val, first) {
|
|
|
|
return this.add(Buffer([
|
|
|
|
(val >>> 24 & 0xFF),
|
|
|
|
(val >>> 16 & 0xFF),
|
|
|
|
(val >>> 8 & 0xFF),
|
|
|
|
(val >>> 0 & 0xFF)
|
|
|
|
]));
|
|
|
|
};
|
|
|
|
|
|
|
|
p.addCString = function(val) {
|
2010-11-02 10:11:40 +08:00
|
|
|
var len = Buffer.byteLength(val);
|
|
|
|
var buffer = new Buffer(len+1);
|
|
|
|
buffer.write(val);
|
|
|
|
buffer[len] = 0;
|
|
|
|
return this.add(buffer);
|
2010-11-01 07:21:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
p.addChar = function(char, first) {
|
|
|
|
return this.add(Buffer(char,'utf8'), first);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.join = function() {
|
2010-12-30 09:04:33 +08:00
|
|
|
var length = this.buffers.length;
|
|
|
|
if(length===1) {
|
|
|
|
return this.buffers[0]
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
var result = Buffer(this.getByteLength());
|
|
|
|
var index = 0;
|
|
|
|
var buffers = this.buffers;
|
|
|
|
for(var i = 0; i < length; i ++) {
|
|
|
|
var buffer = buffers[i];
|
|
|
|
buffer.copy(result, index, 0);
|
|
|
|
index += buffer.length;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Writer;
|
|
|
|
|