File: add-chunk.js

package info (click to toggle)
node-bufferjs 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 72 kB
  • ctags: 6
  • sloc: makefile: 2; sh: 2
file content (30 lines) | stat: -rw-r--r-- 819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true eqeqeq:true immed:true latedef:true*/
(function () {
  "use strict";

  Buffer.prototype.__addchunk_index = 0;

  Buffer.prototype.addChunk = function (chunk) {
    var  len = Math.min(chunk.length, this.length - this.__addchunk_index);

    if (this.__addchunk_index === this.length) {
      //throw new Error("Buffer is full");
      return false;
    }

    chunk.copy(this, this.__addchunk_index, 0, len);

    this.__addchunk_index += len;

    if (len < chunk.length) {
      //remnant = new Buffer(chunk.length - len);
      //chunk.copy(remnant, 0, len, chunk.length);
      // return remnant;
      return chunk.slice(len, chunk.length);
    }

    if (this.__addchunk_index === this.length) {
      return true;
    }
  };
}());