buffer-reader.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BufferReader = void 0;
  4. class BufferReader {
  5. constructor(offset = 0) {
  6. this.offset = offset;
  7. this.buffer = Buffer.allocUnsafe(0);
  8. // TODO(bmc): support non-utf8 encoding?
  9. this.encoding = 'utf-8';
  10. }
  11. setBuffer(offset, buffer) {
  12. this.offset = offset;
  13. this.buffer = buffer;
  14. }
  15. int16() {
  16. const result = this.buffer.readInt16BE(this.offset);
  17. this.offset += 2;
  18. return result;
  19. }
  20. byte() {
  21. const result = this.buffer[this.offset];
  22. this.offset++;
  23. return result;
  24. }
  25. int32() {
  26. const result = this.buffer.readInt32BE(this.offset);
  27. this.offset += 4;
  28. return result;
  29. }
  30. uint32() {
  31. const result = this.buffer.readUInt32BE(this.offset);
  32. this.offset += 4;
  33. return result;
  34. }
  35. string(length) {
  36. const result = this.buffer.toString(this.encoding, this.offset, this.offset + length);
  37. this.offset += length;
  38. return result;
  39. }
  40. cstring() {
  41. const start = this.offset;
  42. let end = start;
  43. // eslint-disable-next-line no-empty
  44. while (this.buffer[end++] !== 0) { }
  45. this.offset = end;
  46. return this.buffer.toString(this.encoding, start, end - 1);
  47. }
  48. bytes(length) {
  49. const result = this.buffer.slice(this.offset, this.offset + length);
  50. this.offset += length;
  51. return result;
  52. }
  53. }
  54. exports.BufferReader = BufferReader;
  55. //# sourceMappingURL=buffer-reader.js.map