StreamingReader.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. peak() {
  38. this.assertSize(1);
  39. return this.view.getUint8(this.x);
  40. }
  41. skip(length) {
  42. this.assertSize(length);
  43. this.x += length;
  44. }
  45. buf(size) {
  46. this.assertSize(size);
  47. const end = this.x + size;
  48. const bin = this.uint8.subarray(this.x, end);
  49. this.x = end;
  50. return bin;
  51. }
  52. u8() {
  53. this.assertSize(1);
  54. return this.view.getUint8(this.x++);
  55. }
  56. i8() {
  57. this.assertSize(1);
  58. return this.view.getInt8(this.x++);
  59. }
  60. u16() {
  61. this.assertSize(2);
  62. const num = this.view.getUint16(this.x);
  63. this.x += 2;
  64. return num;
  65. }
  66. i16() {
  67. this.assertSize(2);
  68. const num = this.view.getInt16(this.x);
  69. this.x += 2;
  70. return num;
  71. }
  72. u32() {
  73. this.assertSize(4);
  74. const num = this.view.getUint32(this.x);
  75. this.x += 4;
  76. return num;
  77. }
  78. i32() {
  79. this.assertSize(4);
  80. const num = this.view.getInt32(this.x);
  81. this.x += 4;
  82. return num;
  83. }
  84. u64() {
  85. this.assertSize(8);
  86. const num = this.view.getBigUint64(this.x);
  87. this.x += 8;
  88. return num;
  89. }
  90. i64() {
  91. this.assertSize(8);
  92. const num = this.view.getBigInt64(this.x);
  93. this.x += 8;
  94. return num;
  95. }
  96. f32() {
  97. this.assertSize(4);
  98. const pos = this.x;
  99. this.x += 4;
  100. return this.view.getFloat32(pos);
  101. }
  102. f64() {
  103. this.assertSize(8);
  104. const pos = this.x;
  105. this.x += 8;
  106. return this.view.getFloat64(pos);
  107. }
  108. utf8(size) {
  109. this.assertSize(size);
  110. const start = this.x;
  111. this.x += size;
  112. return (0, decodeUtf8_1.decodeUtf8)(this.uint8, start, size);
  113. }
  114. ascii(length) {
  115. this.assertSize(length);
  116. const uint8 = this.uint8;
  117. let str = '';
  118. const end = this.x + length;
  119. for (let i = this.x; i < end; i++)
  120. str += String.fromCharCode(uint8[i]);
  121. this.x = end;
  122. return str;
  123. }
  124. reset(uint8) {
  125. this.dx = 0;
  126. this.writer.reset();
  127. this.push(uint8);
  128. }
  129. }
  130. exports.StreamingReader = StreamingReader;
  131. //# sourceMappingURL=StreamingReader.js.map