mirror of https://github.com/nodejs/node.git
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 <trev.norris@gmail.com>
This commit is contained in:
parent
bdc2ea4d52
commit
6462519d3c
|
@ -72,6 +72,12 @@ will be thrown here.
|
||||||
|
|
||||||
Allocates a new buffer using an `array` of octets.
|
Allocates a new buffer using an `array` of octets.
|
||||||
|
|
||||||
|
### new Buffer(buffer)
|
||||||
|
|
||||||
|
* `buffer` {Buffer}
|
||||||
|
|
||||||
|
Copies the passed `buffer` data onto a new `Buffer` instance.
|
||||||
|
|
||||||
### new Buffer(str[, encoding])
|
### new Buffer(str[, encoding])
|
||||||
|
|
||||||
* `str` String - string to encode.
|
* `str` String - string to encode.
|
||||||
|
|
|
@ -49,17 +49,24 @@ function Buffer(subject, encoding) {
|
||||||
if (!util.isBuffer(this))
|
if (!util.isBuffer(this))
|
||||||
return new Buffer(subject, encoding);
|
return new Buffer(subject, encoding);
|
||||||
|
|
||||||
if (util.isNumber(subject))
|
if (util.isNumber(subject)) {
|
||||||
this.length = subject > 0 ? subject >>> 0 : 0;
|
this.length = subject > 0 ? subject >>> 0 : 0;
|
||||||
else if (util.isString(subject))
|
|
||||||
this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
|
} else if (util.isString(subject)) {
|
||||||
else if (util.isObject(subject)) {
|
if (!util.isString(encoding) || encoding.length === 0)
|
||||||
|
encoding = 'utf8';
|
||||||
|
this.length = Buffer.byteLength(subject, encoding);
|
||||||
|
|
||||||
|
// Handle Arrays, Buffers, Uint8Arrays or JSON.
|
||||||
|
} else if (util.isObject(subject)) {
|
||||||
if (subject.type === 'Buffer' && util.isArray(subject.data))
|
if (subject.type === 'Buffer' && util.isArray(subject.data))
|
||||||
subject = subject.data;
|
subject = subject.data;
|
||||||
|
// Must use floor() because array length may be > kMaxLength.
|
||||||
this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0;
|
this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0;
|
||||||
} else
|
|
||||||
|
} else {
|
||||||
throw new TypeError('must start with number, buffer, array or string');
|
throw new TypeError('must start with number, buffer, array or string');
|
||||||
|
}
|
||||||
|
|
||||||
if (this.length > kMaxLength) {
|
if (this.length > kMaxLength) {
|
||||||
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
||||||
|
@ -79,25 +86,31 @@ function Buffer(subject, encoding) {
|
||||||
alloc(this, this.length);
|
alloc(this, this.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!util.isNumber(subject)) {
|
if (util.isNumber(subject)) {
|
||||||
if (util.isString(subject)) {
|
return;
|
||||||
// In the case of base64 it's possible that the size of the buffer
|
}
|
||||||
// allocated was slightly too large. In this case we need to rewrite
|
|
||||||
// the length to the actual length written.
|
|
||||||
var len = this.write(subject, encoding);
|
|
||||||
|
|
||||||
// Buffer was truncated after decode, realloc internal ExternalArray
|
if (util.isString(subject)) {
|
||||||
if (len !== this.length) {
|
// In the case of base64 it's possible that the size of the buffer
|
||||||
this.length = len;
|
// allocated was slightly too large. In this case we need to rewrite
|
||||||
truncate(this, this.length);
|
// the length to the actual length written.
|
||||||
}
|
var len = this.write(subject, encoding);
|
||||||
} else {
|
// Buffer was truncated after decode, realloc internal ExternalArray
|
||||||
if (util.isBuffer(subject))
|
if (len !== this.length) {
|
||||||
subject.copy(this, 0, 0, this.length);
|
var prevLen = this.length;
|
||||||
else if (util.isNumber(subject.length) || util.isArray(subject))
|
this.length = len;
|
||||||
for (var i = 0; i < this.length; i++)
|
truncate(this, this.length);
|
||||||
this[i] = subject[i];
|
poolOffset -= (prevLen - len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (util.isBuffer(subject)) {
|
||||||
|
subject.copy(this, 0, 0, this.length);
|
||||||
|
|
||||||
|
} else if (util.isNumber(subject.length) || util.isArray(subject)) {
|
||||||
|
// Really crappy way to handle Uint8Arrays, but V8 doesn't give a simple
|
||||||
|
// way to access the data from the C++ API.
|
||||||
|
for (var i = 0; i < this.length; i++)
|
||||||
|
this[i] = subject[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue