2011-01-02 01:27:52 +08:00
|
|
|
var ElasticBuffer = function(size) {
|
|
|
|
this.size = size || 1024;
|
|
|
|
this.buffer = new Buffer(this.size);
|
|
|
|
this.offset = 0;
|
2010-11-01 07:21:37 +08:00
|
|
|
};
|
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
var p = ElasticBuffer.prototype;
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p._remaining = function() {
|
|
|
|
return this.buffer.length - this.offset;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p._resize = function() {
|
|
|
|
var oldBuffer = this.buffer;
|
|
|
|
this.buffer = Buffer(oldBuffer.length + this.size);
|
|
|
|
oldBuffer.copy(this.buffer);
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
//resizes internal buffer if not enough size left
|
|
|
|
p._ensure = function(size) {
|
|
|
|
if(this._remaining() < size) {
|
|
|
|
this._resize()
|
2010-12-30 09:04:33 +08:00
|
|
|
}
|
2011-01-02 01:27:52 +08:00
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addInt32 = function(num) {
|
|
|
|
this._ensure(4)
|
|
|
|
this.buffer[this.offset++] = (num >>> 24 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 16 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addInt16 = function(num) {
|
|
|
|
this._ensure(2)
|
|
|
|
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addCString = function(string) {
|
|
|
|
var string = string || "";
|
|
|
|
var len = Buffer.byteLength(string) + 1;
|
|
|
|
this._ensure(len);
|
|
|
|
this.buffer.write(string, this.offset);
|
|
|
|
this.offset += len;
|
|
|
|
this.buffer[this.offset] = 0; //add null terminator
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.addChar = function(char) {
|
|
|
|
this._ensure(1);
|
|
|
|
this.buffer.write(char, this.offset);
|
|
|
|
this.offset++;
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
|
|
|
p.join = function() {
|
2011-01-02 01:27:52 +08:00
|
|
|
return this.buffer.slice(0, this.offset);
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.getByteLength = function() {
|
|
|
|
return this.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.add = function(otherBuffer) {
|
|
|
|
this._ensure(otherBuffer.length);
|
|
|
|
otherBuffer.copy(this.buffer, this.offset);
|
|
|
|
this.offset += otherBuffer.length;
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
module.exports = ElasticBuffer;
|