How to use the buffer.Buffer.byteLength function in buffer

To help you get started, we’ve selected a few buffer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bnoguchi / redis-node / test / string_commands.vows.js View on Github external
'should return the entire image': function (err, value) {
                var paths = [ "sample.png", "test/sample.png" ],
                    path = paths.shift();
                while (true) {
                  try {
                    var fileContents = fs.readFileSync(path, 'binary');
                    break;
                  } catch (e) {
                    path = paths.shift();
                  }
                }
                var imageBuffer = new Buffer(Buffer.byteLength(fileContents, 'binary'));
                imageBuffer.write(fileContents, 0, "binary");

                // TODO Make value binary a Buffer vs the current binary string?
                assert.equal(value, imageBuffer.toString("binary"));
            }
        }
github Azure / azure-relay-node / hyco-https / lib / _hyco_outgoing.js View on Github external
if (conn && conn.readyState === WS.OPEN) {
    if (this.output.length) {
      this._flushOutput(conn);
    }
    return conn.send(data, {binary : false, compress : false }, function ack(error) {
      if ( error ) {
        console.log(error);
      }
    });
  }

  // Buffer, as long as we're not destroyed.
  this.output.push(data);
  this.outputFrameBinary.push(false);
  this.outputCallbacks.push(callback);
  this.outputSize += Buffer.byteLength(data);
  this._onPendingData(data.length);

  if (isBufferSizeExceeded(this.outputSize)) {
    this._assignSocket();
    return true;
  }
  return false;
}
github pmq20 / node-packer / node / lib / _http_outgoing.js View on Github external
}


  // If we get an empty string or buffer, then just do nothing, and
  // signal the user to keep writing.
  if (chunk.length === 0) return true;

  if (!fromEnd && msg.connection && !msg.connection.corked) {
    msg.connection.cork();
    process.nextTick(connectionCorkNT, msg.connection);
  }

  var len, ret;
  if (msg.chunkedEncoding) {
    if (typeof chunk === 'string')
      len = Buffer.byteLength(chunk, encoding);
    else
      len = chunk.length;

    msg._send(len.toString(16), 'latin1', null);
    msg._send(crlf_buf, null, null);
    msg._send(chunk, encoding, null);
    ret = msg._send(crlf_buf, null, callback);
  } else {
    ret = msg._send(chunk, encoding, callback);
  }

  debug('write ret = ' + ret);
  return ret;
}
github node-app / Nodelike / Nodelike Tests / package / test-buffer.js View on Github external
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  assert.equal(24, Buffer.byteLength('Il était tué', encoding));
});
assert.equal(12, Buffer.byteLength('Il était tué', 'ascii'));
github senchalabs / connect / lib / connect / response.js View on Github external
resProto.simpleBody = function(code, message, extraHeaders) {
    if (!warned) {
        console.warn('Warning: connect res.simpleBody() will be removed in 1.0');
        warned = true;
    }
    var length;
    var encoding;
    var type = "text/plain; charset=utf8";
    if (typeof message === 'object' && !(message instanceof Buffer)) {
        message = JSON.stringify(message);
        type = "application/json; charset=utf8";
    }
    message = message || "";
    length = message.length;
    if (typeof message === 'string') {
        length = Buffer.byteLength(message);
        encoding = "utf8";
    }
    var headers = {
        "Content-Type": type,
        "Content-Length": length
    };
    if (extraHeaders) {
        if (typeof extraHeaders === 'string') {
            headers["Content-Type"] = extraHeaders;
        } else {
            Object.keys(extraHeaders).forEach(function (key) {
                headers[key] = extraHeaders[key];
            });
        }
    }
    this.writeHead(code, headers);
github OptimalBits / cabinet / lib / cabinet.js View on Github external
virtuals[path].call(root, function(err, data, files){
          watcher && watcher.setVirtual(path, files);

          var length = typeof data === 'string' ? Buffer.byteLength(data) : data.length;

          setResponseHeaders(path, res, {size:length, mtime:new Date()});

          if(cache){
            cacheFile(data, length, res._headers, path).then(function(entry){
              sendEntry(req, res, entry);
            })
          }else{
            entry = {
              headers: res._headers,
              data: [data]
            };
            sendEntry(req, res, entry);
          }
        });
      }
github tpadjen / ng2-prism / jspm_packages / npm / asn1.js@4.5.0 / lib / asn1 / base / buffer.js View on Github external
if (Array.isArray(value)) {
      this.length = 0;
      this.value = value.map(function(item) {
        if (!(item instanceof EncoderBuffer))
          item = new EncoderBuffer(item, reporter);
        this.length += item.length;
        return item;
      }, this);
    } else if (typeof value === 'number') {
      if (!(0 <= value && value <= 0xff))
        return reporter.error('non-byte EncoderBuffer value');
      this.value = value;
      this.length = 1;
    } else if (typeof value === 'string') {
      this.value = value;
      this.length = Buffer.byteLength(value);
    } else if (Buffer.isBuffer(value)) {
      this.value = value;
      this.length = value.length;
    } else {
      return reporter.error('Unsupported type: ' + typeof value);
    }
  }
  exports.EncoderBuffer = EncoderBuffer;
github pmq20 / node-packer / node / lib / child_process.js View on Github external
child.stdout.on('data', function onChildStdout(chunk) {
      stdoutLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length;

      if (stdoutLen > options.maxBuffer) {
        ex = new Error('stdout maxBuffer exceeded');
        kill();
      } else {
        if (encoding)
          _stdout += chunk;
        else
          _stdout.push(chunk);
      }
    });
  }
github indutny / bitcode-builder / src / bitcode.ts View on Github external
public static cstring(value: string): constants.Array {
    const len = Buffer.byteLength(value);
    const blob = Buffer.alloc(len + 1);
    blob.write(value);
    return Builder.blob(Buffer.from(blob));
  }
github nexe / nexe / src / compiler.ts View on Github external
private _assembleDeliverable(binary: NodeJS.ReadableStream) {
    if (!this.options.mangle) {
      return binary
    }

    const startup = this.code(),
      codeSize = Buffer.byteLength(startup)

    const lengths = Buffer.from(Array(16))
    lengths.writeDoubleLE(codeSize, 0)
    lengths.writeDoubleLE(this.bundle.blobSize, 8)
    return new (MultiStream as any)([
      binary,
      toStream(startup),
      this.bundle.toStream(),
      toStream(Buffer.concat([Buffer.from(''), lengths]))
    ])
  }
}