Skip to content

Commit 293fd04

Browse files
JacksonTianjasnell
authored andcommitted
buffer: make byteLength work with ArrayBuffer & DataView
Convert anything to string, but Buffer, TypedArray and ArrayBuffer ``` var uint8 = new Uint8Array([0xf0, 0x9f, 0x90]); Buffer.byteLength(uint8); // should be 3, but returns 11 Buffer.byteLength(uint8.buffer); // should be 3, but return 20 ``` PR-URL: #5255 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent afd821a commit 293fd04

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

doc/api/buffer.markdown

+8-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ additional performance that `Buffer.allocUnsafe(size)` provides.
468468

469469
### Class Method: Buffer.byteLength(string[, encoding])
470470

471-
* `string` {String}
471+
* `string` {String | Buffer | TypedArray | DataView | ArrayBuffer}
472472
* `encoding` {String} Default: `'utf8'`
473473
* Return: {Number}
474474

@@ -487,6 +487,11 @@ console.log(`${str}: ${str.length} characters, ` +
487487
// ½ + ¼ = ¾: 9 characters, 12 bytes
488488
```
489489

490+
When `string` is a `Buffer`/[`DataView`][]/[`TypedArray`][]/`ArrayBuffer`,
491+
returns the actual byte length.
492+
493+
Otherwise, converts to `String` and returns the byte length of string.
494+
490495
### Class Method: Buffer.compare(buf1, buf2)
491496

492497
* `buf1` {Buffer}
@@ -1765,3 +1770,5 @@ console.log(buf);
17651770
[buffer_allocunsafe]: #buffer_class_method_buffer_allocraw_size
17661771
[buffer_alloc]: #buffer_class_method_buffer_alloc_size_fill_encoding
17671772
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
1773+
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
1774+
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

lib/buffer.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,12 @@ function base64ByteLength(str, bytes) {
364364

365365

366366
function byteLength(string, encoding) {
367-
if (string instanceof Buffer)
368-
return string.length;
367+
if (typeof string !== 'string') {
368+
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
369+
return string.byteLength;
369370

370-
if (typeof string !== 'string')
371371
string = '' + string;
372+
}
372373

373374
var len = string.length;
374375
if (len === 0)

test/parallel/test-buffer-bytelength.js

+34
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,53 @@
33
require('../common');
44
var assert = require('assert');
55
var Buffer = require('buffer').Buffer;
6+
var SlowBuffer = require('buffer').SlowBuffer;
67

78
// coerce values to string
89
assert.equal(Buffer.byteLength(32, 'binary'), 2);
910
assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
1011
assert.equal(Buffer.byteLength({}, 'binary'), 15);
1112
assert.equal(Buffer.byteLength(), 9);
1213

14+
var buff = new Buffer(10);
15+
assert(ArrayBuffer.isView(buff));
16+
var slowbuff = new SlowBuffer(10);
17+
assert(ArrayBuffer.isView(slowbuff));
18+
1319
// buffer
1420
var incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
1521
assert.equal(Buffer.byteLength(incomplete), 5);
1622
var ascii = Buffer.from('abc');
1723
assert.equal(Buffer.byteLength(ascii), 3);
1824

25+
// ArrayBuffer
26+
var buffer = new ArrayBuffer(8);
27+
assert.equal(Buffer.byteLength(buffer), 8);
28+
29+
// TypedArray
30+
var int8 = new Int8Array(8);
31+
assert.equal(Buffer.byteLength(int8), 8);
32+
var uint8 = new Uint8Array(8);
33+
assert.equal(Buffer.byteLength(uint8), 8);
34+
var uintc8 = new Uint8ClampedArray(2);
35+
assert.equal(Buffer.byteLength(uintc8), 2);
36+
var int16 = new Int16Array(8);
37+
assert.equal(Buffer.byteLength(int16), 16);
38+
var uint16 = new Uint16Array(8);
39+
assert.equal(Buffer.byteLength(uint16), 16);
40+
var int32 = new Int32Array(8);
41+
assert.equal(Buffer.byteLength(int32), 32);
42+
var uint32 = new Uint32Array(8);
43+
assert.equal(Buffer.byteLength(uint32), 32);
44+
var float32 = new Float32Array(8);
45+
assert.equal(Buffer.byteLength(float32), 32);
46+
var float64 = new Float64Array(8);
47+
assert.equal(Buffer.byteLength(float64), 64);
48+
49+
// DataView
50+
var dv = new DataView(new ArrayBuffer(2));
51+
assert.equal(Buffer.byteLength(dv), 2);
52+
1953
// special case: zero length string
2054
assert.equal(Buffer.byteLength('', 'ascii'), 0);
2155
assert.equal(Buffer.byteLength('', 'HeX'), 0);

0 commit comments

Comments
 (0)