You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buffer-helper.js 947B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const Buffer = require('buffer').Buffer
  2. var BufferHelper = function () {
  3. this.buffers = [];
  4. this.size = 0;
  5. Object.defineProperty(this, 'length', {
  6. get: function () {
  7. return this.size;
  8. }
  9. });
  10. };
  11. BufferHelper.prototype.concat = function (buffer) {
  12. this.buffers.push(buffer);
  13. this.size += buffer.length;
  14. return this;
  15. };
  16. BufferHelper.prototype.empty = function () {
  17. this.buffers = [];
  18. this.size = 0;
  19. return this;
  20. };
  21. BufferHelper.prototype.toBuffer = function () {
  22. return Buffer.concat(this.buffers, this.size);
  23. };
  24. BufferHelper.prototype.toString = function (encoding) {
  25. return this.toBuffer().toString(encoding);
  26. };
  27. BufferHelper.prototype.load = function (stream, callback) {
  28. var that = this;
  29. stream.on('data', function (trunk) {
  30. that.concat(trunk);
  31. });
  32. stream.on('end', function () {
  33. callback(null, that.toBuffer());
  34. });
  35. stream.once('error', callback);
  36. };
  37. module.exports = BufferHelper;