"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BufferHelper = void 0;

var _buffer = require("buffer");

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class BufferHelper {
  constructor() {
    _defineProperty(this, "buffers", void 0);

    _defineProperty(this, "size", void 0);

    this.buffers = [];
    this.size = 0;
  }

  get length() {
    return this.size;
  }

  concat(buffer) {
    this.buffers.push(buffer);
    this.size += buffer.length;
    return this;
  }

  empty() {
    this.buffers = [];
    this.size = 0;
    return this;
  }

  toBuffer() {
    return _buffer.Buffer.concat(this.buffers, this.size);
  }

  toString(encoding) {
    return this.toBuffer().toString(encoding);
  }

  load(stream, callback) {
    stream.on('data', trunk => {
      this.concat(trunk);
    });
    stream.on('end', () => {
      callback(null, this.toBuffer());
    });
    stream.once('error', callback);
  }

  bytesToString(data, type) {
    this.concat(_buffer.Buffer.from(data));
    const buffer = this.toBuffer();
    return buffer.toString(type);
  }

}

exports.BufferHelper = BufferHelper;
//# sourceMappingURL=BufferHelper.js.map