Skip to content

Commit ee83c95

Browse files
JacksonTianevanlucas
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 <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1f8e4b5 commit ee83c95

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

doc/api/buffer.markdown

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ additional performance that `Buffer.allocUnsafe(size)` provides.
451451

452452
### Class Method: Buffer.byteLength(string[, encoding])
453453

454-
* `string` {String}
454+
* `string` {String | Buffer | TypedArray | DataView | ArrayBuffer}
455455
* `encoding` {String} Default: `'utf8'`
456456
* Return: {Number}
457457

@@ -470,6 +470,11 @@ console.log(`${str}: ${str.length} characters, ` +
470470
// ½ + ¼ = ¾: 9 characters, 12 bytes
471471
```
472472

473+
When `string` is a `Buffer`/[`DataView`][]/[`TypedArray`][]/`ArrayBuffer`,
474+
returns the actual byte length.
475+
476+
Otherwise, converts to `String` and returns the byte length of string.
477+
473478
### Class Method: Buffer.compare(buf1, buf2)
474479

475480
* `buf1` {Buffer}
@@ -1744,4 +1749,6 @@ console.log(buf);
17441749
[buffer_from_string]: #buffer_class_method_buffer_from_str_encoding
17451750
[buffer_allocunsafe]: #buffer_class_method_buffer_allocraw_size
17461751
[buffer_alloc]: #buffer_class_method_buffer_alloc_size_fill_encoding
1747-
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
1752+
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
1753+
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
1754+
[`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
@@ -340,11 +340,12 @@ function base64ByteLength(str, bytes) {
340340

341341

342342
function byteLength(string, encoding) {
343-
if (string instanceof Buffer)
344-
return string.length;
343+
if (typeof string !== 'string') {
344+
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
345+
return string.byteLength;
345346

346-
if (typeof string !== 'string')
347347
string = '' + string;
348+
}
348349

349350
var len = string.length;
350351
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 = new Buffer([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
1521
assert.equal(Buffer.byteLength(incomplete), 5);
1622
var ascii = new Buffer('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)