Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BufferList.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. var Buffer = require('safe-buffer').Buffer;
  4. var util = require('util');
  5. function copyBuffer(src, target, offset) {
  6. src.copy(target, offset);
  7. }
  8. module.exports = function () {
  9. function BufferList() {
  10. _classCallCheck(this, BufferList);
  11. this.head = null;
  12. this.tail = null;
  13. this.length = 0;
  14. }
  15. BufferList.prototype.push = function push(v) {
  16. var entry = { data: v, next: null };
  17. if (this.length > 0) this.tail.next = entry;else this.head = entry;
  18. this.tail = entry;
  19. ++this.length;
  20. };
  21. BufferList.prototype.unshift = function unshift(v) {
  22. var entry = { data: v, next: this.head };
  23. if (this.length === 0) this.tail = entry;
  24. this.head = entry;
  25. ++this.length;
  26. };
  27. BufferList.prototype.shift = function shift() {
  28. if (this.length === 0) return;
  29. var ret = this.head.data;
  30. if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
  31. --this.length;
  32. return ret;
  33. };
  34. BufferList.prototype.clear = function clear() {
  35. this.head = this.tail = null;
  36. this.length = 0;
  37. };
  38. BufferList.prototype.join = function join(s) {
  39. if (this.length === 0) return '';
  40. var p = this.head;
  41. var ret = '' + p.data;
  42. while (p = p.next) {
  43. ret += s + p.data;
  44. }return ret;
  45. };
  46. BufferList.prototype.concat = function concat(n) {
  47. if (this.length === 0) return Buffer.alloc(0);
  48. if (this.length === 1) return this.head.data;
  49. var ret = Buffer.allocUnsafe(n >>> 0);
  50. var p = this.head;
  51. var i = 0;
  52. while (p) {
  53. copyBuffer(p.data, ret, i);
  54. i += p.data.length;
  55. p = p.next;
  56. }
  57. return ret;
  58. };
  59. return BufferList;
  60. }();
  61. if (util && util.inspect && util.inspect.custom) {
  62. module.exports.prototype[util.inspect.custom] = function () {
  63. var obj = util.inspect({ length: this.length });
  64. return this.constructor.name + ' ' + obj;
  65. };
  66. }