pao-aop-server
Version:
基于pao-aop的服务端框架
514 lines (512 loc) • 17.4 kB
JavaScript
/*
* 版权: Copyright (c) 2018 red
*
* 文件: buffer
* 创建日期: Friday September 21st 2018
* 作者: pao
* 说明:
* 1、缓冲区
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* 偏移位置
*/
var OffsetPosition;
(function (OffsetPosition) {
OffsetPosition[OffsetPosition["start"] = 0] = "start";
OffsetPosition[OffsetPosition["current"] = 1] = "current";
OffsetPosition[OffsetPosition["end"] = 2] = "end";
})(OffsetPosition = exports.OffsetPosition || (exports.OffsetPosition = {}));
/**
* 将数据缓存转换为字符串
* @param data 数据缓存
*/
function bufferToBase64(data) {
return data.toString('base64');
}
exports.bufferToBase64 = bufferToBase64;
/**
* 数据缓存
* @param json json 字符串
*/
function bufferFromBase64(json) {
return Buffer.from(json, 'base64');
}
exports.bufferFromBase64 = bufferFromBase64;
/**
* 缓冲区大小,每次分配内存的大小
*/
var BufferSize = 1024;
/**
* 缓冲区定位器
*/
var BufferLocator = /** @class */ (function () {
function BufferLocator() {
/**
* 当前位置
*/
this.position = 0;
/**
* Buffer的实际长度
*/
this.bufferLength = 0;
}
Object.defineProperty(BufferLocator.prototype, "bufferSize", {
/**
* 获取缓冲区大小
*/
get: function () {
return this.bufferLength;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BufferLocator.prototype, "currentPosition", {
/**
* 当前位置
*/
get: function () {
return this.position;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BufferLocator.prototype, "isEnd", {
/**
* 是否已经读取完毕
*/
get: function () {
return this.position === this.bufferLength;
},
enumerable: true,
configurable: true
});
/**
* 获取缓冲区
*/
BufferLocator.prototype.getBuffer = function () {
return this.buffer.slice(0, this.bufferLength);
};
/**
* 位置偏移
* @param startPos 启动位置
* @param offset 偏移
* @returns 偏移后的位置
*/
BufferLocator.prototype.offset = function (startPos, offset) {
switch (startPos) {
case OffsetPosition.start:
this.position = offset;
break;
case OffsetPosition.current:
this.position += offset;
break;
case OffsetPosition.end:
this.position = this.bufferLength + offset;
break;
default:
}
return this.position;
};
return BufferLocator;
}());
exports.BufferLocator = BufferLocator;
/**
* 缓冲区读取器
*/
var BufferReader = /** @class */ (function (_super) {
__extends(BufferReader, _super);
function BufferReader(buffer) {
var _this = _super.call(this) || this;
if (buffer) {
_this.buffer = buffer;
_this.bufferLength = buffer.length;
}
return _this;
}
/**
* 读取字符串
* @param encoding 编码格式
* @returns 字符串
*/
BufferReader.prototype.readString = function (encoding) {
var length = this.buffer.readInt32BE(this.position);
var data = this.buffer.toString(encoding, this.position + 4, this.position + 4 + length);
this.position += length + 4;
return data;
};
/**
* 读取数据缓冲
* @returns 数据缓冲
*/
BufferReader.prototype.readBuffer = function () {
var length = this.buffer.readInt32BE(this.position);
var data = Buffer.alloc(length);
this.buffer.copy(data, 0, this.position + 4, this.position + 4 + length);
this.position += length + 4;
return data;
};
/**
* 读取UInt8
* @returns UInt8
*/
BufferReader.prototype.readUInt8 = function () {
var data = this.buffer.readUInt8(this.position);
this.position += 1;
return data;
};
/**
* 读取UInt16LE
* @returns UInt16LE
*/
BufferReader.prototype.readUInt16LE = function () {
var data = this.buffer.readUInt16LE(this.position);
this.position += 2;
return data;
};
/**
* 读取UInt16BE
* @returns UInt16BE
*/
BufferReader.prototype.readUInt16BE = function () {
var data = this.buffer.readUInt16BE(this.position);
this.position += 2;
return data;
};
/**
* 读取UInt32LE
* @returns UInt32LE
*/
BufferReader.prototype.readUInt32LE = function () {
var data = this.buffer.readUInt32LE(this.position);
this.position += 4;
return data;
};
/**
* 读取UInt32BE
* @returns UInt32BE
*/
BufferReader.prototype.readUInt32BE = function () {
var data = this.buffer.readUInt32BE(this.position);
this.position += 4;
return data;
};
/**
* 读取Int8
* @returns Int8
*/
BufferReader.prototype.readInt8 = function () {
var data = this.buffer.readInt8(this.position);
this.position += 1;
return data;
};
/**
* 读取Int16LE
* @returns Int16LE
*/
BufferReader.prototype.readInt16LE = function () {
var data = this.buffer.readInt16LE(this.position);
this.position += 2;
return data;
};
/**
* 读取Int16BE
* @returns Int16BE
*/
BufferReader.prototype.readInt16BE = function () {
var data = this.buffer.readInt16BE(this.position);
this.position += 2;
return data;
};
/**
* 读取Int32LE
* @returns Int32LE
*/
BufferReader.prototype.readInt32LE = function () {
var data = this.buffer.readInt32LE(this.position);
this.position += 4;
return data;
};
/**
* 读取Int32BE
* @returns Int32BE
*/
BufferReader.prototype.readInt32BE = function () {
var data = this.buffer.readInt32BE(this.position);
this.position += 4;
return data;
};
/**
* 读取FloatLE
* @returns FloatLE
*/
BufferReader.prototype.readFloatLE = function () {
var data = this.buffer.readFloatLE(this.position);
this.position += 4;
return data;
};
/**
* 读取FloatBE
* @returns FloatBE
*/
BufferReader.prototype.readFloatBE = function () {
var data = this.buffer.readFloatBE(this.position);
this.position += 4;
return data;
};
/**
* 读取DoubleLE
* @returns DoubleLE
*/
BufferReader.prototype.readDoubleLE = function () {
var data = this.buffer.readDoubleLE(this.position);
this.position += 8;
return data;
};
/**
* 读取DoubleBE
* @returns DoubleBE
*/
BufferReader.prototype.readDoubleBE = function () {
var data = this.buffer.readDoubleBE(this.position);
this.position += 8;
return data;
};
return BufferReader;
}(BufferLocator));
exports.BufferReader = BufferReader;
/**
* 写缓冲区的工具
*/
var BufferWriter = /** @class */ (function (_super) {
__extends(BufferWriter, _super);
function BufferWriter() {
var _this = _super.call(this) || this;
_this.buffer = Buffer.alloc(BufferSize);
return _this;
}
/**
* 写入字符串
* @param data 数据
* @param encoding 编码格式
* @returns 写入的字符串缓冲区大小
*/
BufferWriter.prototype.writeString = function (data, encoding) {
var stringBufferSize = Buffer.byteLength(data, encoding);
this.allocate(4 + stringBufferSize);
this.buffer.writeInt32BE(stringBufferSize, this.position);
this.buffer.write(data, this.position + 4, data.length, encoding);
this.position += stringBufferSize + 4;
this.bufferLength = Math.max(this.bufferLength, this.position);
return stringBufferSize;
};
/**
* 写入数据缓存
* @param data 数据
* @returns 写入后的位置
*/
BufferWriter.prototype.writeBuffer = function (data) {
this.allocate(4 + data.length);
this.buffer.writeInt32BE(data.length, this.position);
data.copy(this.buffer, this.position + 4, 0, data.length);
this.position += data.length + 4;
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入UInt8
* @param value UInt8
* @returns 写入后的位置
*/
BufferWriter.prototype.writeUInt8 = function (value) {
this.allocate(1);
this.position = this.buffer.writeUInt8(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入UInt16LE
* @param value UInt16LE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeUInt16LE = function (value) {
this.allocate(2);
this.position = this.buffer.writeUInt16LE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入UInt16BE
* @param value UInt16BE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeUInt16BE = function (value) {
this.allocate(2);
this.position = this.buffer.writeUInt16BE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入UInt32LE
* @param value UInt32LE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeUInt32LE = function (value) {
this.allocate(4);
this.position = this.buffer.writeUInt32LE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入UInt32BE
* @param value UInt32BE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeUInt32BE = function (value) {
this.allocate(4);
this.position = this.buffer.writeUInt32BE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入Int8
* @param value Int8
* @returns 写入后的位置
*/
BufferWriter.prototype.writeInt8 = function (value) {
this.allocate(1);
this.position = this.buffer.writeInt8(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入Int16LE
* @param value Int16LE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeInt16LE = function (value) {
this.allocate(2);
this.position = this.buffer.writeInt16LE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入Int16BE
* @param value Int16BE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeInt16BE = function (value) {
this.allocate(2);
this.position = this.buffer.writeInt16BE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入Int32LE
* @param value Int32LE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeInt32LE = function (value) {
this.allocate(4);
this.position = this.buffer.writeInt32LE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入Int32BE
* @param value Int32BE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeInt32BE = function (value) {
this.allocate(4);
this.position = this.buffer.writeInt32BE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入FloatLE
* @param value FloatLE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeFloatLE = function (value) {
this.allocate(4);
this.position = this.buffer.writeFloatLE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入FloatBE
* @param value FloatBE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeFloatBE = function (value) {
this.allocate(4);
this.position = this.buffer.writeFloatBE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入DoubleLE
* @param value DoubleLE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeDoubleLE = function (value) {
this.allocate(8);
this.position = this.buffer.writeDoubleLE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 写入DoubleBE
* @param value DoubleBE
* @returns 写入后的位置
*/
BufferWriter.prototype.writeDoubleBE = function (value) {
this.allocate(8);
this.position = this.buffer.writeDoubleBE(value, this.position);
this.bufferLength = Math.max(this.bufferLength, this.position);
return this.position;
};
/**
* 分配内存
* @param needSize 需要的大小
* @returns 如果新分配了内存,则返回True,否则,返回False
*/
BufferWriter.prototype.allocate = function (needSize) {
if (this.bufferLength + needSize > this.buffer.length) {
// 扩大Buffer
var newBuffer = Buffer.alloc(this.buffer.length + Math.max(BufferSize, needSize));
this.buffer.copy(newBuffer);
this.buffer = newBuffer;
return true;
}
return false;
};
return BufferWriter;
}(BufferLocator));
exports.BufferWriter = BufferWriter;
});
//# sourceMappingURL=index.js.map