summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTrevor Norris <trev.norris@gmail.com>2013-11-13 16:27:20 -0800
committerTrevor Norris <trev.norris@gmail.com>2013-11-15 11:48:03 -0800
commita263abaa812010710958f89a493e0e5c8b29575a (patch)
treeba0fe15d63269f1f94defc23054c0e823c3daa36 /lib
parent85c8eeb838921ca76146387343a4b2e7d87e6324 (diff)
downloadnodejs-a263abaa812010710958f89a493e0e5c8b29575a.tar.gz
nodejs-a263abaa812010710958f89a493e0e5c8b29575a.tar.bz2
nodejs-a263abaa812010710958f89a493e0e5c8b29575a.zip
buffer: no warning when encoding isn't passed
Buffer#write() was showing the deprecation warning when only buf.write('string') was passed. This is incorrect since the encoding is always optional. Argument order should follow: Buffer#write(string[, offset[, length]][, encoding]) (yeah, not confusing at all)
Diffstat (limited to 'lib')
-rw-r--r--lib/buffer.js21
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index de0ab040d..d5e504256 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -75,8 +75,10 @@ function Buffer(subject, encoding) {
if (!util.isNumber(subject)) {
if (util.isString(subject)) {
- // FIXME: the number of bytes hasn't changed, so why change the length?
- this.length = this.write(subject, 0, encoding);
+ // 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.
+ this.length = this.write(subject, encoding);
} else {
if (util.isBuffer(subject))
subject.copy(this, 0, 0, this.length);
@@ -281,18 +283,25 @@ Buffer.prototype.set = util.deprecate(function set(offset, v) {
// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
var writeWarned = false;
var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
- ' Use write(string, offset, length, encoding) instead.';
+ ' Use write(string[, offset[, length]][, encoding]) instead.';
Buffer.prototype.write = function(string, offset, length, encoding) {
- // allow write(string, encoding)
- if (util.isString(offset) && util.isUndefined(length)) {
+ // Buffer#write(string);
+ if (util.isUndefined(offset)) {
+ offset = 0;
+ encoding = 'utf8';
+
+ // Buffer#write(string, encoding)
+ } else if (util.isUndefined(length) && util.isString(offset)) {
encoding = offset;
offset = 0;
- // allow write(string, offset[, length], encoding)
+ // Buffer#write(string, offset[, length][, encoding])
} else if (isFinite(offset)) {
offset = ~~offset;
if (isFinite(length)) {
length = ~~length;
+ if (util.isUndefined(encoding))
+ encoding = 'utf8';
} else {
encoding = length;
length = undefined;