UNPKG

@kikuchan/binary-reader

Version:
1 lines 3.13 kB
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=class{#e;#t;#n;#r={};constructor(e,t){ArrayBuffer.isView(e)?this.#e=new DataView(e.buffer,e.byteOffset,e.byteLength):this.#e=new DataView(e),this.#t=0,this.#n=t?.littleEndian}get position(){return this.#t}get size(){return this.#e.byteLength}get remain(){return this.size-this.position}eof(){return this.remain<=0}seek(e){if(typeof e==`string`){let t=this.#r[e];if(t===void 0)throw Error(`seek: no such bookmark`);e=t}return typeof e==`number`&&(this.#t=Math.min(Math.max(e,0),this.size)),this}bookmark(e){return this.#r[e]=this.#t,this}rewind(){return this.seek(0)}skip(e){return this.seek(this.#t+(e??1))}align(e){if(!(e>0))throw RangeError(`align(n): n must be > 0`);return this.position%e?this.skip(e-this.position%e):this}peekBytes(e){e===void 0&&(e=this.remain);let t=this.#e.byteOffset+this.position;return new Uint8Array(this.#e.buffer.slice(t,t+e))}readUint8(){let e=this.#e.getUint8(this.position);return this.skip(1),e}readUint16(e){let t=this.#e.getUint16(this.position,e??this.#n);return this.skip(2),t}readUint16le(){return this.readUint16(!0)}readUint16be(){return this.readUint16(!1)}readUint32(e){let t=this.#e.getUint32(this.position,e??this.#n);return this.skip(4),t}readUint32le(){return this.readUint32(!0)}readUint32be(){return this.readUint32(!1)}readUint64(e){let t=this.#e.getBigUint64(this.position,e??this.#n);return this.skip(8),t}readUint64le(){return this.readUint64(!0)}readUint64be(){return this.readUint64(!1)}readInt8(){let e=this.#e.getInt8(this.position);return this.skip(1),e}readInt16(e){let t=this.#e.getInt16(this.position,e??this.#n);return this.skip(2),t}readInt16le(){return this.readInt16(!0)}readInt16be(){return this.readInt16(!1)}readInt32(e){let t=this.#e.getInt32(this.position,e??this.#n);return this.skip(4),t}readInt32le(){return this.readInt32(!0)}readInt32be(){return this.readInt32(!1)}readInt64(e){let t=this.#e.getBigInt64(this.position,e??this.#n);return this.skip(8),t}readInt64le(){return this.readInt64(!0)}readInt64be(){return this.readInt64(!1)}readFloat16(e){let t=this.#e.getFloat16(this.position,e??this.#n);return this.skip(2),t}readFloat16le(){return this.readFloat16(!0)}readFloat16be(){return this.readFloat16(!1)}readFloat32(e){let t=this.#e.getFloat32(this.position,e??this.#n);return this.skip(4),t}readFloat32le(){return this.readFloat32(!0)}readFloat32be(){return this.readFloat32(!1)}readFloat64(e){let t=this.#e.getFloat64(this.position,e??this.#n);return this.skip(8),t}readFloat64le(){return this.readFloat64(!0)}readFloat64be(){return this.readFloat64(!1)}#i(){let e=this.remain;for(let t=0;t<e;t++)if(this.#e.getUint8(this.position+t)===0)return t}readBytes(e){let t=this.peekBytes(e);return t&&this.skip(t.byteLength),t}readString(e,t){let n=this.remain,r=0;if(e===void 0&&(e=this.#i(),r=1),e===void 0||n<e)return;let i=this.peekBytes(e);try{if(typeof t==`function`){let n=t(i);return this.skip(e+r),n}else{let n=new TextDecoder(t??`utf-8`,{fatal:!0}).decode(i);return this.skip(e+r),n}}catch{return}}};exports.BinaryReader=e,exports.default=e;