Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add an optional maxLength argument to Buffer.write
Browse files Browse the repository at this point in the history
Fixes #243.
Fixes #1361.
  • Loading branch information
koichik committed Jul 21, 2011
1 parent 4d3a907 commit f2ee5aa
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 24 deletions.
69 changes: 45 additions & 24 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ SlowBuffer.prototype.toString = function(encoding, start, end) {
};


SlowBuffer.prototype.hexWrite = function(string, offset) {
SlowBuffer.prototype.hexWrite = function(string, offset, maxLength) {
var len = string.length;
offset = +offset || 0;
maxLength = Math.min(+maxLength || Number.MAX_VALUE, this.length - offset);

// must be an even number of digits
if (len % 2) {
throw new Error('Invalid hex string');
}
for (var i = 0; i < len / 2; i++) {
maxLength = Math.min(maxLength, len / 2);
for (var i = 0; i < maxLength; i++) {
var byte = parseInt(string.substr(i * 2, 2), 16);
if (isNaN(byte)) throw new Error('Invalid hex string');
this[offset + i] = byte;
Expand All @@ -109,38 +111,48 @@ SlowBuffer.prototype.hexWrite = function(string, offset) {
};


SlowBuffer.prototype.write = function(string, offset, encoding) {
// Support both (string, offset, encoding)
// and the legacy (string, encoding, offset)
if (!isFinite(offset)) {
var swap = encoding;
encoding = offset;
offset = swap;
SlowBuffer.prototype.write = function(string) {
// Support both (string, offset, maxLength, encoding)
// and the legacy (string, encoding, offset, maxLength)
var offset, maxLength, encoding;
if (isFinite(arguments[1])) {
offset = arguments[1];
if (isFinite(arguments[2])) {
maxLength = arguments[2];
encoding = arguments[3];
} else {
encoding = arguments[2];
}
} else { // legacy
encoding = arguments[1];
offset = arguments[2];
maxLength = arguments[3];
}

offset = +offset || 0;
maxLength = Math.min(+maxLength || Number.MAX_VALUE, this.length - offset);
encoding = String(encoding || 'utf8').toLowerCase();

switch (encoding) {
case 'hex':
return this.hexWrite(string, offset);
return this.hexWrite(string, offset, maxLength);

case 'utf8':
case 'utf-8':
return this.utf8Write(string, offset);
return this.utf8Write(string, offset, maxLength);

case 'ascii':
return this.asciiWrite(string, offset);
return this.asciiWrite(string, offset, maxLength);

case 'binary':
return this.binaryWrite(string, offset);
return this.binaryWrite(string, offset, maxLength);

case 'base64':
return this.base64Write(string, offset);
return this.base64Write(string, offset, maxLength);

case 'ucs2':
case 'ucs-2':
return this.ucs2Write(string, offset);
return this.ucs2Write(string, offset, maxLength);

default:
throw new Error('Unknown encoding');
Expand Down Expand Up @@ -271,20 +283,29 @@ Buffer.prototype.set = function set(i, v) {
};


// write(string, offset = 0, encoding = 'utf8')
Buffer.prototype.write = function(string, offset, encoding) {
if (!isFinite(offset)) {
var swap = encoding;
encoding = offset;
offset = swap;
// write(string, offset = 0, maxLength = this.length-offset, encoding = 'utf8')
Buffer.prototype.write = function(string) {
// Support both (string, offset, maxLength, encoding)
// and the legacy (string, encoding, offset, maxLength)
var offset, maxLength, encoding;
if (isFinite(arguments[1])) {
offset = arguments[1];
if (isFinite(arguments[2])) {
maxLength = arguments[2];
encoding = arguments[3];
} else {
encoding = arguments[2];
}
} else { // legacy
encoding = arguments[1];
offset = arguments[2];
maxLength = arguments[3];
}

offset = +offset || 0;
maxLength = Math.min(+maxLength || Number.MAX_VALUE, this.length - offset);
encoding = String(encoding || 'utf8').toLowerCase();

// Make sure we are not going to overflow
var maxLength = this.length - offset;

var ret;
switch (encoding) {
case 'hex':
Expand Down
47 changes: 47 additions & 0 deletions test/simple/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,50 @@ assert.equal(written, 9);
written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
assert.equal(written, 10);

// #243 Test write() with maxLength
var buf = new Buffer(4);
buf.fill(0xFF);
var written = buf.write('abcd', 1, 2, 'utf8');
console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);

buf.fill(0xFF);
written = buf.write('abcd', 1, 4);
console.log(buf);
assert.equal(written, 3);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0x63);

buf.fill(0xFF);
written = buf.write('abcd', 'utf8', 1, 2); // legacy style
console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);

buf.fill(0xFF);
written = buf.write('abcdef', 1, 2, 'hex');
console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0xAB);
assert.equal(buf[2], 0xCD);
assert.equal(buf[3], 0xFF);

buf.fill(0xFF);
written = buf.write('abcd', 0, 2, 'ucs2');
console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0x61);
assert.equal(buf[1], 0x00);
assert.equal(buf[2], 0xFF);
assert.equal(buf[3], 0xFF);

0 comments on commit f2ee5aa

Please sign in to comment.