buffer-reader.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. string(length) {
  32. const result = this.buffer.toString(this.encoding, this.offset, this.offset + length);
  33. this.offset += length;
  34. return result;
  35. }
  36. cstring() {
  37. const start = this.offset;
  38. let end = start;
  39. while (this.buffer[end++] !== 0) { }
  40. this.offset = end;
  41. return this.buffer.toString(this.encoding, start, end - 1);
  42. }
  43. bytes(length) {
  44. const result = this.buffer.slice(this.offset, this.offset + length);
  45. this.offset += length;
  46. return result;
  47. }
  48. }
  49. exports.BufferReader = BufferReader;
  50. //# sourceMappingURL=buffer-reader.js.map