steam-condenser
Version:
TypeScript port of steam-condenser.
125 lines • 4.25 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var bignum_1 = __importDefault(require("bignum"));
var ByteBuffer = /** @class */ (function () {
function ByteBuffer(buffer) {
if (typeof buffer === 'undefined') {
this.buffer = Buffer.alloc(0); // Empty buffer: note \0 byte?
}
else {
this.buffer = buffer;
}
this.capacity = this.buffer.length;
this.mylimit = this.capacity;
this.myposition = 0;
}
ByteBuffer.prototype.clear = function () {
this.mylimit = this.capacity;
this.myposition = 0;
this.buffer.fill(0x00);
return this;
};
ByteBuffer.prototype.flip = function () {
this.mylimit = this.myposition;
this.myposition = 0;
return this;
};
ByteBuffer.prototype.get = function (length) {
var l = length;
if (typeof l === 'undefined') {
l = this.mylimit - this.myposition;
}
else if (l > this.remaining()) {
throw new Error('BufferUnderFlorException');
}
var data = this.buffer.slice(this.myposition, this.myposition + l);
this.myposition += l;
return data;
};
ByteBuffer.prototype.getByte = function () {
var res = this.buffer.readInt8(this.myposition);
this.myposition += 1;
return res;
};
ByteBuffer.prototype.getShort = function () {
var res = this.buffer.readInt16LE(this.myposition);
this.myposition += 2;
return res;
};
ByteBuffer.prototype.getUShort = function () {
var res = this.buffer.readUInt16LE(this.myposition);
this.myposition += 2;
return res;
};
ByteBuffer.prototype.getLong = function () {
var res = this.buffer.readInt32LE(this.myposition);
this.myposition += 4;
return res;
};
ByteBuffer.prototype.getUnsignedLong = function () {
var res = this.buffer.readUInt32LE(this.myposition);
this.myposition += 4;
return res;
};
ByteBuffer.prototype.getFloat = function () {
var res = this.buffer.readFloatLE(this.myposition);
this.myposition += 4;
return res;
};
ByteBuffer.prototype.getLongLong = function () {
var a = this.buffer.readInt32LE(this.myposition);
this.myposition += 4;
var b = this.buffer.readInt32LE(this.myposition);
this.myposition += 4;
return bignum_1.default.add(a, bignum_1.default.mul(bignum_1.default.pow(2, 16), b)).toString();
};
ByteBuffer.prototype.getString = function () {
var txt = '';
while (this.buffer.readInt8(this.myposition) !== 0x00) {
txt += String.fromCharCode(this.buffer.readInt8(this.myposition));
this.myposition += 1;
}
this.myposition += 1;
return txt;
};
ByteBuffer.prototype.limit = function (newmylimit) {
if (typeof newmylimit === 'undefined') {
return this.mylimit;
}
this.mylimit = newmylimit;
};
ByteBuffer.prototype.position = function (position) {
if (typeof position === 'number') {
this.myposition = position;
}
return this.myposition;
};
ByteBuffer.prototype.getBuffer = function () {
return this.buffer;
};
ByteBuffer.prototype.put = function (source) {
source.copy(this.buffer, this.myposition);
// this.buffer.write(source, this.myposition);
this.myposition += source.length;
return this;
};
ByteBuffer.prototype.remaining = function () {
return this.mylimit - this.myposition;
};
ByteBuffer.prototype.rewind = function () {
this.myposition = 0;
return this;
};
ByteBuffer.Allocate = function (length) {
return new ByteBuffer(Buffer.alloc(length, 0x00));
};
ByteBuffer.Wrap = function (buffer) {
return new ByteBuffer(buffer);
};
return ByteBuffer;
}());
exports.default = ByteBuffer;
//# sourceMappingURL=ByteBuffer.js.map