Example #1
0
ReadFileContext.prototype.read = function() {
  var buffer;
  var offset;
  var length;

  if (this.size === 0) {
    buffer = this.buffer = Buffer.allocUnsafeSlow(kReadFileBufferLength);
    offset = 0;
    length = kReadFileBufferLength;
  } else {
    buffer = this.buffer;
    offset = this.pos;
    length = this.size - this.pos;
  }

  var req = new FSReqWrap();
  req.oncomplete = readFileAfterRead;
  req.context = this;

  binding.read(this.fd, buffer, offset, length, -1, req);
};
Example #2
0
function readFileAfterStat(err, st) {
  var context = this.context;

  if (err)
    return context.close(err);

  var size = context.size = st.isFile() ? st.size : 0;

  if (size === 0) {
    context.buffers = [];
    context.read();
    return;
  }

  if (size > kMaxLength) {
    err = new RangeError('File size is greater than possible Buffer: ' +
                         `0x${kMaxLength.toString(16)} bytes`);
    return context.close(err);
  }

  context.buffer = Buffer.allocUnsafeSlow(size);
  context.read();
}
Example #3
0
assert.throws(function() {
  Buffer.allocUnsafeSlow((-1 >>> 0) + 1);
}, RangeError);
Example #4
0
    assert.equal(b[100 + i], slice[i]);
  }
}

{
  // make sure only top level parent propagates from allocPool
  const b = Buffer.allocUnsafe(5);
  const c = b.slice(0, 4);
  const d = c.slice(0, 2);
  assert.equal(b.parent, c.parent);
  assert.equal(b.parent, d.parent);
}

{
  // also from a non-pooled instance
  const b = Buffer.allocUnsafeSlow(5);
  const c = b.slice(0, 4);
  const d = c.slice(0, 2);
  assert.equal(c.parent, d.parent);
}

{
  // Bug regression test
  const testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
  const buffer = Buffer.allocUnsafe(32);
  const size = buffer.write(testValue, 0, 'utf8');
  console.log('bytes written to buffer: ' + size);
  const slice = buffer.toString('utf8', 0, size);
  assert.equal(slice, testValue);
}
// coerce values to string
assert.throws(() => { Buffer.byteLength(32, 'latin1'); },
              '"string" must be a string, Buffer, or ArrayBuffer');
assert.throws(() => { Buffer.byteLength(NaN, 'utf8'); },
              '"string" must be a string, Buffer, or ArrayBuffer');
assert.throws(() => { Buffer.byteLength({}, 'latin1'); },
              '"string" must be a string, Buffer, or ArrayBuffer');
assert.throws(() => { Buffer.byteLength(); },
              '"string" must be a string, Buffer, or ArrayBuffer');

assert(ArrayBuffer.isView(new Buffer(10)));
assert(ArrayBuffer.isView(new SlowBuffer(10)));
assert(ArrayBuffer.isView(Buffer.alloc(10)));
assert(ArrayBuffer.isView(Buffer.allocUnsafe(10)));
assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10)));
assert(ArrayBuffer.isView(Buffer.from('')));

// buffer
var incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
assert.strictEqual(Buffer.byteLength(incomplete), 5);
var ascii = Buffer.from('abc');
assert.strictEqual(Buffer.byteLength(ascii), 3);

// ArrayBuffer
var buffer = new ArrayBuffer(8);
assert.equal(Buffer.byteLength(buffer), 8);

// TypedArray
var int8 = new Int8Array(8);
assert.strictEqual(Buffer.byteLength(int8), 8);
Example #6
0
assert.throws(() => Buffer.allocUnsafeSlow((-1 >>> 0) + 1), RangeError);
Example #7
0
assert.throws(() => Buffer.allocUnsafeSlow(-100),
Example #8
0
assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize),