packet-node-iconv.js 920 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const Packet = require('./packet');
  3. const Iconv = require('iconv-lite');
  4. class PacketIconvEncoded extends Packet {
  5. constructor(buf, pos, end, encoding) {
  6. super(buf, pos, end);
  7. this.encoding = encoding;
  8. }
  9. readStringLength() {
  10. const len = this.readUnsignedLength();
  11. if (len === null) return null;
  12. this.pos += len;
  13. return Iconv.decode(this.buf.slice(this.pos - len, this.pos), this.encoding);
  14. }
  15. readString(beg, len) {
  16. return Iconv.decode(this.buf.slice(beg, beg + len), this.encoding);
  17. }
  18. subPacketLengthEncoded() {
  19. const len = this.readUnsignedLength();
  20. this.skip(len);
  21. return new PacketIconvEncoded(this.buf, this.pos - len, this.pos, this.encoding);
  22. }
  23. readStringRemaining() {
  24. const str = Iconv.decode(this.buf.slice(this.pos, this.end), this.encoding);
  25. this.pos = this.end;
  26. return str;
  27. }
  28. }
  29. module.exports = PacketIconvEncoded;