@yagisumi/simple-buffer-reader
Version:
simple buffer reader
3 lines (2 loc) • 6.62 kB
JavaScript
!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t=t||self).window=t.window||{})}(this,function(t){"use strict";t.SimpleBufferReader=class{constructor(t,s=!0){this.pos=0,this.limit=-1,this.buf=t,this.view=new DataView(t),this.littleEndian=s}stringify(t){const s=[];for(let e in t)s.push(`${e}: ${t[e]}`);return`{ ${s.join(", ")} }`}setLimit(t){if(t<0||this.buf.byteLength<t)throw new RangeError(`Position out of range of buffer. setLimit: ${this.stringify({limit:t,bufferLength:this.buf.byteLength})}`);return this.limit=t,this}resetLimit(){return this.limit=-1,this}getLimit(){return this.limit>=0?this.limit:null}getPos(){return this.pos}seek(t){if(t<0||this.buf.byteLength<t)throw new RangeError(`Position out of range of buffer. seek: ${this.stringify({pos:t,bufferLength:this.buf.byteLength})}`);return this.pos=t,this}skip(t){return this.checkPos(this.pos,t,"skip"),this.pos+=t,this}checkPos(t,s,e){if(this.limit>=0&&t+s>this.limit)throw new RangeError(`Position exceeds limit. ${e}: ${this.stringify({pos:t,limit:this.limit,readByte:s})}`);if(t+s>this.buf.byteLength)throw new RangeError(`Position exceeds buffer length. ${e}: ${this.stringify({pos:t,bufferLength:this.buf.byteLength,readByte:s})}`)}readString(t){this.checkPos(this.pos,t,"readString");const s=[];for(let e=0;e<t;e++)s.push(this.view.getUint8(this.pos+e));const e=String.fromCharCode(...s);return this.pos+=t,e}peekString(t,s){const e=null==s?this.pos:s;this.checkPos(e,t,"peekString");const i=[];for(let s=0;s<t;s++)i.push(this.view.getUint8(e+s));return String.fromCharCode(...i)}readBuffer(t){this.checkPos(this.pos,t,"readBuffer");const s=this.buf.slice(this.pos,this.pos+t);return this.pos+=t,s}peekBuffer(t,s){const e=null==s?this.pos:s;return this.checkPos(e,t,"peekBuffer"),this.buf.slice(e,e+t)}readInt8(){this.checkPos(this.pos,1,"readInt8");const t=this.view.getInt8(this.pos);return this.pos+=1,t}peekInt8(t){const s=null==t?this.pos:t;return this.checkPos(s,1,"peekInt8"),this.view.getInt8(s)}readUint8(){this.checkPos(this.pos,1,"readUint8");const t=this.view.getUint8(this.pos);return this.pos+=1,t}peekUint8(t){const s=null==t?this.pos:t;return this.checkPos(s,1,"peekUint8"),this.view.getUint8(s)}readInt16(){this.checkPos(this.pos,2,"readInt16");const t=this.view.getInt16(this.pos,this.littleEndian);return this.pos+=2,t}peekInt16(t){const s=null==t?this.pos:t;return this.checkPos(s,2,"peekInt16"),this.view.getInt16(s,this.littleEndian)}readInt16LE(){this.checkPos(this.pos,2,"readInt16LE");const t=this.view.getInt16(this.pos,!0);return this.pos+=2,t}peekInt16LE(t){const s=null==t?this.pos:t;return this.checkPos(s,2,"peekInt16LE"),this.view.getInt16(s,!0)}readInt16BE(){this.checkPos(this.pos,2,"readInt16BE");const t=this.view.getInt16(this.pos,!1);return this.pos+=2,t}peekInt16BE(t){const s=null==t?this.pos:t;return this.checkPos(s,2,"peekInt16BE"),this.view.getInt16(s,!1)}readUint16(){this.checkPos(this.pos,2,"readUint16");const t=this.view.getUint16(this.pos,this.littleEndian);return this.pos+=2,t}peekUint16(t){const s=null==t?this.pos:t;return this.checkPos(s,2,"peekUint16"),this.view.getUint16(s,this.littleEndian)}readUint16LE(){this.checkPos(this.pos,2,"readUint16LE");const t=this.view.getUint16(this.pos,!0);return this.pos+=2,t}peekUint16LE(t){const s=null==t?this.pos:t;return this.checkPos(s,2,"peekUint16LE"),this.view.getUint16(s,!0)}readUint16BE(){this.checkPos(this.pos,2,"readUint16BE");const t=this.view.getUint16(this.pos,!1);return this.pos+=2,t}peekUint16BE(t){const s=null==t?this.pos:t;return this.checkPos(s,2,"peekUint16BE"),this.view.getUint16(s,!1)}readInt32(){this.checkPos(this.pos,4,"readInt32");const t=this.view.getInt32(this.pos,this.littleEndian);return this.pos+=4,t}peekInt32(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekInt32"),this.view.getInt32(s,this.littleEndian)}readInt32LE(){this.checkPos(this.pos,4,"readInt32LE");const t=this.view.getInt32(this.pos,!0);return this.pos+=4,t}peekInt32LE(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekInt32LE"),this.view.getInt32(s,!0)}readInt32BE(){this.checkPos(this.pos,4,"readInt32BE");const t=this.view.getInt32(this.pos,!1);return this.pos+=4,t}peekInt32BE(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekInt32BE"),this.view.getInt32(s,!1)}readUint32(){this.checkPos(this.pos,4,"readUint32");const t=this.view.getUint32(this.pos,this.littleEndian);return this.pos+=4,t}peekUint32(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekUint32"),this.view.getUint32(s,this.littleEndian)}readUint32LE(){this.checkPos(this.pos,4,"readUint32LE");const t=this.view.getUint32(this.pos,!0);return this.pos+=4,t}peekUint32LE(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekUint32LE"),this.view.getUint32(s,!0)}readUint32BE(){this.checkPos(this.pos,4,"readUint32BE");const t=this.view.getUint32(this.pos,!1);return this.pos+=4,t}peekUint32BE(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekUint32BE"),this.view.getUint32(s,!1)}readFloat32(){this.checkPos(this.pos,4,"readFloat32");const t=this.view.getFloat32(this.pos,this.littleEndian);return this.pos+=4,t}peekFloat32(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekFloat32"),this.view.getFloat32(s,this.littleEndian)}readFloat32LE(){this.checkPos(this.pos,4,"readFloat32LE");const t=this.view.getFloat32(this.pos,!0);return this.pos+=4,t}peekFloat32LE(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekFloat32LE"),this.view.getFloat32(s,!0)}readFloat32BE(){this.checkPos(this.pos,4,"readFloat32BE");const t=this.view.getFloat32(this.pos,!1);return this.pos+=4,t}peekFloat32BE(t){const s=null==t?this.pos:t;return this.checkPos(s,4,"peekFloat32BE"),this.view.getFloat32(s,!1)}readFloat64(){this.checkPos(this.pos,8,"readFloat64");const t=this.view.getFloat64(this.pos,this.littleEndian);return this.pos+=8,t}peekFloat64(t){const s=null==t?this.pos:t;return this.checkPos(s,8,"peekFloat64"),this.view.getFloat64(s,this.littleEndian)}readFloat64LE(){this.checkPos(this.pos,8,"readFloat64LE");const t=this.view.getFloat64(this.pos,!0);return this.pos+=8,t}peekFloat64LE(t){const s=null==t?this.pos:t;return this.checkPos(s,8,"peekFloat64LE"),this.view.getFloat64(s,!0)}readFloat64BE(){this.checkPos(this.pos,8,"readFloat64BE");const t=this.view.getFloat64(this.pos,!1);return this.pos+=8,t}peekFloat64BE(t){const s=null==t?this.pos:t;return this.checkPos(s,8,"peekFloat64BE"),this.view.getFloat64(s,!1)}},Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=simple-buffer-reader.js.map