11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_ciObject.defineProperty(exports, "__esModule", { value: true }); 31cb0ef41Sopenharmony_ciconst utils_1 = require("./utils"); 41cb0ef41Sopenharmony_ci// The default Buffer size if one is not provided. 51cb0ef41Sopenharmony_ciconst DEFAULT_SMARTBUFFER_SIZE = 4096; 61cb0ef41Sopenharmony_ci// The default string encoding to use for reading/writing strings. 71cb0ef41Sopenharmony_ciconst DEFAULT_SMARTBUFFER_ENCODING = 'utf8'; 81cb0ef41Sopenharmony_ciclass SmartBuffer { 91cb0ef41Sopenharmony_ci /** 101cb0ef41Sopenharmony_ci * Creates a new SmartBuffer instance. 111cb0ef41Sopenharmony_ci * 121cb0ef41Sopenharmony_ci * @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance. 131cb0ef41Sopenharmony_ci */ 141cb0ef41Sopenharmony_ci constructor(options) { 151cb0ef41Sopenharmony_ci this.length = 0; 161cb0ef41Sopenharmony_ci this._encoding = DEFAULT_SMARTBUFFER_ENCODING; 171cb0ef41Sopenharmony_ci this._writeOffset = 0; 181cb0ef41Sopenharmony_ci this._readOffset = 0; 191cb0ef41Sopenharmony_ci if (SmartBuffer.isSmartBufferOptions(options)) { 201cb0ef41Sopenharmony_ci // Checks for encoding 211cb0ef41Sopenharmony_ci if (options.encoding) { 221cb0ef41Sopenharmony_ci utils_1.checkEncoding(options.encoding); 231cb0ef41Sopenharmony_ci this._encoding = options.encoding; 241cb0ef41Sopenharmony_ci } 251cb0ef41Sopenharmony_ci // Checks for initial size length 261cb0ef41Sopenharmony_ci if (options.size) { 271cb0ef41Sopenharmony_ci if (utils_1.isFiniteInteger(options.size) && options.size > 0) { 281cb0ef41Sopenharmony_ci this._buff = Buffer.allocUnsafe(options.size); 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci else { 311cb0ef41Sopenharmony_ci throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_SIZE); 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci // Check for initial Buffer 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci else if (options.buff) { 361cb0ef41Sopenharmony_ci if (Buffer.isBuffer(options.buff)) { 371cb0ef41Sopenharmony_ci this._buff = options.buff; 381cb0ef41Sopenharmony_ci this.length = options.buff.length; 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci else { 411cb0ef41Sopenharmony_ci throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_BUFFER); 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci } 441cb0ef41Sopenharmony_ci else { 451cb0ef41Sopenharmony_ci this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE); 461cb0ef41Sopenharmony_ci } 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci else { 491cb0ef41Sopenharmony_ci // If something was passed but it's not a SmartBufferOptions object 501cb0ef41Sopenharmony_ci if (typeof options !== 'undefined') { 511cb0ef41Sopenharmony_ci throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_OBJECT); 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci // Otherwise default to sane options 541cb0ef41Sopenharmony_ci this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci /** 581cb0ef41Sopenharmony_ci * Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding. 591cb0ef41Sopenharmony_ci * 601cb0ef41Sopenharmony_ci * @param size { Number } The size of the internal Buffer. 611cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for strings. 621cb0ef41Sopenharmony_ci * 631cb0ef41Sopenharmony_ci * @return { SmartBuffer } 641cb0ef41Sopenharmony_ci */ 651cb0ef41Sopenharmony_ci static fromSize(size, encoding) { 661cb0ef41Sopenharmony_ci return new this({ 671cb0ef41Sopenharmony_ci size: size, 681cb0ef41Sopenharmony_ci encoding: encoding 691cb0ef41Sopenharmony_ci }); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci /** 721cb0ef41Sopenharmony_ci * Creates a new SmartBuffer instance with the provided Buffer and optional encoding. 731cb0ef41Sopenharmony_ci * 741cb0ef41Sopenharmony_ci * @param buffer { Buffer } The Buffer to use as the internal Buffer value. 751cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for strings. 761cb0ef41Sopenharmony_ci * 771cb0ef41Sopenharmony_ci * @return { SmartBuffer } 781cb0ef41Sopenharmony_ci */ 791cb0ef41Sopenharmony_ci static fromBuffer(buff, encoding) { 801cb0ef41Sopenharmony_ci return new this({ 811cb0ef41Sopenharmony_ci buff: buff, 821cb0ef41Sopenharmony_ci encoding: encoding 831cb0ef41Sopenharmony_ci }); 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci /** 861cb0ef41Sopenharmony_ci * Creates a new SmartBuffer instance with the provided SmartBufferOptions options. 871cb0ef41Sopenharmony_ci * 881cb0ef41Sopenharmony_ci * @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance. 891cb0ef41Sopenharmony_ci */ 901cb0ef41Sopenharmony_ci static fromOptions(options) { 911cb0ef41Sopenharmony_ci return new this(options); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci /** 941cb0ef41Sopenharmony_ci * Type checking function that determines if an object is a SmartBufferOptions object. 951cb0ef41Sopenharmony_ci */ 961cb0ef41Sopenharmony_ci static isSmartBufferOptions(options) { 971cb0ef41Sopenharmony_ci const castOptions = options; 981cb0ef41Sopenharmony_ci return (castOptions && 991cb0ef41Sopenharmony_ci (castOptions.encoding !== undefined || castOptions.size !== undefined || castOptions.buff !== undefined)); 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci // Signed integers 1021cb0ef41Sopenharmony_ci /** 1031cb0ef41Sopenharmony_ci * Reads an Int8 value from the current read position or an optionally provided offset. 1041cb0ef41Sopenharmony_ci * 1051cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1061cb0ef41Sopenharmony_ci * @return { Number } 1071cb0ef41Sopenharmony_ci */ 1081cb0ef41Sopenharmony_ci readInt8(offset) { 1091cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readInt8, 1, offset); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci /** 1121cb0ef41Sopenharmony_ci * Reads an Int16BE value from the current read position or an optionally provided offset. 1131cb0ef41Sopenharmony_ci * 1141cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1151cb0ef41Sopenharmony_ci * @return { Number } 1161cb0ef41Sopenharmony_ci */ 1171cb0ef41Sopenharmony_ci readInt16BE(offset) { 1181cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readInt16BE, 2, offset); 1191cb0ef41Sopenharmony_ci } 1201cb0ef41Sopenharmony_ci /** 1211cb0ef41Sopenharmony_ci * Reads an Int16LE value from the current read position or an optionally provided offset. 1221cb0ef41Sopenharmony_ci * 1231cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1241cb0ef41Sopenharmony_ci * @return { Number } 1251cb0ef41Sopenharmony_ci */ 1261cb0ef41Sopenharmony_ci readInt16LE(offset) { 1271cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readInt16LE, 2, offset); 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci /** 1301cb0ef41Sopenharmony_ci * Reads an Int32BE value from the current read position or an optionally provided offset. 1311cb0ef41Sopenharmony_ci * 1321cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1331cb0ef41Sopenharmony_ci * @return { Number } 1341cb0ef41Sopenharmony_ci */ 1351cb0ef41Sopenharmony_ci readInt32BE(offset) { 1361cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readInt32BE, 4, offset); 1371cb0ef41Sopenharmony_ci } 1381cb0ef41Sopenharmony_ci /** 1391cb0ef41Sopenharmony_ci * Reads an Int32LE value from the current read position or an optionally provided offset. 1401cb0ef41Sopenharmony_ci * 1411cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1421cb0ef41Sopenharmony_ci * @return { Number } 1431cb0ef41Sopenharmony_ci */ 1441cb0ef41Sopenharmony_ci readInt32LE(offset) { 1451cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readInt32LE, 4, offset); 1461cb0ef41Sopenharmony_ci } 1471cb0ef41Sopenharmony_ci /** 1481cb0ef41Sopenharmony_ci * Reads a BigInt64BE value from the current read position or an optionally provided offset. 1491cb0ef41Sopenharmony_ci * 1501cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1511cb0ef41Sopenharmony_ci * @return { BigInt } 1521cb0ef41Sopenharmony_ci */ 1531cb0ef41Sopenharmony_ci readBigInt64BE(offset) { 1541cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('readBigInt64BE'); 1551cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readBigInt64BE, 8, offset); 1561cb0ef41Sopenharmony_ci } 1571cb0ef41Sopenharmony_ci /** 1581cb0ef41Sopenharmony_ci * Reads a BigInt64LE value from the current read position or an optionally provided offset. 1591cb0ef41Sopenharmony_ci * 1601cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 1611cb0ef41Sopenharmony_ci * @return { BigInt } 1621cb0ef41Sopenharmony_ci */ 1631cb0ef41Sopenharmony_ci readBigInt64LE(offset) { 1641cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('readBigInt64LE'); 1651cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readBigInt64LE, 8, offset); 1661cb0ef41Sopenharmony_ci } 1671cb0ef41Sopenharmony_ci /** 1681cb0ef41Sopenharmony_ci * Writes an Int8 value to the current write position (or at optional offset). 1691cb0ef41Sopenharmony_ci * 1701cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 1711cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 1721cb0ef41Sopenharmony_ci * 1731cb0ef41Sopenharmony_ci * @return this 1741cb0ef41Sopenharmony_ci */ 1751cb0ef41Sopenharmony_ci writeInt8(value, offset) { 1761cb0ef41Sopenharmony_ci this._writeNumberValue(Buffer.prototype.writeInt8, 1, value, offset); 1771cb0ef41Sopenharmony_ci return this; 1781cb0ef41Sopenharmony_ci } 1791cb0ef41Sopenharmony_ci /** 1801cb0ef41Sopenharmony_ci * Inserts an Int8 value at the given offset value. 1811cb0ef41Sopenharmony_ci * 1821cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 1831cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 1841cb0ef41Sopenharmony_ci * 1851cb0ef41Sopenharmony_ci * @return this 1861cb0ef41Sopenharmony_ci */ 1871cb0ef41Sopenharmony_ci insertInt8(value, offset) { 1881cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeInt8, 1, value, offset); 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci /** 1911cb0ef41Sopenharmony_ci * Writes an Int16BE value to the current write position (or at optional offset). 1921cb0ef41Sopenharmony_ci * 1931cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 1941cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 1951cb0ef41Sopenharmony_ci * 1961cb0ef41Sopenharmony_ci * @return this 1971cb0ef41Sopenharmony_ci */ 1981cb0ef41Sopenharmony_ci writeInt16BE(value, offset) { 1991cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset); 2001cb0ef41Sopenharmony_ci } 2011cb0ef41Sopenharmony_ci /** 2021cb0ef41Sopenharmony_ci * Inserts an Int16BE value at the given offset value. 2031cb0ef41Sopenharmony_ci * 2041cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 2051cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 2061cb0ef41Sopenharmony_ci * 2071cb0ef41Sopenharmony_ci * @return this 2081cb0ef41Sopenharmony_ci */ 2091cb0ef41Sopenharmony_ci insertInt16BE(value, offset) { 2101cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset); 2111cb0ef41Sopenharmony_ci } 2121cb0ef41Sopenharmony_ci /** 2131cb0ef41Sopenharmony_ci * Writes an Int16LE value to the current write position (or at optional offset). 2141cb0ef41Sopenharmony_ci * 2151cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 2161cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 2171cb0ef41Sopenharmony_ci * 2181cb0ef41Sopenharmony_ci * @return this 2191cb0ef41Sopenharmony_ci */ 2201cb0ef41Sopenharmony_ci writeInt16LE(value, offset) { 2211cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset); 2221cb0ef41Sopenharmony_ci } 2231cb0ef41Sopenharmony_ci /** 2241cb0ef41Sopenharmony_ci * Inserts an Int16LE value at the given offset value. 2251cb0ef41Sopenharmony_ci * 2261cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 2271cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 2281cb0ef41Sopenharmony_ci * 2291cb0ef41Sopenharmony_ci * @return this 2301cb0ef41Sopenharmony_ci */ 2311cb0ef41Sopenharmony_ci insertInt16LE(value, offset) { 2321cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset); 2331cb0ef41Sopenharmony_ci } 2341cb0ef41Sopenharmony_ci /** 2351cb0ef41Sopenharmony_ci * Writes an Int32BE value to the current write position (or at optional offset). 2361cb0ef41Sopenharmony_ci * 2371cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 2381cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 2391cb0ef41Sopenharmony_ci * 2401cb0ef41Sopenharmony_ci * @return this 2411cb0ef41Sopenharmony_ci */ 2421cb0ef41Sopenharmony_ci writeInt32BE(value, offset) { 2431cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset); 2441cb0ef41Sopenharmony_ci } 2451cb0ef41Sopenharmony_ci /** 2461cb0ef41Sopenharmony_ci * Inserts an Int32BE value at the given offset value. 2471cb0ef41Sopenharmony_ci * 2481cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 2491cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 2501cb0ef41Sopenharmony_ci * 2511cb0ef41Sopenharmony_ci * @return this 2521cb0ef41Sopenharmony_ci */ 2531cb0ef41Sopenharmony_ci insertInt32BE(value, offset) { 2541cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset); 2551cb0ef41Sopenharmony_ci } 2561cb0ef41Sopenharmony_ci /** 2571cb0ef41Sopenharmony_ci * Writes an Int32LE value to the current write position (or at optional offset). 2581cb0ef41Sopenharmony_ci * 2591cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 2601cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 2611cb0ef41Sopenharmony_ci * 2621cb0ef41Sopenharmony_ci * @return this 2631cb0ef41Sopenharmony_ci */ 2641cb0ef41Sopenharmony_ci writeInt32LE(value, offset) { 2651cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset); 2661cb0ef41Sopenharmony_ci } 2671cb0ef41Sopenharmony_ci /** 2681cb0ef41Sopenharmony_ci * Inserts an Int32LE value at the given offset value. 2691cb0ef41Sopenharmony_ci * 2701cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 2711cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 2721cb0ef41Sopenharmony_ci * 2731cb0ef41Sopenharmony_ci * @return this 2741cb0ef41Sopenharmony_ci */ 2751cb0ef41Sopenharmony_ci insertInt32LE(value, offset) { 2761cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset); 2771cb0ef41Sopenharmony_ci } 2781cb0ef41Sopenharmony_ci /** 2791cb0ef41Sopenharmony_ci * Writes a BigInt64BE value to the current write position (or at optional offset). 2801cb0ef41Sopenharmony_ci * 2811cb0ef41Sopenharmony_ci * @param value { BigInt } The value to write. 2821cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 2831cb0ef41Sopenharmony_ci * 2841cb0ef41Sopenharmony_ci * @return this 2851cb0ef41Sopenharmony_ci */ 2861cb0ef41Sopenharmony_ci writeBigInt64BE(value, offset) { 2871cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigInt64BE'); 2881cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeBigInt64BE, 8, value, offset); 2891cb0ef41Sopenharmony_ci } 2901cb0ef41Sopenharmony_ci /** 2911cb0ef41Sopenharmony_ci * Inserts a BigInt64BE value at the given offset value. 2921cb0ef41Sopenharmony_ci * 2931cb0ef41Sopenharmony_ci * @param value { BigInt } The value to insert. 2941cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 2951cb0ef41Sopenharmony_ci * 2961cb0ef41Sopenharmony_ci * @return this 2971cb0ef41Sopenharmony_ci */ 2981cb0ef41Sopenharmony_ci insertBigInt64BE(value, offset) { 2991cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigInt64BE'); 3001cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeBigInt64BE, 8, value, offset); 3011cb0ef41Sopenharmony_ci } 3021cb0ef41Sopenharmony_ci /** 3031cb0ef41Sopenharmony_ci * Writes a BigInt64LE value to the current write position (or at optional offset). 3041cb0ef41Sopenharmony_ci * 3051cb0ef41Sopenharmony_ci * @param value { BigInt } The value to write. 3061cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 3071cb0ef41Sopenharmony_ci * 3081cb0ef41Sopenharmony_ci * @return this 3091cb0ef41Sopenharmony_ci */ 3101cb0ef41Sopenharmony_ci writeBigInt64LE(value, offset) { 3111cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigInt64LE'); 3121cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeBigInt64LE, 8, value, offset); 3131cb0ef41Sopenharmony_ci } 3141cb0ef41Sopenharmony_ci /** 3151cb0ef41Sopenharmony_ci * Inserts a Int64LE value at the given offset value. 3161cb0ef41Sopenharmony_ci * 3171cb0ef41Sopenharmony_ci * @param value { BigInt } The value to insert. 3181cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 3191cb0ef41Sopenharmony_ci * 3201cb0ef41Sopenharmony_ci * @return this 3211cb0ef41Sopenharmony_ci */ 3221cb0ef41Sopenharmony_ci insertBigInt64LE(value, offset) { 3231cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigInt64LE'); 3241cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeBigInt64LE, 8, value, offset); 3251cb0ef41Sopenharmony_ci } 3261cb0ef41Sopenharmony_ci // Unsigned Integers 3271cb0ef41Sopenharmony_ci /** 3281cb0ef41Sopenharmony_ci * Reads an UInt8 value from the current read position or an optionally provided offset. 3291cb0ef41Sopenharmony_ci * 3301cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3311cb0ef41Sopenharmony_ci * @return { Number } 3321cb0ef41Sopenharmony_ci */ 3331cb0ef41Sopenharmony_ci readUInt8(offset) { 3341cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readUInt8, 1, offset); 3351cb0ef41Sopenharmony_ci } 3361cb0ef41Sopenharmony_ci /** 3371cb0ef41Sopenharmony_ci * Reads an UInt16BE value from the current read position or an optionally provided offset. 3381cb0ef41Sopenharmony_ci * 3391cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3401cb0ef41Sopenharmony_ci * @return { Number } 3411cb0ef41Sopenharmony_ci */ 3421cb0ef41Sopenharmony_ci readUInt16BE(offset) { 3431cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readUInt16BE, 2, offset); 3441cb0ef41Sopenharmony_ci } 3451cb0ef41Sopenharmony_ci /** 3461cb0ef41Sopenharmony_ci * Reads an UInt16LE value from the current read position or an optionally provided offset. 3471cb0ef41Sopenharmony_ci * 3481cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3491cb0ef41Sopenharmony_ci * @return { Number } 3501cb0ef41Sopenharmony_ci */ 3511cb0ef41Sopenharmony_ci readUInt16LE(offset) { 3521cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readUInt16LE, 2, offset); 3531cb0ef41Sopenharmony_ci } 3541cb0ef41Sopenharmony_ci /** 3551cb0ef41Sopenharmony_ci * Reads an UInt32BE value from the current read position or an optionally provided offset. 3561cb0ef41Sopenharmony_ci * 3571cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3581cb0ef41Sopenharmony_ci * @return { Number } 3591cb0ef41Sopenharmony_ci */ 3601cb0ef41Sopenharmony_ci readUInt32BE(offset) { 3611cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readUInt32BE, 4, offset); 3621cb0ef41Sopenharmony_ci } 3631cb0ef41Sopenharmony_ci /** 3641cb0ef41Sopenharmony_ci * Reads an UInt32LE value from the current read position or an optionally provided offset. 3651cb0ef41Sopenharmony_ci * 3661cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3671cb0ef41Sopenharmony_ci * @return { Number } 3681cb0ef41Sopenharmony_ci */ 3691cb0ef41Sopenharmony_ci readUInt32LE(offset) { 3701cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readUInt32LE, 4, offset); 3711cb0ef41Sopenharmony_ci } 3721cb0ef41Sopenharmony_ci /** 3731cb0ef41Sopenharmony_ci * Reads a BigUInt64BE value from the current read position or an optionally provided offset. 3741cb0ef41Sopenharmony_ci * 3751cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3761cb0ef41Sopenharmony_ci * @return { BigInt } 3771cb0ef41Sopenharmony_ci */ 3781cb0ef41Sopenharmony_ci readBigUInt64BE(offset) { 3791cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('readBigUInt64BE'); 3801cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readBigUInt64BE, 8, offset); 3811cb0ef41Sopenharmony_ci } 3821cb0ef41Sopenharmony_ci /** 3831cb0ef41Sopenharmony_ci * Reads a BigUInt64LE value from the current read position or an optionally provided offset. 3841cb0ef41Sopenharmony_ci * 3851cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 3861cb0ef41Sopenharmony_ci * @return { BigInt } 3871cb0ef41Sopenharmony_ci */ 3881cb0ef41Sopenharmony_ci readBigUInt64LE(offset) { 3891cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('readBigUInt64LE'); 3901cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readBigUInt64LE, 8, offset); 3911cb0ef41Sopenharmony_ci } 3921cb0ef41Sopenharmony_ci /** 3931cb0ef41Sopenharmony_ci * Writes an UInt8 value to the current write position (or at optional offset). 3941cb0ef41Sopenharmony_ci * 3951cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 3961cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 3971cb0ef41Sopenharmony_ci * 3981cb0ef41Sopenharmony_ci * @return this 3991cb0ef41Sopenharmony_ci */ 4001cb0ef41Sopenharmony_ci writeUInt8(value, offset) { 4011cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeUInt8, 1, value, offset); 4021cb0ef41Sopenharmony_ci } 4031cb0ef41Sopenharmony_ci /** 4041cb0ef41Sopenharmony_ci * Inserts an UInt8 value at the given offset value. 4051cb0ef41Sopenharmony_ci * 4061cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 4071cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 4081cb0ef41Sopenharmony_ci * 4091cb0ef41Sopenharmony_ci * @return this 4101cb0ef41Sopenharmony_ci */ 4111cb0ef41Sopenharmony_ci insertUInt8(value, offset) { 4121cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeUInt8, 1, value, offset); 4131cb0ef41Sopenharmony_ci } 4141cb0ef41Sopenharmony_ci /** 4151cb0ef41Sopenharmony_ci * Writes an UInt16BE value to the current write position (or at optional offset). 4161cb0ef41Sopenharmony_ci * 4171cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 4181cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 4191cb0ef41Sopenharmony_ci * 4201cb0ef41Sopenharmony_ci * @return this 4211cb0ef41Sopenharmony_ci */ 4221cb0ef41Sopenharmony_ci writeUInt16BE(value, offset) { 4231cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset); 4241cb0ef41Sopenharmony_ci } 4251cb0ef41Sopenharmony_ci /** 4261cb0ef41Sopenharmony_ci * Inserts an UInt16BE value at the given offset value. 4271cb0ef41Sopenharmony_ci * 4281cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 4291cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 4301cb0ef41Sopenharmony_ci * 4311cb0ef41Sopenharmony_ci * @return this 4321cb0ef41Sopenharmony_ci */ 4331cb0ef41Sopenharmony_ci insertUInt16BE(value, offset) { 4341cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset); 4351cb0ef41Sopenharmony_ci } 4361cb0ef41Sopenharmony_ci /** 4371cb0ef41Sopenharmony_ci * Writes an UInt16LE value to the current write position (or at optional offset). 4381cb0ef41Sopenharmony_ci * 4391cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 4401cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 4411cb0ef41Sopenharmony_ci * 4421cb0ef41Sopenharmony_ci * @return this 4431cb0ef41Sopenharmony_ci */ 4441cb0ef41Sopenharmony_ci writeUInt16LE(value, offset) { 4451cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset); 4461cb0ef41Sopenharmony_ci } 4471cb0ef41Sopenharmony_ci /** 4481cb0ef41Sopenharmony_ci * Inserts an UInt16LE value at the given offset value. 4491cb0ef41Sopenharmony_ci * 4501cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 4511cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 4521cb0ef41Sopenharmony_ci * 4531cb0ef41Sopenharmony_ci * @return this 4541cb0ef41Sopenharmony_ci */ 4551cb0ef41Sopenharmony_ci insertUInt16LE(value, offset) { 4561cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset); 4571cb0ef41Sopenharmony_ci } 4581cb0ef41Sopenharmony_ci /** 4591cb0ef41Sopenharmony_ci * Writes an UInt32BE value to the current write position (or at optional offset). 4601cb0ef41Sopenharmony_ci * 4611cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 4621cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 4631cb0ef41Sopenharmony_ci * 4641cb0ef41Sopenharmony_ci * @return this 4651cb0ef41Sopenharmony_ci */ 4661cb0ef41Sopenharmony_ci writeUInt32BE(value, offset) { 4671cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset); 4681cb0ef41Sopenharmony_ci } 4691cb0ef41Sopenharmony_ci /** 4701cb0ef41Sopenharmony_ci * Inserts an UInt32BE value at the given offset value. 4711cb0ef41Sopenharmony_ci * 4721cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 4731cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 4741cb0ef41Sopenharmony_ci * 4751cb0ef41Sopenharmony_ci * @return this 4761cb0ef41Sopenharmony_ci */ 4771cb0ef41Sopenharmony_ci insertUInt32BE(value, offset) { 4781cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset); 4791cb0ef41Sopenharmony_ci } 4801cb0ef41Sopenharmony_ci /** 4811cb0ef41Sopenharmony_ci * Writes an UInt32LE value to the current write position (or at optional offset). 4821cb0ef41Sopenharmony_ci * 4831cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 4841cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 4851cb0ef41Sopenharmony_ci * 4861cb0ef41Sopenharmony_ci * @return this 4871cb0ef41Sopenharmony_ci */ 4881cb0ef41Sopenharmony_ci writeUInt32LE(value, offset) { 4891cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset); 4901cb0ef41Sopenharmony_ci } 4911cb0ef41Sopenharmony_ci /** 4921cb0ef41Sopenharmony_ci * Inserts an UInt32LE value at the given offset value. 4931cb0ef41Sopenharmony_ci * 4941cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 4951cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 4961cb0ef41Sopenharmony_ci * 4971cb0ef41Sopenharmony_ci * @return this 4981cb0ef41Sopenharmony_ci */ 4991cb0ef41Sopenharmony_ci insertUInt32LE(value, offset) { 5001cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset); 5011cb0ef41Sopenharmony_ci } 5021cb0ef41Sopenharmony_ci /** 5031cb0ef41Sopenharmony_ci * Writes a BigUInt64BE value to the current write position (or at optional offset). 5041cb0ef41Sopenharmony_ci * 5051cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 5061cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 5071cb0ef41Sopenharmony_ci * 5081cb0ef41Sopenharmony_ci * @return this 5091cb0ef41Sopenharmony_ci */ 5101cb0ef41Sopenharmony_ci writeBigUInt64BE(value, offset) { 5111cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigUInt64BE'); 5121cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeBigUInt64BE, 8, value, offset); 5131cb0ef41Sopenharmony_ci } 5141cb0ef41Sopenharmony_ci /** 5151cb0ef41Sopenharmony_ci * Inserts a BigUInt64BE value at the given offset value. 5161cb0ef41Sopenharmony_ci * 5171cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 5181cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 5191cb0ef41Sopenharmony_ci * 5201cb0ef41Sopenharmony_ci * @return this 5211cb0ef41Sopenharmony_ci */ 5221cb0ef41Sopenharmony_ci insertBigUInt64BE(value, offset) { 5231cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigUInt64BE'); 5241cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeBigUInt64BE, 8, value, offset); 5251cb0ef41Sopenharmony_ci } 5261cb0ef41Sopenharmony_ci /** 5271cb0ef41Sopenharmony_ci * Writes a BigUInt64LE value to the current write position (or at optional offset). 5281cb0ef41Sopenharmony_ci * 5291cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 5301cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 5311cb0ef41Sopenharmony_ci * 5321cb0ef41Sopenharmony_ci * @return this 5331cb0ef41Sopenharmony_ci */ 5341cb0ef41Sopenharmony_ci writeBigUInt64LE(value, offset) { 5351cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigUInt64LE'); 5361cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeBigUInt64LE, 8, value, offset); 5371cb0ef41Sopenharmony_ci } 5381cb0ef41Sopenharmony_ci /** 5391cb0ef41Sopenharmony_ci * Inserts a BigUInt64LE value at the given offset value. 5401cb0ef41Sopenharmony_ci * 5411cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 5421cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 5431cb0ef41Sopenharmony_ci * 5441cb0ef41Sopenharmony_ci * @return this 5451cb0ef41Sopenharmony_ci */ 5461cb0ef41Sopenharmony_ci insertBigUInt64LE(value, offset) { 5471cb0ef41Sopenharmony_ci utils_1.bigIntAndBufferInt64Check('writeBigUInt64LE'); 5481cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeBigUInt64LE, 8, value, offset); 5491cb0ef41Sopenharmony_ci } 5501cb0ef41Sopenharmony_ci // Floating Point 5511cb0ef41Sopenharmony_ci /** 5521cb0ef41Sopenharmony_ci * Reads an FloatBE value from the current read position or an optionally provided offset. 5531cb0ef41Sopenharmony_ci * 5541cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 5551cb0ef41Sopenharmony_ci * @return { Number } 5561cb0ef41Sopenharmony_ci */ 5571cb0ef41Sopenharmony_ci readFloatBE(offset) { 5581cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readFloatBE, 4, offset); 5591cb0ef41Sopenharmony_ci } 5601cb0ef41Sopenharmony_ci /** 5611cb0ef41Sopenharmony_ci * Reads an FloatLE value from the current read position or an optionally provided offset. 5621cb0ef41Sopenharmony_ci * 5631cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 5641cb0ef41Sopenharmony_ci * @return { Number } 5651cb0ef41Sopenharmony_ci */ 5661cb0ef41Sopenharmony_ci readFloatLE(offset) { 5671cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readFloatLE, 4, offset); 5681cb0ef41Sopenharmony_ci } 5691cb0ef41Sopenharmony_ci /** 5701cb0ef41Sopenharmony_ci * Writes a FloatBE value to the current write position (or at optional offset). 5711cb0ef41Sopenharmony_ci * 5721cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 5731cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 5741cb0ef41Sopenharmony_ci * 5751cb0ef41Sopenharmony_ci * @return this 5761cb0ef41Sopenharmony_ci */ 5771cb0ef41Sopenharmony_ci writeFloatBE(value, offset) { 5781cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset); 5791cb0ef41Sopenharmony_ci } 5801cb0ef41Sopenharmony_ci /** 5811cb0ef41Sopenharmony_ci * Inserts a FloatBE value at the given offset value. 5821cb0ef41Sopenharmony_ci * 5831cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 5841cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 5851cb0ef41Sopenharmony_ci * 5861cb0ef41Sopenharmony_ci * @return this 5871cb0ef41Sopenharmony_ci */ 5881cb0ef41Sopenharmony_ci insertFloatBE(value, offset) { 5891cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset); 5901cb0ef41Sopenharmony_ci } 5911cb0ef41Sopenharmony_ci /** 5921cb0ef41Sopenharmony_ci * Writes a FloatLE value to the current write position (or at optional offset). 5931cb0ef41Sopenharmony_ci * 5941cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 5951cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 5961cb0ef41Sopenharmony_ci * 5971cb0ef41Sopenharmony_ci * @return this 5981cb0ef41Sopenharmony_ci */ 5991cb0ef41Sopenharmony_ci writeFloatLE(value, offset) { 6001cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset); 6011cb0ef41Sopenharmony_ci } 6021cb0ef41Sopenharmony_ci /** 6031cb0ef41Sopenharmony_ci * Inserts a FloatLE value at the given offset value. 6041cb0ef41Sopenharmony_ci * 6051cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 6061cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 6071cb0ef41Sopenharmony_ci * 6081cb0ef41Sopenharmony_ci * @return this 6091cb0ef41Sopenharmony_ci */ 6101cb0ef41Sopenharmony_ci insertFloatLE(value, offset) { 6111cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset); 6121cb0ef41Sopenharmony_ci } 6131cb0ef41Sopenharmony_ci // Double Floating Point 6141cb0ef41Sopenharmony_ci /** 6151cb0ef41Sopenharmony_ci * Reads an DoublEBE value from the current read position or an optionally provided offset. 6161cb0ef41Sopenharmony_ci * 6171cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 6181cb0ef41Sopenharmony_ci * @return { Number } 6191cb0ef41Sopenharmony_ci */ 6201cb0ef41Sopenharmony_ci readDoubleBE(offset) { 6211cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readDoubleBE, 8, offset); 6221cb0ef41Sopenharmony_ci } 6231cb0ef41Sopenharmony_ci /** 6241cb0ef41Sopenharmony_ci * Reads an DoubleLE value from the current read position or an optionally provided offset. 6251cb0ef41Sopenharmony_ci * 6261cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read data from (optional) 6271cb0ef41Sopenharmony_ci * @return { Number } 6281cb0ef41Sopenharmony_ci */ 6291cb0ef41Sopenharmony_ci readDoubleLE(offset) { 6301cb0ef41Sopenharmony_ci return this._readNumberValue(Buffer.prototype.readDoubleLE, 8, offset); 6311cb0ef41Sopenharmony_ci } 6321cb0ef41Sopenharmony_ci /** 6331cb0ef41Sopenharmony_ci * Writes a DoubleBE value to the current write position (or at optional offset). 6341cb0ef41Sopenharmony_ci * 6351cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 6361cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 6371cb0ef41Sopenharmony_ci * 6381cb0ef41Sopenharmony_ci * @return this 6391cb0ef41Sopenharmony_ci */ 6401cb0ef41Sopenharmony_ci writeDoubleBE(value, offset) { 6411cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset); 6421cb0ef41Sopenharmony_ci } 6431cb0ef41Sopenharmony_ci /** 6441cb0ef41Sopenharmony_ci * Inserts a DoubleBE value at the given offset value. 6451cb0ef41Sopenharmony_ci * 6461cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 6471cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 6481cb0ef41Sopenharmony_ci * 6491cb0ef41Sopenharmony_ci * @return this 6501cb0ef41Sopenharmony_ci */ 6511cb0ef41Sopenharmony_ci insertDoubleBE(value, offset) { 6521cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset); 6531cb0ef41Sopenharmony_ci } 6541cb0ef41Sopenharmony_ci /** 6551cb0ef41Sopenharmony_ci * Writes a DoubleLE value to the current write position (or at optional offset). 6561cb0ef41Sopenharmony_ci * 6571cb0ef41Sopenharmony_ci * @param value { Number } The value to write. 6581cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the value at. 6591cb0ef41Sopenharmony_ci * 6601cb0ef41Sopenharmony_ci * @return this 6611cb0ef41Sopenharmony_ci */ 6621cb0ef41Sopenharmony_ci writeDoubleLE(value, offset) { 6631cb0ef41Sopenharmony_ci return this._writeNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset); 6641cb0ef41Sopenharmony_ci } 6651cb0ef41Sopenharmony_ci /** 6661cb0ef41Sopenharmony_ci * Inserts a DoubleLE value at the given offset value. 6671cb0ef41Sopenharmony_ci * 6681cb0ef41Sopenharmony_ci * @param value { Number } The value to insert. 6691cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the value at. 6701cb0ef41Sopenharmony_ci * 6711cb0ef41Sopenharmony_ci * @return this 6721cb0ef41Sopenharmony_ci */ 6731cb0ef41Sopenharmony_ci insertDoubleLE(value, offset) { 6741cb0ef41Sopenharmony_ci return this._insertNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset); 6751cb0ef41Sopenharmony_ci } 6761cb0ef41Sopenharmony_ci // Strings 6771cb0ef41Sopenharmony_ci /** 6781cb0ef41Sopenharmony_ci * Reads a String from the current read position. 6791cb0ef41Sopenharmony_ci * 6801cb0ef41Sopenharmony_ci * @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for 6811cb0ef41Sopenharmony_ci * the string (Defaults to instance level encoding). 6821cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding). 6831cb0ef41Sopenharmony_ci * 6841cb0ef41Sopenharmony_ci * @return { String } 6851cb0ef41Sopenharmony_ci */ 6861cb0ef41Sopenharmony_ci readString(arg1, encoding) { 6871cb0ef41Sopenharmony_ci let lengthVal; 6881cb0ef41Sopenharmony_ci // Length provided 6891cb0ef41Sopenharmony_ci if (typeof arg1 === 'number') { 6901cb0ef41Sopenharmony_ci utils_1.checkLengthValue(arg1); 6911cb0ef41Sopenharmony_ci lengthVal = Math.min(arg1, this.length - this._readOffset); 6921cb0ef41Sopenharmony_ci } 6931cb0ef41Sopenharmony_ci else { 6941cb0ef41Sopenharmony_ci encoding = arg1; 6951cb0ef41Sopenharmony_ci lengthVal = this.length - this._readOffset; 6961cb0ef41Sopenharmony_ci } 6971cb0ef41Sopenharmony_ci // Check encoding 6981cb0ef41Sopenharmony_ci if (typeof encoding !== 'undefined') { 6991cb0ef41Sopenharmony_ci utils_1.checkEncoding(encoding); 7001cb0ef41Sopenharmony_ci } 7011cb0ef41Sopenharmony_ci const value = this._buff.slice(this._readOffset, this._readOffset + lengthVal).toString(encoding || this._encoding); 7021cb0ef41Sopenharmony_ci this._readOffset += lengthVal; 7031cb0ef41Sopenharmony_ci return value; 7041cb0ef41Sopenharmony_ci } 7051cb0ef41Sopenharmony_ci /** 7061cb0ef41Sopenharmony_ci * Inserts a String 7071cb0ef41Sopenharmony_ci * 7081cb0ef41Sopenharmony_ci * @param value { String } The String value to insert. 7091cb0ef41Sopenharmony_ci * @param offset { Number } The offset to insert the string at. 7101cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding). 7111cb0ef41Sopenharmony_ci * 7121cb0ef41Sopenharmony_ci * @return this 7131cb0ef41Sopenharmony_ci */ 7141cb0ef41Sopenharmony_ci insertString(value, offset, encoding) { 7151cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 7161cb0ef41Sopenharmony_ci return this._handleString(value, true, offset, encoding); 7171cb0ef41Sopenharmony_ci } 7181cb0ef41Sopenharmony_ci /** 7191cb0ef41Sopenharmony_ci * Writes a String 7201cb0ef41Sopenharmony_ci * 7211cb0ef41Sopenharmony_ci * @param value { String } The String value to write. 7221cb0ef41Sopenharmony_ci * @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use. 7231cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding). 7241cb0ef41Sopenharmony_ci * 7251cb0ef41Sopenharmony_ci * @return this 7261cb0ef41Sopenharmony_ci */ 7271cb0ef41Sopenharmony_ci writeString(value, arg2, encoding) { 7281cb0ef41Sopenharmony_ci return this._handleString(value, false, arg2, encoding); 7291cb0ef41Sopenharmony_ci } 7301cb0ef41Sopenharmony_ci /** 7311cb0ef41Sopenharmony_ci * Reads a null-terminated String from the current read position. 7321cb0ef41Sopenharmony_ci * 7331cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding). 7341cb0ef41Sopenharmony_ci * 7351cb0ef41Sopenharmony_ci * @return { String } 7361cb0ef41Sopenharmony_ci */ 7371cb0ef41Sopenharmony_ci readStringNT(encoding) { 7381cb0ef41Sopenharmony_ci if (typeof encoding !== 'undefined') { 7391cb0ef41Sopenharmony_ci utils_1.checkEncoding(encoding); 7401cb0ef41Sopenharmony_ci } 7411cb0ef41Sopenharmony_ci // Set null character position to the end SmartBuffer instance. 7421cb0ef41Sopenharmony_ci let nullPos = this.length; 7431cb0ef41Sopenharmony_ci // Find next null character (if one is not found, default from above is used) 7441cb0ef41Sopenharmony_ci for (let i = this._readOffset; i < this.length; i++) { 7451cb0ef41Sopenharmony_ci if (this._buff[i] === 0x00) { 7461cb0ef41Sopenharmony_ci nullPos = i; 7471cb0ef41Sopenharmony_ci break; 7481cb0ef41Sopenharmony_ci } 7491cb0ef41Sopenharmony_ci } 7501cb0ef41Sopenharmony_ci // Read string value 7511cb0ef41Sopenharmony_ci const value = this._buff.slice(this._readOffset, nullPos); 7521cb0ef41Sopenharmony_ci // Increment internal Buffer read offset 7531cb0ef41Sopenharmony_ci this._readOffset = nullPos + 1; 7541cb0ef41Sopenharmony_ci return value.toString(encoding || this._encoding); 7551cb0ef41Sopenharmony_ci } 7561cb0ef41Sopenharmony_ci /** 7571cb0ef41Sopenharmony_ci * Inserts a null-terminated String. 7581cb0ef41Sopenharmony_ci * 7591cb0ef41Sopenharmony_ci * @param value { String } The String value to write. 7601cb0ef41Sopenharmony_ci * @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use. 7611cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding). 7621cb0ef41Sopenharmony_ci * 7631cb0ef41Sopenharmony_ci * @return this 7641cb0ef41Sopenharmony_ci */ 7651cb0ef41Sopenharmony_ci insertStringNT(value, offset, encoding) { 7661cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 7671cb0ef41Sopenharmony_ci // Write Values 7681cb0ef41Sopenharmony_ci this.insertString(value, offset, encoding); 7691cb0ef41Sopenharmony_ci this.insertUInt8(0x00, offset + value.length); 7701cb0ef41Sopenharmony_ci return this; 7711cb0ef41Sopenharmony_ci } 7721cb0ef41Sopenharmony_ci /** 7731cb0ef41Sopenharmony_ci * Writes a null-terminated String. 7741cb0ef41Sopenharmony_ci * 7751cb0ef41Sopenharmony_ci * @param value { String } The String value to write. 7761cb0ef41Sopenharmony_ci * @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use. 7771cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding). 7781cb0ef41Sopenharmony_ci * 7791cb0ef41Sopenharmony_ci * @return this 7801cb0ef41Sopenharmony_ci */ 7811cb0ef41Sopenharmony_ci writeStringNT(value, arg2, encoding) { 7821cb0ef41Sopenharmony_ci // Write Values 7831cb0ef41Sopenharmony_ci this.writeString(value, arg2, encoding); 7841cb0ef41Sopenharmony_ci this.writeUInt8(0x00, typeof arg2 === 'number' ? arg2 + value.length : this.writeOffset); 7851cb0ef41Sopenharmony_ci return this; 7861cb0ef41Sopenharmony_ci } 7871cb0ef41Sopenharmony_ci // Buffers 7881cb0ef41Sopenharmony_ci /** 7891cb0ef41Sopenharmony_ci * Reads a Buffer from the internal read position. 7901cb0ef41Sopenharmony_ci * 7911cb0ef41Sopenharmony_ci * @param length { Number } The length of data to read as a Buffer. 7921cb0ef41Sopenharmony_ci * 7931cb0ef41Sopenharmony_ci * @return { Buffer } 7941cb0ef41Sopenharmony_ci */ 7951cb0ef41Sopenharmony_ci readBuffer(length) { 7961cb0ef41Sopenharmony_ci if (typeof length !== 'undefined') { 7971cb0ef41Sopenharmony_ci utils_1.checkLengthValue(length); 7981cb0ef41Sopenharmony_ci } 7991cb0ef41Sopenharmony_ci const lengthVal = typeof length === 'number' ? length : this.length; 8001cb0ef41Sopenharmony_ci const endPoint = Math.min(this.length, this._readOffset + lengthVal); 8011cb0ef41Sopenharmony_ci // Read buffer value 8021cb0ef41Sopenharmony_ci const value = this._buff.slice(this._readOffset, endPoint); 8031cb0ef41Sopenharmony_ci // Increment internal Buffer read offset 8041cb0ef41Sopenharmony_ci this._readOffset = endPoint; 8051cb0ef41Sopenharmony_ci return value; 8061cb0ef41Sopenharmony_ci } 8071cb0ef41Sopenharmony_ci /** 8081cb0ef41Sopenharmony_ci * Writes a Buffer to the current write position. 8091cb0ef41Sopenharmony_ci * 8101cb0ef41Sopenharmony_ci * @param value { Buffer } The Buffer to write. 8111cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the Buffer to. 8121cb0ef41Sopenharmony_ci * 8131cb0ef41Sopenharmony_ci * @return this 8141cb0ef41Sopenharmony_ci */ 8151cb0ef41Sopenharmony_ci insertBuffer(value, offset) { 8161cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 8171cb0ef41Sopenharmony_ci return this._handleBuffer(value, true, offset); 8181cb0ef41Sopenharmony_ci } 8191cb0ef41Sopenharmony_ci /** 8201cb0ef41Sopenharmony_ci * Writes a Buffer to the current write position. 8211cb0ef41Sopenharmony_ci * 8221cb0ef41Sopenharmony_ci * @param value { Buffer } The Buffer to write. 8231cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the Buffer to. 8241cb0ef41Sopenharmony_ci * 8251cb0ef41Sopenharmony_ci * @return this 8261cb0ef41Sopenharmony_ci */ 8271cb0ef41Sopenharmony_ci writeBuffer(value, offset) { 8281cb0ef41Sopenharmony_ci return this._handleBuffer(value, false, offset); 8291cb0ef41Sopenharmony_ci } 8301cb0ef41Sopenharmony_ci /** 8311cb0ef41Sopenharmony_ci * Reads a null-terminated Buffer from the current read poisiton. 8321cb0ef41Sopenharmony_ci * 8331cb0ef41Sopenharmony_ci * @return { Buffer } 8341cb0ef41Sopenharmony_ci */ 8351cb0ef41Sopenharmony_ci readBufferNT() { 8361cb0ef41Sopenharmony_ci // Set null character position to the end SmartBuffer instance. 8371cb0ef41Sopenharmony_ci let nullPos = this.length; 8381cb0ef41Sopenharmony_ci // Find next null character (if one is not found, default from above is used) 8391cb0ef41Sopenharmony_ci for (let i = this._readOffset; i < this.length; i++) { 8401cb0ef41Sopenharmony_ci if (this._buff[i] === 0x00) { 8411cb0ef41Sopenharmony_ci nullPos = i; 8421cb0ef41Sopenharmony_ci break; 8431cb0ef41Sopenharmony_ci } 8441cb0ef41Sopenharmony_ci } 8451cb0ef41Sopenharmony_ci // Read value 8461cb0ef41Sopenharmony_ci const value = this._buff.slice(this._readOffset, nullPos); 8471cb0ef41Sopenharmony_ci // Increment internal Buffer read offset 8481cb0ef41Sopenharmony_ci this._readOffset = nullPos + 1; 8491cb0ef41Sopenharmony_ci return value; 8501cb0ef41Sopenharmony_ci } 8511cb0ef41Sopenharmony_ci /** 8521cb0ef41Sopenharmony_ci * Inserts a null-terminated Buffer. 8531cb0ef41Sopenharmony_ci * 8541cb0ef41Sopenharmony_ci * @param value { Buffer } The Buffer to write. 8551cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the Buffer to. 8561cb0ef41Sopenharmony_ci * 8571cb0ef41Sopenharmony_ci * @return this 8581cb0ef41Sopenharmony_ci */ 8591cb0ef41Sopenharmony_ci insertBufferNT(value, offset) { 8601cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 8611cb0ef41Sopenharmony_ci // Write Values 8621cb0ef41Sopenharmony_ci this.insertBuffer(value, offset); 8631cb0ef41Sopenharmony_ci this.insertUInt8(0x00, offset + value.length); 8641cb0ef41Sopenharmony_ci return this; 8651cb0ef41Sopenharmony_ci } 8661cb0ef41Sopenharmony_ci /** 8671cb0ef41Sopenharmony_ci * Writes a null-terminated Buffer. 8681cb0ef41Sopenharmony_ci * 8691cb0ef41Sopenharmony_ci * @param value { Buffer } The Buffer to write. 8701cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the Buffer to. 8711cb0ef41Sopenharmony_ci * 8721cb0ef41Sopenharmony_ci * @return this 8731cb0ef41Sopenharmony_ci */ 8741cb0ef41Sopenharmony_ci writeBufferNT(value, offset) { 8751cb0ef41Sopenharmony_ci // Checks for valid numberic value; 8761cb0ef41Sopenharmony_ci if (typeof offset !== 'undefined') { 8771cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 8781cb0ef41Sopenharmony_ci } 8791cb0ef41Sopenharmony_ci // Write Values 8801cb0ef41Sopenharmony_ci this.writeBuffer(value, offset); 8811cb0ef41Sopenharmony_ci this.writeUInt8(0x00, typeof offset === 'number' ? offset + value.length : this._writeOffset); 8821cb0ef41Sopenharmony_ci return this; 8831cb0ef41Sopenharmony_ci } 8841cb0ef41Sopenharmony_ci /** 8851cb0ef41Sopenharmony_ci * Clears the SmartBuffer instance to its original empty state. 8861cb0ef41Sopenharmony_ci */ 8871cb0ef41Sopenharmony_ci clear() { 8881cb0ef41Sopenharmony_ci this._writeOffset = 0; 8891cb0ef41Sopenharmony_ci this._readOffset = 0; 8901cb0ef41Sopenharmony_ci this.length = 0; 8911cb0ef41Sopenharmony_ci return this; 8921cb0ef41Sopenharmony_ci } 8931cb0ef41Sopenharmony_ci /** 8941cb0ef41Sopenharmony_ci * Gets the remaining data left to be read from the SmartBuffer instance. 8951cb0ef41Sopenharmony_ci * 8961cb0ef41Sopenharmony_ci * @return { Number } 8971cb0ef41Sopenharmony_ci */ 8981cb0ef41Sopenharmony_ci remaining() { 8991cb0ef41Sopenharmony_ci return this.length - this._readOffset; 9001cb0ef41Sopenharmony_ci } 9011cb0ef41Sopenharmony_ci /** 9021cb0ef41Sopenharmony_ci * Gets the current read offset value of the SmartBuffer instance. 9031cb0ef41Sopenharmony_ci * 9041cb0ef41Sopenharmony_ci * @return { Number } 9051cb0ef41Sopenharmony_ci */ 9061cb0ef41Sopenharmony_ci get readOffset() { 9071cb0ef41Sopenharmony_ci return this._readOffset; 9081cb0ef41Sopenharmony_ci } 9091cb0ef41Sopenharmony_ci /** 9101cb0ef41Sopenharmony_ci * Sets the read offset value of the SmartBuffer instance. 9111cb0ef41Sopenharmony_ci * 9121cb0ef41Sopenharmony_ci * @param offset { Number } - The offset value to set. 9131cb0ef41Sopenharmony_ci */ 9141cb0ef41Sopenharmony_ci set readOffset(offset) { 9151cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 9161cb0ef41Sopenharmony_ci // Check for bounds. 9171cb0ef41Sopenharmony_ci utils_1.checkTargetOffset(offset, this); 9181cb0ef41Sopenharmony_ci this._readOffset = offset; 9191cb0ef41Sopenharmony_ci } 9201cb0ef41Sopenharmony_ci /** 9211cb0ef41Sopenharmony_ci * Gets the current write offset value of the SmartBuffer instance. 9221cb0ef41Sopenharmony_ci * 9231cb0ef41Sopenharmony_ci * @return { Number } 9241cb0ef41Sopenharmony_ci */ 9251cb0ef41Sopenharmony_ci get writeOffset() { 9261cb0ef41Sopenharmony_ci return this._writeOffset; 9271cb0ef41Sopenharmony_ci } 9281cb0ef41Sopenharmony_ci /** 9291cb0ef41Sopenharmony_ci * Sets the write offset value of the SmartBuffer instance. 9301cb0ef41Sopenharmony_ci * 9311cb0ef41Sopenharmony_ci * @param offset { Number } - The offset value to set. 9321cb0ef41Sopenharmony_ci */ 9331cb0ef41Sopenharmony_ci set writeOffset(offset) { 9341cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 9351cb0ef41Sopenharmony_ci // Check for bounds. 9361cb0ef41Sopenharmony_ci utils_1.checkTargetOffset(offset, this); 9371cb0ef41Sopenharmony_ci this._writeOffset = offset; 9381cb0ef41Sopenharmony_ci } 9391cb0ef41Sopenharmony_ci /** 9401cb0ef41Sopenharmony_ci * Gets the currently set string encoding of the SmartBuffer instance. 9411cb0ef41Sopenharmony_ci * 9421cb0ef41Sopenharmony_ci * @return { BufferEncoding } The string Buffer encoding currently set. 9431cb0ef41Sopenharmony_ci */ 9441cb0ef41Sopenharmony_ci get encoding() { 9451cb0ef41Sopenharmony_ci return this._encoding; 9461cb0ef41Sopenharmony_ci } 9471cb0ef41Sopenharmony_ci /** 9481cb0ef41Sopenharmony_ci * Sets the string encoding of the SmartBuffer instance. 9491cb0ef41Sopenharmony_ci * 9501cb0ef41Sopenharmony_ci * @param encoding { BufferEncoding } The string Buffer encoding to set. 9511cb0ef41Sopenharmony_ci */ 9521cb0ef41Sopenharmony_ci set encoding(encoding) { 9531cb0ef41Sopenharmony_ci utils_1.checkEncoding(encoding); 9541cb0ef41Sopenharmony_ci this._encoding = encoding; 9551cb0ef41Sopenharmony_ci } 9561cb0ef41Sopenharmony_ci /** 9571cb0ef41Sopenharmony_ci * Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer) 9581cb0ef41Sopenharmony_ci * 9591cb0ef41Sopenharmony_ci * @return { Buffer } The Buffer value. 9601cb0ef41Sopenharmony_ci */ 9611cb0ef41Sopenharmony_ci get internalBuffer() { 9621cb0ef41Sopenharmony_ci return this._buff; 9631cb0ef41Sopenharmony_ci } 9641cb0ef41Sopenharmony_ci /** 9651cb0ef41Sopenharmony_ci * Gets the value of the internal managed Buffer (Includes managed data only) 9661cb0ef41Sopenharmony_ci * 9671cb0ef41Sopenharmony_ci * @param { Buffer } 9681cb0ef41Sopenharmony_ci */ 9691cb0ef41Sopenharmony_ci toBuffer() { 9701cb0ef41Sopenharmony_ci return this._buff.slice(0, this.length); 9711cb0ef41Sopenharmony_ci } 9721cb0ef41Sopenharmony_ci /** 9731cb0ef41Sopenharmony_ci * Gets the String value of the internal managed Buffer 9741cb0ef41Sopenharmony_ci * 9751cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding). 9761cb0ef41Sopenharmony_ci */ 9771cb0ef41Sopenharmony_ci toString(encoding) { 9781cb0ef41Sopenharmony_ci const encodingVal = typeof encoding === 'string' ? encoding : this._encoding; 9791cb0ef41Sopenharmony_ci // Check for invalid encoding. 9801cb0ef41Sopenharmony_ci utils_1.checkEncoding(encodingVal); 9811cb0ef41Sopenharmony_ci return this._buff.toString(encodingVal, 0, this.length); 9821cb0ef41Sopenharmony_ci } 9831cb0ef41Sopenharmony_ci /** 9841cb0ef41Sopenharmony_ci * Destroys the SmartBuffer instance. 9851cb0ef41Sopenharmony_ci */ 9861cb0ef41Sopenharmony_ci destroy() { 9871cb0ef41Sopenharmony_ci this.clear(); 9881cb0ef41Sopenharmony_ci return this; 9891cb0ef41Sopenharmony_ci } 9901cb0ef41Sopenharmony_ci /** 9911cb0ef41Sopenharmony_ci * Handles inserting and writing strings. 9921cb0ef41Sopenharmony_ci * 9931cb0ef41Sopenharmony_ci * @param value { String } The String value to insert. 9941cb0ef41Sopenharmony_ci * @param isInsert { Boolean } True if inserting a string, false if writing. 9951cb0ef41Sopenharmony_ci * @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use. 9961cb0ef41Sopenharmony_ci * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding). 9971cb0ef41Sopenharmony_ci */ 9981cb0ef41Sopenharmony_ci _handleString(value, isInsert, arg3, encoding) { 9991cb0ef41Sopenharmony_ci let offsetVal = this._writeOffset; 10001cb0ef41Sopenharmony_ci let encodingVal = this._encoding; 10011cb0ef41Sopenharmony_ci // Check for offset 10021cb0ef41Sopenharmony_ci if (typeof arg3 === 'number') { 10031cb0ef41Sopenharmony_ci offsetVal = arg3; 10041cb0ef41Sopenharmony_ci // Check for encoding 10051cb0ef41Sopenharmony_ci } 10061cb0ef41Sopenharmony_ci else if (typeof arg3 === 'string') { 10071cb0ef41Sopenharmony_ci utils_1.checkEncoding(arg3); 10081cb0ef41Sopenharmony_ci encodingVal = arg3; 10091cb0ef41Sopenharmony_ci } 10101cb0ef41Sopenharmony_ci // Check for encoding (third param) 10111cb0ef41Sopenharmony_ci if (typeof encoding === 'string') { 10121cb0ef41Sopenharmony_ci utils_1.checkEncoding(encoding); 10131cb0ef41Sopenharmony_ci encodingVal = encoding; 10141cb0ef41Sopenharmony_ci } 10151cb0ef41Sopenharmony_ci // Calculate bytelength of string. 10161cb0ef41Sopenharmony_ci const byteLength = Buffer.byteLength(value, encodingVal); 10171cb0ef41Sopenharmony_ci // Ensure there is enough internal Buffer capacity. 10181cb0ef41Sopenharmony_ci if (isInsert) { 10191cb0ef41Sopenharmony_ci this.ensureInsertable(byteLength, offsetVal); 10201cb0ef41Sopenharmony_ci } 10211cb0ef41Sopenharmony_ci else { 10221cb0ef41Sopenharmony_ci this._ensureWriteable(byteLength, offsetVal); 10231cb0ef41Sopenharmony_ci } 10241cb0ef41Sopenharmony_ci // Write value 10251cb0ef41Sopenharmony_ci this._buff.write(value, offsetVal, byteLength, encodingVal); 10261cb0ef41Sopenharmony_ci // Increment internal Buffer write offset; 10271cb0ef41Sopenharmony_ci if (isInsert) { 10281cb0ef41Sopenharmony_ci this._writeOffset += byteLength; 10291cb0ef41Sopenharmony_ci } 10301cb0ef41Sopenharmony_ci else { 10311cb0ef41Sopenharmony_ci // If an offset was given, check to see if we wrote beyond the current writeOffset. 10321cb0ef41Sopenharmony_ci if (typeof arg3 === 'number') { 10331cb0ef41Sopenharmony_ci this._writeOffset = Math.max(this._writeOffset, offsetVal + byteLength); 10341cb0ef41Sopenharmony_ci } 10351cb0ef41Sopenharmony_ci else { 10361cb0ef41Sopenharmony_ci // If no offset was given, we wrote to the end of the SmartBuffer so increment writeOffset. 10371cb0ef41Sopenharmony_ci this._writeOffset += byteLength; 10381cb0ef41Sopenharmony_ci } 10391cb0ef41Sopenharmony_ci } 10401cb0ef41Sopenharmony_ci return this; 10411cb0ef41Sopenharmony_ci } 10421cb0ef41Sopenharmony_ci /** 10431cb0ef41Sopenharmony_ci * Handles writing or insert of a Buffer. 10441cb0ef41Sopenharmony_ci * 10451cb0ef41Sopenharmony_ci * @param value { Buffer } The Buffer to write. 10461cb0ef41Sopenharmony_ci * @param offset { Number } The offset to write the Buffer to. 10471cb0ef41Sopenharmony_ci */ 10481cb0ef41Sopenharmony_ci _handleBuffer(value, isInsert, offset) { 10491cb0ef41Sopenharmony_ci const offsetVal = typeof offset === 'number' ? offset : this._writeOffset; 10501cb0ef41Sopenharmony_ci // Ensure there is enough internal Buffer capacity. 10511cb0ef41Sopenharmony_ci if (isInsert) { 10521cb0ef41Sopenharmony_ci this.ensureInsertable(value.length, offsetVal); 10531cb0ef41Sopenharmony_ci } 10541cb0ef41Sopenharmony_ci else { 10551cb0ef41Sopenharmony_ci this._ensureWriteable(value.length, offsetVal); 10561cb0ef41Sopenharmony_ci } 10571cb0ef41Sopenharmony_ci // Write buffer value 10581cb0ef41Sopenharmony_ci value.copy(this._buff, offsetVal); 10591cb0ef41Sopenharmony_ci // Increment internal Buffer write offset; 10601cb0ef41Sopenharmony_ci if (isInsert) { 10611cb0ef41Sopenharmony_ci this._writeOffset += value.length; 10621cb0ef41Sopenharmony_ci } 10631cb0ef41Sopenharmony_ci else { 10641cb0ef41Sopenharmony_ci // If an offset was given, check to see if we wrote beyond the current writeOffset. 10651cb0ef41Sopenharmony_ci if (typeof offset === 'number') { 10661cb0ef41Sopenharmony_ci this._writeOffset = Math.max(this._writeOffset, offsetVal + value.length); 10671cb0ef41Sopenharmony_ci } 10681cb0ef41Sopenharmony_ci else { 10691cb0ef41Sopenharmony_ci // If no offset was given, we wrote to the end of the SmartBuffer so increment writeOffset. 10701cb0ef41Sopenharmony_ci this._writeOffset += value.length; 10711cb0ef41Sopenharmony_ci } 10721cb0ef41Sopenharmony_ci } 10731cb0ef41Sopenharmony_ci return this; 10741cb0ef41Sopenharmony_ci } 10751cb0ef41Sopenharmony_ci /** 10761cb0ef41Sopenharmony_ci * Ensures that the internal Buffer is large enough to read data. 10771cb0ef41Sopenharmony_ci * 10781cb0ef41Sopenharmony_ci * @param length { Number } The length of the data that needs to be read. 10791cb0ef41Sopenharmony_ci * @param offset { Number } The offset of the data that needs to be read. 10801cb0ef41Sopenharmony_ci */ 10811cb0ef41Sopenharmony_ci ensureReadable(length, offset) { 10821cb0ef41Sopenharmony_ci // Offset value defaults to managed read offset. 10831cb0ef41Sopenharmony_ci let offsetVal = this._readOffset; 10841cb0ef41Sopenharmony_ci // If an offset was provided, use it. 10851cb0ef41Sopenharmony_ci if (typeof offset !== 'undefined') { 10861cb0ef41Sopenharmony_ci // Checks for valid numberic value; 10871cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 10881cb0ef41Sopenharmony_ci // Overide with custom offset. 10891cb0ef41Sopenharmony_ci offsetVal = offset; 10901cb0ef41Sopenharmony_ci } 10911cb0ef41Sopenharmony_ci // Checks if offset is below zero, or the offset+length offset is beyond the total length of the managed data. 10921cb0ef41Sopenharmony_ci if (offsetVal < 0 || offsetVal + length > this.length) { 10931cb0ef41Sopenharmony_ci throw new Error(utils_1.ERRORS.INVALID_READ_BEYOND_BOUNDS); 10941cb0ef41Sopenharmony_ci } 10951cb0ef41Sopenharmony_ci } 10961cb0ef41Sopenharmony_ci /** 10971cb0ef41Sopenharmony_ci * Ensures that the internal Buffer is large enough to insert data. 10981cb0ef41Sopenharmony_ci * 10991cb0ef41Sopenharmony_ci * @param dataLength { Number } The length of the data that needs to be written. 11001cb0ef41Sopenharmony_ci * @param offset { Number } The offset of the data to be written. 11011cb0ef41Sopenharmony_ci */ 11021cb0ef41Sopenharmony_ci ensureInsertable(dataLength, offset) { 11031cb0ef41Sopenharmony_ci // Checks for valid numberic value; 11041cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 11051cb0ef41Sopenharmony_ci // Ensure there is enough internal Buffer capacity. 11061cb0ef41Sopenharmony_ci this._ensureCapacity(this.length + dataLength); 11071cb0ef41Sopenharmony_ci // If an offset was provided and its not the very end of the buffer, copy data into appropriate location in regards to the offset. 11081cb0ef41Sopenharmony_ci if (offset < this.length) { 11091cb0ef41Sopenharmony_ci this._buff.copy(this._buff, offset + dataLength, offset, this._buff.length); 11101cb0ef41Sopenharmony_ci } 11111cb0ef41Sopenharmony_ci // Adjust tracked smart buffer length 11121cb0ef41Sopenharmony_ci if (offset + dataLength > this.length) { 11131cb0ef41Sopenharmony_ci this.length = offset + dataLength; 11141cb0ef41Sopenharmony_ci } 11151cb0ef41Sopenharmony_ci else { 11161cb0ef41Sopenharmony_ci this.length += dataLength; 11171cb0ef41Sopenharmony_ci } 11181cb0ef41Sopenharmony_ci } 11191cb0ef41Sopenharmony_ci /** 11201cb0ef41Sopenharmony_ci * Ensures that the internal Buffer is large enough to write data. 11211cb0ef41Sopenharmony_ci * 11221cb0ef41Sopenharmony_ci * @param dataLength { Number } The length of the data that needs to be written. 11231cb0ef41Sopenharmony_ci * @param offset { Number } The offset of the data to be written (defaults to writeOffset). 11241cb0ef41Sopenharmony_ci */ 11251cb0ef41Sopenharmony_ci _ensureWriteable(dataLength, offset) { 11261cb0ef41Sopenharmony_ci const offsetVal = typeof offset === 'number' ? offset : this._writeOffset; 11271cb0ef41Sopenharmony_ci // Ensure enough capacity to write data. 11281cb0ef41Sopenharmony_ci this._ensureCapacity(offsetVal + dataLength); 11291cb0ef41Sopenharmony_ci // Adjust SmartBuffer length (if offset + length is larger than managed length, adjust length) 11301cb0ef41Sopenharmony_ci if (offsetVal + dataLength > this.length) { 11311cb0ef41Sopenharmony_ci this.length = offsetVal + dataLength; 11321cb0ef41Sopenharmony_ci } 11331cb0ef41Sopenharmony_ci } 11341cb0ef41Sopenharmony_ci /** 11351cb0ef41Sopenharmony_ci * Ensures that the internal Buffer is large enough to write at least the given amount of data. 11361cb0ef41Sopenharmony_ci * 11371cb0ef41Sopenharmony_ci * @param minLength { Number } The minimum length of the data needs to be written. 11381cb0ef41Sopenharmony_ci */ 11391cb0ef41Sopenharmony_ci _ensureCapacity(minLength) { 11401cb0ef41Sopenharmony_ci const oldLength = this._buff.length; 11411cb0ef41Sopenharmony_ci if (minLength > oldLength) { 11421cb0ef41Sopenharmony_ci let data = this._buff; 11431cb0ef41Sopenharmony_ci let newLength = (oldLength * 3) / 2 + 1; 11441cb0ef41Sopenharmony_ci if (newLength < minLength) { 11451cb0ef41Sopenharmony_ci newLength = minLength; 11461cb0ef41Sopenharmony_ci } 11471cb0ef41Sopenharmony_ci this._buff = Buffer.allocUnsafe(newLength); 11481cb0ef41Sopenharmony_ci data.copy(this._buff, 0, 0, oldLength); 11491cb0ef41Sopenharmony_ci } 11501cb0ef41Sopenharmony_ci } 11511cb0ef41Sopenharmony_ci /** 11521cb0ef41Sopenharmony_ci * Reads a numeric number value using the provided function. 11531cb0ef41Sopenharmony_ci * 11541cb0ef41Sopenharmony_ci * @typeparam T { number | bigint } The type of the value to be read 11551cb0ef41Sopenharmony_ci * 11561cb0ef41Sopenharmony_ci * @param func { Function(offset: number) => number } The function to read data on the internal Buffer with. 11571cb0ef41Sopenharmony_ci * @param byteSize { Number } The number of bytes read. 11581cb0ef41Sopenharmony_ci * @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead. 11591cb0ef41Sopenharmony_ci * 11601cb0ef41Sopenharmony_ci * @returns { T } the number value 11611cb0ef41Sopenharmony_ci */ 11621cb0ef41Sopenharmony_ci _readNumberValue(func, byteSize, offset) { 11631cb0ef41Sopenharmony_ci this.ensureReadable(byteSize, offset); 11641cb0ef41Sopenharmony_ci // Call Buffer.readXXXX(); 11651cb0ef41Sopenharmony_ci const value = func.call(this._buff, typeof offset === 'number' ? offset : this._readOffset); 11661cb0ef41Sopenharmony_ci // Adjust internal read offset if an optional read offset was not provided. 11671cb0ef41Sopenharmony_ci if (typeof offset === 'undefined') { 11681cb0ef41Sopenharmony_ci this._readOffset += byteSize; 11691cb0ef41Sopenharmony_ci } 11701cb0ef41Sopenharmony_ci return value; 11711cb0ef41Sopenharmony_ci } 11721cb0ef41Sopenharmony_ci /** 11731cb0ef41Sopenharmony_ci * Inserts a numeric number value based on the given offset and value. 11741cb0ef41Sopenharmony_ci * 11751cb0ef41Sopenharmony_ci * @typeparam T { number | bigint } The type of the value to be written 11761cb0ef41Sopenharmony_ci * 11771cb0ef41Sopenharmony_ci * @param func { Function(offset: T, offset?) => number} The function to write data on the internal Buffer with. 11781cb0ef41Sopenharmony_ci * @param byteSize { Number } The number of bytes written. 11791cb0ef41Sopenharmony_ci * @param value { T } The number value to write. 11801cb0ef41Sopenharmony_ci * @param offset { Number } the offset to write the number at (REQUIRED). 11811cb0ef41Sopenharmony_ci * 11821cb0ef41Sopenharmony_ci * @returns SmartBuffer this buffer 11831cb0ef41Sopenharmony_ci */ 11841cb0ef41Sopenharmony_ci _insertNumberValue(func, byteSize, value, offset) { 11851cb0ef41Sopenharmony_ci // Check for invalid offset values. 11861cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 11871cb0ef41Sopenharmony_ci // Ensure there is enough internal Buffer capacity. (raw offset is passed) 11881cb0ef41Sopenharmony_ci this.ensureInsertable(byteSize, offset); 11891cb0ef41Sopenharmony_ci // Call buffer.writeXXXX(); 11901cb0ef41Sopenharmony_ci func.call(this._buff, value, offset); 11911cb0ef41Sopenharmony_ci // Adjusts internally managed write offset. 11921cb0ef41Sopenharmony_ci this._writeOffset += byteSize; 11931cb0ef41Sopenharmony_ci return this; 11941cb0ef41Sopenharmony_ci } 11951cb0ef41Sopenharmony_ci /** 11961cb0ef41Sopenharmony_ci * Writes a numeric number value based on the given offset and value. 11971cb0ef41Sopenharmony_ci * 11981cb0ef41Sopenharmony_ci * @typeparam T { number | bigint } The type of the value to be written 11991cb0ef41Sopenharmony_ci * 12001cb0ef41Sopenharmony_ci * @param func { Function(offset: T, offset?) => number} The function to write data on the internal Buffer with. 12011cb0ef41Sopenharmony_ci * @param byteSize { Number } The number of bytes written. 12021cb0ef41Sopenharmony_ci * @param value { T } The number value to write. 12031cb0ef41Sopenharmony_ci * @param offset { Number } the offset to write the number at (REQUIRED). 12041cb0ef41Sopenharmony_ci * 12051cb0ef41Sopenharmony_ci * @returns SmartBuffer this buffer 12061cb0ef41Sopenharmony_ci */ 12071cb0ef41Sopenharmony_ci _writeNumberValue(func, byteSize, value, offset) { 12081cb0ef41Sopenharmony_ci // If an offset was provided, validate it. 12091cb0ef41Sopenharmony_ci if (typeof offset === 'number') { 12101cb0ef41Sopenharmony_ci // Check if we're writing beyond the bounds of the managed data. 12111cb0ef41Sopenharmony_ci if (offset < 0) { 12121cb0ef41Sopenharmony_ci throw new Error(utils_1.ERRORS.INVALID_WRITE_BEYOND_BOUNDS); 12131cb0ef41Sopenharmony_ci } 12141cb0ef41Sopenharmony_ci utils_1.checkOffsetValue(offset); 12151cb0ef41Sopenharmony_ci } 12161cb0ef41Sopenharmony_ci // Default to writeOffset if no offset value was given. 12171cb0ef41Sopenharmony_ci const offsetVal = typeof offset === 'number' ? offset : this._writeOffset; 12181cb0ef41Sopenharmony_ci // Ensure there is enough internal Buffer capacity. (raw offset is passed) 12191cb0ef41Sopenharmony_ci this._ensureWriteable(byteSize, offsetVal); 12201cb0ef41Sopenharmony_ci func.call(this._buff, value, offsetVal); 12211cb0ef41Sopenharmony_ci // If an offset was given, check to see if we wrote beyond the current writeOffset. 12221cb0ef41Sopenharmony_ci if (typeof offset === 'number') { 12231cb0ef41Sopenharmony_ci this._writeOffset = Math.max(this._writeOffset, offsetVal + byteSize); 12241cb0ef41Sopenharmony_ci } 12251cb0ef41Sopenharmony_ci else { 12261cb0ef41Sopenharmony_ci // If no numeric offset was given, we wrote to the end of the SmartBuffer so increment writeOffset. 12271cb0ef41Sopenharmony_ci this._writeOffset += byteSize; 12281cb0ef41Sopenharmony_ci } 12291cb0ef41Sopenharmony_ci return this; 12301cb0ef41Sopenharmony_ci } 12311cb0ef41Sopenharmony_ci} 12321cb0ef41Sopenharmony_ciexports.SmartBuffer = SmartBuffer; 12331cb0ef41Sopenharmony_ci//# sourceMappingURL=smartbuffer.js.map