Skip to content

Commit

Permalink
fs.WriteStream.write should support buffer
Browse files Browse the repository at this point in the history
Also re-adding the callback parameter.
  • Loading branch information
ry committed May 18, 2010
1 parent 1036aa9 commit e232f09
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
17 changes: 14 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,14 +752,25 @@ WriteStream.prototype.flush = function () {
};


WriteStream.prototype.write = function(data, encoding) {
WriteStream.prototype.write = function (data) {
if (!this.writeable) {
throw new Error('stream not writeable');
}

// TODO handle Buffer
var cb;
if (typeof(arguments[arguments.length-1]) == 'function') {
cb = arguments[arguments.length-1];
}

if (data instanceof Buffer) {
this._queue.push([fs.write, data, 0, data.length, null, cb]);
} else {
var encoding = 'utf8';
if (typeof(arguments[1]) == 'string') encoding = arguments[1];
this._queue.push([fs.write, data, undefined, encoding, cb]);
}


this._queue.push([fs.write, data, undefined, encoding || 'utf8', null]);
this.flush();

return false;
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/print-chars-from-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require("../common");
Buffer = require("buffer").Buffer;

var n = parseInt(process.argv[2]);

b = new Buffer(n);
for (var i = 0; i < n; i++) { b[i] = 100; }

process.stdout.write(b);
2 changes: 0 additions & 2 deletions test/fixtures/stdout.js

This file was deleted.

69 changes: 51 additions & 18 deletions test/simple/test-stdout-to-file.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
require('../common');
var path = require('path')
, childProccess = require('child_process')
, fs = require('fs')
, stdoutScript = path.join(path.dirname(__dirname), 'fixtures/stdout.js')
, tmpFile = path.join(path.dirname(__dirname), 'fixtures/stdout.txt')
, cmd = process.argv[0]+' '+stdoutScript+' > '+tmpFile;

try {
fs.unlinkSync(tmpFile);
} catch (e) {}

childProccess.exec(cmd, function(err) {
if (err) throw err;

var data = fs.readFileSync(tmpFile);
assert.equal(data, "test\n");
fs.unlinkSync(tmpFile);
});
path = require('path');
childProccess = require('child_process');
fs = require('fs');
scriptString = path.join(fixturesDir, 'print-chars.js');
scriptBuffer = path.join(fixturesDir, 'print-chars-from-buffer.js');
tmpFile = path.join(fixturesDir, 'stdout.txt');

function test (size, useBuffer, cb) {
var cmd = process.argv[0]
+ ' '
+ (useBuffer ? scriptBuffer : scriptString)
+ ' '
+ size
+ ' > '
+ tmpFile
;

try {
fs.unlinkSync(tmpFile);
} catch (e) {}

print(size + ' chars to ' + tmpFile + '...');

childProccess.exec(cmd, function(err) {
if (err) throw err;

puts('done!');

var stat = fs.statSync(tmpFile);

puts(tmpFile + ' has ' + stat.size + ' bytes');

assert.equal(size, stat.size);
fs.unlinkSync(tmpFile);

cb();
});
}

finished = false;
test(1024*1024, false, function () {
puts("Done printing with string");
test(1024*1024, true, function () {
puts("Done printing with buffer");
finished = true;
});
});

process.addListener('exit', function () {
assert.ok(finished);
});

0 comments on commit e232f09

Please sign in to comment.