diff --git a/BinaryReader.js b/BinaryReader.js index 5ef2e34..7d53c63 100644 --- a/BinaryReader.js +++ b/BinaryReader.js @@ -1,4 +1,4 @@ -'use strict'; +'use strict'; /* * Simple BinaryReader is a minimal tool to read binary stream. * Useful for binary deserialization. @@ -37,42 +37,78 @@ BinaryReader.prototype.readInt8 = function () { return value; }; -BinaryReader.prototype.readUInt16 = function () { +BinaryReader.prototype.readUInt16LE = function () { var value = this._buffer.readUInt16LE(this._offset); this._offset += 2; return value; }; -BinaryReader.prototype.readInt16 = function () { +BinaryReader.prototype.readUInt16BE = function () { + var value = this._buffer.readUInt16BE(this._offset); + this._offset += 2; + return value; +}; + +BinaryReader.prototype.readInt16LE = function () { var value = this._buffer.readInt16LE(this._offset); this._offset += 2; return value; }; -BinaryReader.prototype.readUInt32 = function () { +BinaryReader.prototype.readInt16BE = function () { + var value = this._buffer.readInt16BE(this._offset); + this._offset += 2; + return value; +}; + +BinaryReader.prototype.readUInt32LE = function () { var value = this._buffer.readUInt32LE(this._offset); this._offset += 4; return value; }; -BinaryReader.prototype.readInt32 = function () { +BinaryReader.prototype.readUInt32BE = function () { + var value = this._buffer.readUInt32BE(this._offset); + this._offset += 4; + return value; +}; + +BinaryReader.prototype.readInt32LE = function () { var value = this._buffer.readInt32LE(this._offset); this._offset += 4; return value; }; -BinaryReader.prototype.readFloat = function () { +BinaryReader.prototype.readInt32BE = function () { + var value = this._buffer.readInt32LE(this._offset); + this._offset += 4; + return value; +}; + +BinaryReader.prototype.readFloatLE = function () { var value = this._buffer.readFloatLE(this._offset); this._offset += 4; return value; }; -BinaryReader.prototype.readDouble = function () { +BinaryReader.prototype.readFloatBE = function () { + var value = this._buffer.readFloatBE(this._offset); + this._offset += 4; + return value; +}; + +BinaryReader.prototype.readDoubleLE = function () { var value = this._buffer.readDoubleLE(this._offset); this._offset += 8; return value; }; +BinaryReader.prototype.readDoubleBE = function () { + var value = this._buffer.readDoubleBE(this._offset); + this._offset += 8; + return value; +}; + BinaryReader.prototype.readBytes = function (length) { return this._buffer.slice(this._offset, this._offset + length); this._offset += length; diff --git a/BinaryWriter.js b/BinaryWriter.js index 83926b1..60ba712 100644 --- a/BinaryWriter.js +++ b/BinaryWriter.js @@ -1,4 +1,4 @@ -'use strict'; +'use strict'; /* * Simple BinaryWriter is a minimal tool to write binary stream with unpredictable size. * Useful for binary serialization. @@ -40,19 +40,31 @@ BinaryWriter.prototype.writeInt8 = function (value) { this._buffer[this._length++] = value; }; -BinaryWriter.prototype.writeUInt16 = function (value) { +BinaryWriter.prototype.writeUInt16BE = function (value) { + checkAlloc(this, 2); + this._buffer[this._length++] = value >> 8; + this._buffer[this._length++] = value; +}; + +BinaryWriter.prototype.writeUInt16LE = function (value) { checkAlloc(this, 2); this._buffer[this._length++] = value; this._buffer[this._length++] = value >> 8; }; -BinaryWriter.prototype.writeInt16 = function (value) { +BinaryWriter.prototype.writeInt16BE = function (value) { + checkAlloc(this, 2); + this._buffer[this._length++] = value >> 8; + this._buffer[this._length++] = value; +}; + +BinaryWriter.prototype.writeInt16LE = function (value) { checkAlloc(this, 2); this._buffer[this._length++] = value; this._buffer[this._length++] = value >> 8; }; -BinaryWriter.prototype.writeUInt32 = function (value) { +BinaryWriter.prototype.writeUInt32BE = function (value) { checkAlloc(this, 4); this._buffer[this._length++] = value; this._buffer[this._length++] = value >> 8; @@ -60,7 +72,23 @@ BinaryWriter.prototype.writeUInt32 = function (value) { this._buffer[this._length++] = value >> 24; }; -BinaryWriter.prototype.writeInt32 = function (value) { +BinaryWriter.prototype.writeUInt32LE = function (value) { + checkAlloc(this, 4); + this._buffer[this._length++] = value >> 24; + this._buffer[this._length++] = value >> 16; + this._buffer[this._length++] = value >> 8; + this._buffer[this._length++] = value; +}; + +BinaryWriter.prototype.writeInt32BE = function (value) { + checkAlloc(this, 4); + this._buffer[this._length++] = value >> 24; + this._buffer[this._length++] = value >> 16; + this._buffer[this._length++] = value >> 8; + this._buffer[this._length++] = value; +}; + +BinaryWriter.prototype.writeInt32LE = function (value) { checkAlloc(this, 4); this._buffer[this._length++] = value; this._buffer[this._length++] = value >> 8; @@ -68,13 +96,25 @@ BinaryWriter.prototype.writeInt32 = function (value) { this._buffer[this._length++] = value >> 24; }; -BinaryWriter.prototype.writeFloat = function (value) { +BinaryWriter.prototype.writeFloatBE = function (value) { + checkAlloc(this, 4); + this._buffer.writeFloatBE(value, this._length, true); + this._length += 4; +}; + +BinaryWriter.prototype.writeFloatLE = function (value) { checkAlloc(this, 4); this._buffer.writeFloatLE(value, this._length, true); this._length += 4; }; -BinaryWriter.prototype.writeDouble = function (value) { +BinaryWriter.prototype.writeDoubleBE = function (value) { + checkAlloc(this, 8); + this._buffer.writeDoubleBE(value, this._length, true); + this._length += 8; +}; + +BinaryWriter.prototype.writeDoubleLE = function (value) { checkAlloc(this, 8); this._buffer.writeDoubleLE(value, this._length, true); this._length += 8; diff --git a/package.json b/package.json new file mode 100644 index 0000000..d8a7160 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "name": "binarywriter-node", + "description": ".NET like binarywriter in nodejs", + "version": "0.1.0" +}