buffer-reader.js 1.6 KB

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