netbuffer
Version:
General purpose & network buffer extensions.
329 lines (298 loc) • 8.48 kB
JavaScript
/**
* NetBuffer
*
* Copyright (C) 2012 João Francisco Biondo Trinca
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package NetBuffer
* @author João Francisco Biondo Trinca <wolfulus@gmail.com>
* @copyright 2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://code.google.com/p/node-netbuffer/
*
*/
/**
* NetBuffer classes
*/
var Buffer = require('buffer').Buffer;
/**
* Class constructor
*/
function NetWriter() {
// Stores the buffer contents
this.content = [];
// Stores the current buffer size
this.size = 0;
}
/**
* Writes a char to the buffer
* @param value Char value
*/
NetWriter.prototype.writeInt8 = function (value) {
var buffer = new Buffer(1);
buffer.writeInt8(value, 0, true);
this.content.push(buffer);
this.size += 1;
};
/**
* Writes a byte to the buffer
* @param value Byte value
*/
NetWriter.prototype.writeUInt8 = function (value) {
var buffer = new Buffer(1);
buffer.writeUInt8(value, 0, true);
this.content.push(buffer);
this.size += 1;
};
/**
* Writes a short to the buffer
* @param value Short value
*/
NetWriter.prototype.writeInt16 = function (value) {
this.writeInt16BE(value);
};
/**
* Writes a short to the buffer
* @param value Short value
*/
NetWriter.prototype.writeInt16BE = function (value) {
var buffer = new Buffer(2);
buffer.writeInt16BE(value, 0, true);
this.content.push(buffer);
this.size += 2;
};
/**
* Writes a short to the buffer
* @param value Short value
*/
NetWriter.prototype.writeInt16LE = function (value) {
var buffer = new Buffer(2);
buffer.writeInt16LE(value, 0, true);
this.content.push(buffer);
this.size += 2;
};
/**
* Writes a short to the buffer
* @param value Short value
*/
NetWriter.prototype.writeUInt16 = function (value) {
this.writeUInt16BE(value);
};
/**
* Writes a short to the buffer
* @param value Short value
*/
NetWriter.prototype.writeUInt16BE = function (value) {
var buffer = new Buffer(2);
buffer.writeUInt16BE(value, 0, true);
this.content.push(buffer);
this.size += 2;
};
/**
* Writes a short to the buffer
* @param value Short value
*/
NetWriter.prototype.writeUInt16LE = function (value) {
var buffer = new Buffer(2);
buffer.writeUInt16LE(value, 0, true);
this.content.push(buffer);
this.size += 2;
};
/**
* Writes an integer to the buffer
* @param value Integer value
*/
NetWriter.prototype.writeInt32 = function (value) {
this.writeInt32BE(value);
};
/**
* Writes an integer to the buffer
* @param value Integer value
*/
NetWriter.prototype.writeInt32BE = function (value) {
var buffer = new Buffer(4);
buffer.writeInt32BE(value, 0, true);
this.content.push(buffer);
this.size += 4;
};
/**
* Writes an integer to the buffer
* @param value Integer value
*/
NetWriter.prototype.writeInt32LE = function (value) {
var buffer = new Buffer(4);
buffer.writeInt32LE(value, 0, true);
this.content.push(buffer);
this.size += 4;
};
/**
* Writes an integer to the buffer
* @param value Integer value
*/
NetWriter.prototype.writeUInt32 = function (value) {
this.writeUInt32BE(value);
};
/**
* Writes an integer to the buffer
* @param value Integer value
*/
NetWriter.prototype.writeUInt32BE = function (value) {
var buffer = new Buffer(4);
buffer.writeUInt32BE(value, 0, true);
this.content.push(buffer);
this.size += 4;
};
/**
* Writes an integer to the buffer
* @param value Integer value
*/
NetWriter.prototype.writeUInt32LE = function (value) {
var buffer = new Buffer(4);
buffer.writeUInt32LE(value, 0, true);
this.content.push(buffer);
this.size += 4;
};
/**
* Writes a float to the buffer
* @param value Float value
*/
NetWriter.prototype.writeFloat = function (value) {
this.writeFloatBE(value);
};
/**
* Writes a float to the buffer
* @param value Float value
*/
NetWriter.prototype.writeFloatBE = function (value) {
var buffer = new Buffer(4);
buffer.writeFloatBE(value, 0, true);
this.content.push(buffer);
this.size += 4;
};
/**
* Writes a float to the buffer
* @param value Float value
*/
NetWriter.prototype.writeFloatLE = function (value) {
var buffer = new Buffer(4);
buffer.writeFloatLE(value, 0, true);
this.content.push(buffer);
this.size += 4;
};
/**
* Writes a float to the buffer
* @param value Double value
*/
NetWriter.prototype.writeDouble = function (value) {
this.writeDoubleBE(value);
};
/**
* Writes a double to the buffer
* @param value Double value
*/
NetWriter.prototype.writeDoubleBE = function (value) {
var buffer = new Buffer(8);
buffer.writeDoubleBE(value, 0, true);
this.content.push(buffer);
this.size += 8;
};
/**
* Writes a double to the buffer
* @param value Double value
*/
NetWriter.prototype.writeDoubleLE = function (value) {
var buffer = new Buffer(8);
buffer.writeDoubleLE(value, 0, true);
this.content.push(buffer);
this.size += 8;
};
/**
* Writes a null-terminated string to the buffer
* @param value String value
*/
NetWriter.prototype.writeNtString = function (value) {
var buffer = new Buffer(value.length + 1);
for (var pos = 0, len = value.length; pos < len; pos++) {
buffer[pos] = value.charCodeAt(pos);
}
buffer[value.length] = 0;
this.content.push(buffer);
this.size += value.length + 1;
};
/**
* Writes a null-terminated string to the buffer
* @param value String value
*/
NetWriter.prototype.writeString = function (value) {
var buffer = new Buffer(value.length);
for (var pos = 0, len = value.length; pos < len; pos++) {
var val = value.charCodeAt(pos);
buffer[pos] = val;
if (!val) {
break;
}
}
this.content.push(buffer);
this.size += value.length + 1;
};
/**
* Writes a fixed-length string to the buffer (filled with zeros)
* @param value String value
* @param maxlength Maximum length of the string
*/
NetWriter.prototype.writeFixedString = function (value, maxlength) {
var len = value.length;
var buffer = new Buffer(maxlength);
for (var pos = 0; pos < maxlength; pos++) {
if (pos < len) {
buffer[pos] = value.charCodeAt(pos);
} else {
buffer[pos] = 0;
}
}
this.content.push(buffer);
this.size += maxlength;
};
/**
* Writes a buffer
* @param buffer Buffer contents
*/
NetWriter.prototype.writeBuffer = function (buffer) {
this.content.push(buffer);
this.size += buffer.length;
};
/**
* Converts the current buffer collection to the final Node.js's Buffer class.
*/
NetWriter.prototype.toBuffer = function () {
var offset = 0;
var count = this.content.length;
var buffer = new Buffer(this.size);
for (var i = 0; i < count; i++) {
this.content[i].copy(buffer, offset);
offset += this.content[i].length;
}
return buffer;
};
/**
* Class export
*/
exports.NetWriter = NetWriter;