UNPKG

biner

Version:

Declarative binary data encoder / decoder.

168 lines 4.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const stream_1 = require("stream"); const bl_1 = require("./bl"); const errors_1 = require("./errors"); class BinaryStream extends stream_1.Transform { constructor(options = {}) { super(options); this._bl = new bl_1.BufferList(); } get buffer() { return this._bl; } get length() { return this.buffer.length; } append(buf) { this.buffer.append(buf); } get(i) { return this.buffer.get(i); } slice(start, end) { return this.buffer.slice(start, end); } consume(bytes) { this.buffer.consume(bytes); } toString(encoding, start, end) { return this.buffer.toString(encoding, start, end); } indexOf(byte, offset = 0) { return this.buffer.indexOf(byte, offset); } readBuffer(size) { assertSize(size, this.length); const buf = this.slice(0, size); this.consume(size); return buf; } writeBuffer(chunk) { this.append(chunk); } doRead(method, size) { assertSize(size, this.length); let res = this.buffer[method](0, size); this.consume(size); return res; } doWrite(method, value, size) { return size === undefined ? this.buffer[method](value) : this.buffer[method](value, size); } readDoubleBE() { return this.doRead('readDoubleBE', 8); } readDoubleLE() { return this.doRead('readDoubleLE', 8); } readFloatBE() { return this.doRead('readFloatBE', 4); } readFloatLE() { return this.doRead('readFloatLE', 4); } readInt16BE() { return this.doRead('readInt16BE', 2); } readInt16LE() { return this.doRead('readInt16LE', 2); } readInt32BE() { return this.doRead('readInt32BE', 4); } readInt32LE() { return this.doRead('readInt32LE', 4); } readInt8() { return this.doRead('readInt8', 1); } readIntBE(byteLength) { return this.doRead('readIntBE', byteLength); } readIntLE(byteLength) { return this.doRead('readIntLE', byteLength); } readUInt16BE() { return this.doRead('readUInt16BE', 2); } readUInt16LE() { return this.doRead('readUInt16LE', 2); } readUInt32BE() { return this.doRead('readUInt32BE', 4); } readUInt32LE() { return this.doRead('readUInt32LE', 4); } readUInt8() { return this.doRead('readUInt8', 1); } readUIntBE(byteLength) { return this.doRead('readUIntBE', byteLength); } readUIntLE(byteLength) { return this.doRead('readUIntLE', byteLength); } writeDoubleBE(value) { return this.doWrite('writeDoubleBE', value); } writeDoubleLE(value) { return this.doWrite('writeDoubleLE', value); } writeFloatBE(value) { return this.doWrite('writeFloatBE', value); } writeFloatLE(value) { return this.doWrite('writeFloatLE', value); } writeInt16BE(value) { return this.doWrite('writeInt16BE', value); } writeInt16LE(value) { return this.doWrite('writeInt16LE', value); } writeInt32BE(value) { return this.doWrite('writeInt32BE', value); } writeInt32LE(value) { return this.doWrite('writeInt32LE', value); } writeInt8(value) { return this.doWrite('writeInt8', value); } writeIntBE(value, byteLength) { return this.doWrite('writeIntBE', value, byteLength); } writeIntLE(value, byteLength) { return this.doWrite('writeIntLE', value, byteLength); } writeUInt16BE(value) { return this.doWrite('writeUInt16BE', value); } writeUInt16LE(value) { return this.doWrite('writeUInt16LE', value); } writeUInt32BE(value) { return this.doWrite('writeUInt32BE', value); } writeUInt32LE(value) { return this.doWrite('writeUInt32LE', value); } writeUInt8(value) { return this.doWrite('writeUInt8', value); } writeUIntBE(value, byteLength) { return this.doWrite('writeUIntBE', value, byteLength); } writeUIntLE(value, byteLength) { return this.doWrite('writeUIntLE', value, byteLength); } } exports.BinaryStream = BinaryStream; function assertSize(size, length) { if (size > length) { throw new errors_1.NotEnoughDataError(size, length); } } //# sourceMappingURL=bs.js.map