mirror of https://github.com/lukechilds/node.git
Browse Source
This patch makes buffers the preferred input for fs.write() and fs.writeSync(). The old string interface is still supported by converting strings to buffers dynamically. This allows to remove the C++ code for string handling which is also part of this patch.v0.7.4-release
Felix Geisendörfer
15 years ago
committed by
Ryan Dahl
5 changed files with 95 additions and 107 deletions
@ -0,0 +1,32 @@ |
|||||
|
require('../common'); |
||||
|
var path = require('path') |
||||
|
, Buffer = require('buffer').Buffer |
||||
|
, fs = require('fs') |
||||
|
, fn = path.join(fixturesDir, 'write.txt') |
||||
|
, expected = new Buffer('hello') |
||||
|
, openCalled = 0 |
||||
|
, writeCalled = 0; |
||||
|
|
||||
|
|
||||
|
fs.open(fn, 'w', 0644, function (err, fd) { |
||||
|
openCalled++; |
||||
|
if (err) throw err; |
||||
|
|
||||
|
fs.write(fd, expected, 0, expected.length, null, function(err, written) { |
||||
|
writeCalled++; |
||||
|
if (err) throw err; |
||||
|
|
||||
|
assert.equal(expected.length, written); |
||||
|
fs.closeSync(fd); |
||||
|
|
||||
|
var found = fs.readFileSync(fn); |
||||
|
assert.deepEqual(found, expected.toString()); |
||||
|
fs.unlinkSync(fn); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
process.addListener("exit", function () { |
||||
|
assert.equal(openCalled, 1); |
||||
|
assert.equal(writeCalled, 1); |
||||
|
}); |
||||
|
|
@ -0,0 +1,12 @@ |
|||||
|
require('../common'); |
||||
|
var path = require('path') |
||||
|
, Buffer = require('buffer').Buffer |
||||
|
, fs = require('fs') |
||||
|
, fn = path.join(fixturesDir, 'write.txt'); |
||||
|
|
||||
|
var fd = fs.openSync(fn, 'w'); |
||||
|
fs.writeSync(fd, 'foo'); |
||||
|
fs.writeSync(fd, new Buffer('bar'), 0, 3); |
||||
|
fs.closeSync(fd); |
||||
|
|
||||
|
assert.equal(fs.readFileSync(fn), 'foobar'); |
Loading…
Reference in new issue