From e232f09d38194844305b4038a68b8a70717704e4 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 17 May 2010 15:22:09 -0700 Subject: [PATCH] fs.WriteStream.write should support buffer Also re-adding the callback parameter. --- lib/fs.js | 17 +++++-- test/fixtures/print-chars-from-buffer.js | 9 ++++ test/fixtures/stdout.js | 2 - test/simple/test-stdout-to-file.js | 63 ++++++++++++++++++------ 4 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 test/fixtures/print-chars-from-buffer.js delete mode 100644 test/fixtures/stdout.js diff --git a/lib/fs.js b/lib/fs.js index 833300d2e3a..ec68c02e9d6 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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; diff --git a/test/fixtures/print-chars-from-buffer.js b/test/fixtures/print-chars-from-buffer.js new file mode 100644 index 00000000000..94fce08889f --- /dev/null +++ b/test/fixtures/print-chars-from-buffer.js @@ -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); diff --git a/test/fixtures/stdout.js b/test/fixtures/stdout.js deleted file mode 100644 index 9aa72340c10..00000000000 --- a/test/fixtures/stdout.js +++ /dev/null @@ -1,2 +0,0 @@ -var sys = require('sys'); -sys.puts('test'); \ No newline at end of file diff --git a/test/simple/test-stdout-to-file.js b/test/simple/test-stdout-to-file.js index 12d8b5e2eb6..5c611bfe499 100644 --- a/test/simple/test-stdout-to-file.js +++ b/test/simple/test-stdout-to-file.js @@ -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; +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'); -try { - fs.unlinkSync(tmpFile); -} catch (e) {} +function test (size, useBuffer, cb) { + var cmd = process.argv[0] + + ' ' + + (useBuffer ? scriptBuffer : scriptString) + + ' ' + + size + + ' > ' + + tmpFile + ; -childProccess.exec(cmd, function(err) { - if (err) throw err; + try { + fs.unlinkSync(tmpFile); + } catch (e) {} - var data = fs.readFileSync(tmpFile); - assert.equal(data, "test\n"); - fs.unlinkSync(tmpFile); -}); \ No newline at end of file + 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); +});