StreamingReader.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StreamingReader = void 0;
  4. const Writer_1 = require("./Writer");
  5. const decodeUtf8_1 = require("./utf8/decodeUtf8");
  6. class StreamingReader {
  7. constructor(allocSize = 16 * 1024) {
  8. this.dx = 0;
  9. this.writer = new Writer_1.Writer(allocSize);
  10. }
  11. size() {
  12. return this.writer.x - this.x;
  13. }
  14. assertSize(size) {
  15. if (size > this.size())
  16. throw new RangeError('OUT_OF_BOUNDS');
  17. }
  18. push(uint8) {
  19. this.writer.buf(uint8, uint8.length);
  20. }
  21. consume() {
  22. this.writer.x0 += this.dx;
  23. this.dx = 0;
  24. }
  25. get uint8() {
  26. return this.writer.uint8;
  27. }
  28. get view() {
  29. return this.writer.view;
  30. }
  31. get x() {
  32. return this.writer.x0 + this.dx;
  33. }
  34. set x(x) {
  35. this.dx = x - this.writer.x0;
  36. }
  37. peek() {
  38. this.assertSize(1);
  39. return this.view.getUint8(this.x);
  40. }
  41. peak() {
  42. return this.peek();
  43. }
  44. skip(length) {
  45. this.assertSize(length);
  46. this.x += length;
  47. }
  48. buf(size) {
  49. this.assertSize(size);
  50. const end = this.x + size;
  51. const bin = this.uint8.subarray(this.x, end);
  52. this.x = end;
  53. return bin;
  54. }
  55. u8() {
  56. this.assertSize(1);
  57. return this.view.getUint8(this.x++);
  58. }
  59. i8() {
  60. this.assertSize(1);
  61. return this.view.getInt8(this.x++);
  62. }
  63. u16() {
  64. this.assertSize(2);
  65. const num = this.view.getUint16(this.x);
  66. this.x += 2;
  67. return num;
  68. }
  69. i16() {
  70. this.assertSize(2);
  71. const num = this.view.getInt16(this.x);
  72. this.x += 2;
  73. return num;
  74. }
  75. u32() {
  76. this.assertSize(4);
  77. const num = this.view.getUint32(this.x);
  78. this.x += 4;
  79. return num;
  80. }
  81. i32() {
  82. this.assertSize(4);
  83. const num = this.view.getInt32(this.x);
  84. this.x += 4;
  85. return num;
  86. }
  87. u64() {
  88. this.assertSize(8);
  89. const num = this.view.getBigUint64(this.x);
  90. this.x += 8;
  91. return num;
  92. }
  93. i64() {
  94. this.assertSize(8);
  95. const num = this.view.getBigInt64(this.x);
  96. this.x += 8;
  97. return num;
  98. }
  99. f32() {
  100. this.assertSize(4);
  101. const pos = this.x;
  102. this.x += 4;
  103. return this.view.getFloat32(pos);
  104. }
  105. f64() {
  106. this.assertSize(8);
  107. const pos = this.x;
  108. this.x += 8;
  109. return this.view.getFloat64(pos);
  110. }
  111. utf8(size) {
  112. this.assertSize(size);
  113. const start = this.x;
  114. this.x += size;
  115. return (0, decodeUtf8_1.decodeUtf8)(this.uint8, start, size);
  116. }
  117. ascii(length) {
  118. this.assertSize(length);
  119. const uint8 = this.uint8;
  120. let str = '';
  121. const end = this.x + length;
  122. for (let i = this.x; i < end; i++)
  123. str += String.fromCharCode(uint8[i]);
  124. this.x = end;
  125. return str;
  126. }
  127. reset(uint8) {
  128. this.dx = 0;
  129. this.writer.reset();
  130. this.push(uint8);
  131. }
  132. }
  133. exports.StreamingReader = StreamingReader;
  134. //# sourceMappingURL=StreamingReader.js.map