Reader.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Reader = void 0;
  4. const decodeUtf8_1 = require("./utf8/decodeUtf8");
  5. class Reader {
  6. constructor() {
  7. this.uint8 = new Uint8Array([]);
  8. this.view = new DataView(this.uint8.buffer);
  9. this.x = 0;
  10. }
  11. reset(uint8) {
  12. this.x = 0;
  13. this.uint8 = uint8;
  14. this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length);
  15. }
  16. peak() {
  17. return this.view.getUint8(this.x);
  18. }
  19. skip(length) {
  20. this.x += length;
  21. }
  22. buf(size) {
  23. const end = this.x + size;
  24. const bin = this.uint8.subarray(this.x, end);
  25. this.x = end;
  26. return bin;
  27. }
  28. u8() {
  29. return this.uint8[this.x++];
  30. }
  31. i8() {
  32. return this.view.getInt8(this.x++);
  33. }
  34. u16() {
  35. let x = this.x;
  36. const num = (this.uint8[x++] << 8) + this.uint8[x++];
  37. this.x = x;
  38. return num;
  39. }
  40. i16() {
  41. const num = this.view.getInt16(this.x);
  42. this.x += 2;
  43. return num;
  44. }
  45. u32() {
  46. const num = this.view.getUint32(this.x);
  47. this.x += 4;
  48. return num;
  49. }
  50. i32() {
  51. const num = this.view.getInt32(this.x);
  52. this.x += 4;
  53. return num;
  54. }
  55. u64() {
  56. const num = this.view.getBigUint64(this.x);
  57. this.x += 8;
  58. return num;
  59. }
  60. i64() {
  61. const num = this.view.getBigInt64(this.x);
  62. this.x += 8;
  63. return num;
  64. }
  65. f32() {
  66. const pos = this.x;
  67. this.x += 4;
  68. return this.view.getFloat32(pos);
  69. }
  70. f64() {
  71. const pos = this.x;
  72. this.x += 8;
  73. return this.view.getFloat64(pos);
  74. }
  75. utf8(size) {
  76. const start = this.x;
  77. this.x += size;
  78. return (0, decodeUtf8_1.decodeUtf8)(this.uint8, start, size);
  79. }
  80. ascii(length) {
  81. const uint8 = this.uint8;
  82. let str = '';
  83. const end = this.x + length;
  84. for (let i = this.x; i < end; i++)
  85. str += String.fromCharCode(uint8[i]);
  86. this.x = end;
  87. return str;
  88. }
  89. }
  90. exports.Reader = Reader;
  91. //# sourceMappingURL=Reader.js.map