Skip to content

Commit 0c74f60

Browse files
committed
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 ```
1 parent 6a3d38f commit 0c74f60

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

doc/api/buffer.markdown

Lines changed: 8 additions & 1 deletion
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}
@@ -1727,3 +1732,5 @@ console.log(buf);
17271732
[buffer_allocunsafe]: #buffer_class_method_buffer_allocraw_size
17281733
[buffer_alloc]: #buffer_class_method_buffer_alloc_size_fill_encoding
17291734
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
1735+
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
1736+
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

lib/buffer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,12 @@ function base64ByteLength(str, bytes) {
317317

318318

319319
function byteLength(string, encoding) {
320-
if (string instanceof Buffer)
321-
return string.length;
320+
if (typeof string !== 'string') {
321+
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
322+
return string.byteLength;
322323

323-
if (typeof string !== 'string')
324324
string = '' + string;
325+
}
325326

326327
var len = string.length;
327328
if (len === 0)

test/parallel/test-buffer-bytelength.js

Lines changed: 34 additions & 0 deletions
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)