Created
September 20, 2015 11:33
-
-
Save gtk2k/13192d1a4d16441ec440 to your computer and use it in GitHub Desktop.
Node.js sequentialReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function sequentialReader(buf) { | |
this.buffer = buf; | |
this.offset = 0; | |
} | |
sequentialReader.prototype.readUInt8 = function () { | |
var val = this.buffer.readUInt8(this.offset); | |
this.offset++; | |
return val; | |
}; | |
sequentialReader.prototype.readInt8 = function () { | |
var val = this.buffer.readInt8(this.offset); | |
this.offset++; | |
return val; | |
}; | |
sequentialReader.prototype.readUInt16LE = function () { | |
var val = this.buffer.readUInt16LE(this.offset); | |
this.offset += 2; | |
return val; | |
}; | |
sequentialReader.prototype.readUInt16BE = function () { | |
var val = this.buffer.readUInt16BE(this.offset); | |
this.offset += 2; | |
return val; | |
}; | |
sequentialReader.prototype.readInt16LE = function () { | |
var val = this.buffer.readInt16LE(this.offset); | |
this.offset += 2; | |
return val; | |
}; | |
sequentialReader.prototype.readInt16BE = function () { | |
var val = this.buffer.readInt16BE(this.offset); | |
this.offset += 2; | |
return val; | |
}; | |
sequentialReader.prototype.readUInt32LE = function () { | |
var val = this.buffer.readUInt32LE(this.offset); | |
this.offset += 4; | |
return val; | |
}; | |
sequentialReader.prototype.readUInt32BE = function () { | |
var val = this.buffer.readUInt32BE(this.offset); | |
this.offset += 4; | |
return val; | |
}; | |
sequentialReader.prototype.readInt32LE = function () { | |
var val = this.buffer.readInt32LE(this.offset); | |
this.offset += 4; | |
return val; | |
}; | |
sequentialReader.prototype.readInt32BE = function () { | |
var val = this.buffer.readInt32BE(this.offset); | |
this.offset += 4; | |
return val; | |
}; | |
sequentialReader.prototype.readFloatLE = function () { | |
var val = this.buffer.readFloatLE(this.offset); | |
this.offset += 4; | |
return val; | |
}; | |
sequentialReader.prototype.readFloatBE = function () { | |
var val = this.buffer.readFloatBE(this.offset); | |
this.offset += 4; | |
return val; | |
}; | |
sequentialReader.prototype.readDoubleLE = function () { | |
var val = this.buffer.readDoubleLE(this.offset); | |
this.offset += 8; | |
return val; | |
}; | |
sequentialReader.prototype.readBytes = function (size) { | |
var copyBuffer = new Buffer(size); | |
var val = this.buffer.copy(copyBuffer, this.offset, this.offset + size); | |
this.offset += size; | |
return copyBuffer; | |
}; | |
sequentialReader.prototype.readString = function (encoding, size) { | |
if (typeof encoding === 'number') { | |
size = encoding; | |
encoding = 'utf8'; | |
} else { | |
encoding = encoding || 'utf8'; | |
} | |
var val = this.buffer.toString(encoding, this.offset, this.offset + size); | |
val = val.replace(/\0/g, ''); | |
this.offset += size; | |
return val; | |
}; | |
sequentialReader.prototype.slice = function (size) { | |
var subBuffer = this.buffer.slice(this.offset, this.offset + size); | |
this.offset += size; | |
return subBuffer; | |
}; | |
sequentialReader.prototype.createCopy = function (size) { | |
var copyBuffer = new Buffer(size); | |
this.buffer.copy(copyBuffer, 0, this.offset, this.offset + size); | |
this.offset += size; | |
return copyBuffer; | |
}; | |
sequentialReader.prototype.bufferSize = function (){ | |
return this.buffer.length; | |
} | |
module.exports = sequentialReader; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment