Reader.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. peek() {
  17. return this.view.getUint8(this.x);
  18. }
  19. peak() {
  20. return this.peek();
  21. }
  22. skip(length) {
  23. this.x += length;
  24. }
  25. buf(size) {
  26. const end = this.x + size;
  27. const bin = this.uint8.subarray(this.x, end);
  28. this.x = end;
  29. return bin;
  30. }
  31. u8() {
  32. return this.uint8[this.x++];
  33. }
  34. i8() {
  35. return this.view.getInt8(this.x++);
  36. }
  37. u16() {
  38. let x = this.x;
  39. const num = (this.uint8[x++] << 8) + this.uint8[x++];
  40. this.x = x;
  41. return num;
  42. }
  43. i16() {
  44. const num = this.view.getInt16(this.x);
  45. this.x += 2;
  46. return num;
  47. }
  48. u32() {
  49. const num = this.view.getUint32(this.x);
  50. this.x += 4;
  51. return num;
  52. }
  53. i32() {
  54. const num = this.view.getInt32(this.x);
  55. this.x += 4;
  56. return num;
  57. }
  58. u64() {
  59. const num = this.view.getBigUint64(this.x);
  60. this.x += 8;
  61. return num;
  62. }
  63. i64() {
  64. const num = this.view.getBigInt64(this.x);
  65. this.x += 8;
  66. return num;
  67. }
  68. f32() {
  69. const pos = this.x;
  70. this.x += 4;
  71. return this.view.getFloat32(pos);
  72. }
  73. f64() {
  74. const pos = this.x;
  75. this.x += 8;
  76. return this.view.getFloat64(pos);
  77. }
  78. utf8(size) {
  79. const start = this.x;
  80. this.x += size;
  81. return (0, decodeUtf8_1.decodeUtf8)(this.uint8, start, size);
  82. }
  83. ascii(length) {
  84. const uint8 = this.uint8;
  85. let str = '';
  86. const end = this.x + length;
  87. for (let i = this.x; i < end; i++)
  88. str += String.fromCharCode(uint8[i]);
  89. this.x = end;
  90. return str;
  91. }
  92. }
  93. exports.Reader = Reader;
  94. //# sourceMappingURL=Reader.js.map