Skip to content

Commit 6462519

Browse files
committed
buffer, doc: misc. fix and cleanup
* Add official documentation that a Buffer instance is a viable argument when instantiating a new Buffer. * Properly set the poolOffset when a buffer needs to be truncated. * Add comments clarifying specific peculiar coding choices. * Remove a level of unnecessary indentation. Signed-off-by: Trevor Norris <[email protected]>
1 parent bdc2ea4 commit 6462519

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

doc/api/buffer.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ will be thrown here.
7272

7373
Allocates a new buffer using an `array` of octets.
7474

75+
### new Buffer(buffer)
76+
77+
* `buffer` {Buffer}
78+
79+
Copies the passed `buffer` data onto a new `Buffer` instance.
80+
7581
### new Buffer(str[, encoding])
7682

7783
* `str` String - string to encode.

lib/buffer.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ function Buffer(subject, encoding) {
4949
if (!util.isBuffer(this))
5050
return new Buffer(subject, encoding);
5151

52-
if (util.isNumber(subject))
52+
if (util.isNumber(subject)) {
5353
this.length = subject > 0 ? subject >>> 0 : 0;
54-
else if (util.isString(subject))
55-
this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
56-
else if (util.isObject(subject)) {
54+
55+
} else if (util.isString(subject)) {
56+
if (!util.isString(encoding) || encoding.length === 0)
57+
encoding = 'utf8';
58+
this.length = Buffer.byteLength(subject, encoding);
59+
60+
// Handle Arrays, Buffers, Uint8Arrays or JSON.
61+
} else if (util.isObject(subject)) {
5762
if (subject.type === 'Buffer' && util.isArray(subject.data))
5863
subject = subject.data;
59-
64+
// Must use floor() because array length may be > kMaxLength.
6065
this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0;
61-
} else
66+
67+
} else {
6268
throw new TypeError('must start with number, buffer, array or string');
69+
}
6370

6471
if (this.length > kMaxLength) {
6572
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
@@ -79,25 +86,31 @@ function Buffer(subject, encoding) {
7986
alloc(this, this.length);
8087
}
8188

82-
if (!util.isNumber(subject)) {
83-
if (util.isString(subject)) {
84-
// In the case of base64 it's possible that the size of the buffer
85-
// allocated was slightly too large. In this case we need to rewrite
86-
// the length to the actual length written.
87-
var len = this.write(subject, encoding);
88-
89-
// Buffer was truncated after decode, realloc internal ExternalArray
90-
if (len !== this.length) {
91-
this.length = len;
92-
truncate(this, this.length);
93-
}
94-
} else {
95-
if (util.isBuffer(subject))
96-
subject.copy(this, 0, 0, this.length);
97-
else if (util.isNumber(subject.length) || util.isArray(subject))
98-
for (var i = 0; i < this.length; i++)
99-
this[i] = subject[i];
89+
if (util.isNumber(subject)) {
90+
return;
91+
}
92+
93+
if (util.isString(subject)) {
94+
// In the case of base64 it's possible that the size of the buffer
95+
// allocated was slightly too large. In this case we need to rewrite
96+
// the length to the actual length written.
97+
var len = this.write(subject, encoding);
98+
// Buffer was truncated after decode, realloc internal ExternalArray
99+
if (len !== this.length) {
100+
var prevLen = this.length;
101+
this.length = len;
102+
truncate(this, this.length);
103+
poolOffset -= (prevLen - len);
100104
}
105+
106+
} else if (util.isBuffer(subject)) {
107+
subject.copy(this, 0, 0, this.length);
108+
109+
} else if (util.isNumber(subject.length) || util.isArray(subject)) {
110+
// Really crappy way to handle Uint8Arrays, but V8 doesn't give a simple
111+
// way to access the data from the C++ API.
112+
for (var i = 0; i < this.length; i++)
113+
this[i] = subject[i];
101114
}
102115
}
103116

0 commit comments

Comments
 (0)