mirror of https://github.com/lukechilds/node.git
Browse Source
This patch makes buffers the preferred output for fs.read() and fs.readSync(). The old string interface is still supported by converting buffers to strings 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 128 additions and 115 deletions
@ -0,0 +1,25 @@ |
|||||
|
require('../common'); |
||||
|
var path = require('path'), |
||||
|
Buffer = require('buffer').Buffer, |
||||
|
fs = require('fs'), |
||||
|
filepath = path.join(fixturesDir, 'x.txt'), |
||||
|
fd = fs.openSync(filepath, 'r'), |
||||
|
expected = 'xyz\n', |
||||
|
bufferAsync = new Buffer(expected.length), |
||||
|
bufferSync = new Buffer(expected.length), |
||||
|
readCalled = 0; |
||||
|
|
||||
|
fs.read(fd, bufferAsync, 0, expected.length, 0, function(err, bytesRead) { |
||||
|
readCalled++; |
||||
|
|
||||
|
assert.equal(bytesRead, expected.length); |
||||
|
assert.deepEqual(bufferAsync, new Buffer(expected)); |
||||
|
}); |
||||
|
|
||||
|
var r = fs.readSync(fd, bufferSync, 0, expected.length, 0); |
||||
|
assert.deepEqual(bufferSync, new Buffer(expected)); |
||||
|
assert.equal(r, expected.length); |
||||
|
|
||||
|
process.addListener('exit', function() { |
||||
|
assert.equal(readCalled, 1); |
||||
|
}); |
@ -0,0 +1,23 @@ |
|||||
|
require('../common'); |
||||
|
var path = require('path'), |
||||
|
fs = require('fs'), |
||||
|
filepath = path.join(fixturesDir, 'x.txt'), |
||||
|
fd = fs.openSync(filepath, 'r'), |
||||
|
expected = 'xyz\n', |
||||
|
readCalled = 0; |
||||
|
|
||||
|
fs.read(fd, expected.length, 0, 'utf-8', function(err, str, bytesRead) { |
||||
|
readCalled++; |
||||
|
|
||||
|
assert.ok(!err); |
||||
|
assert.equal(str, expected); |
||||
|
assert.equal(bytesRead, expected.length); |
||||
|
}); |
||||
|
|
||||
|
var r = fs.readSync(fd, expected.length, 0, 'utf-8'); |
||||
|
assert.equal(r[0], expected); |
||||
|
assert.equal(r[1], expected.length); |
||||
|
|
||||
|
process.addListener('exit', function() { |
||||
|
assert.equal(readCalled, 1); |
||||
|
}); |
Loading…
Reference in new issue