Skip to content

Commit

Permalink
Adding untested write functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Apr 9, 2011
1 parent e16cf20 commit e6cc1d0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/fs.js
Expand Up @@ -100,3 +100,34 @@ function readFile(path, callback) {
stream.on("end", callback.bind(null, null, buffer));
}
exports.readFile = readFile;

function writeFile(path, content, encoding, callback) {
try {
if (!callback) {
callback = encoding;
encoding = undefined;
}

encoding = String(encoding || "utf-8").toUpperCase();
let fd = FileDescriptor(path);
let outputStream = FileOutputStream();
let converter = ConverterStream();

outputStream.init(fd, 0x02 | 0x08 | 0x20, parseInt("0666"),
outputStream.DEFER_OPEN);
converter.charset = encoding;
let inputStream = converter.convertToInputStream(content.toString(encoding));
let copier = StreamCopier();
copier.init(inputStream, outputStream, null, sourceBuffered, sinkBuffered,
0x8000, true, true);
copier.asyncCopy({
onStartRequest: function onStartRequest(aRequest, aContext) {},
onStopRequest: function onStopRequest(aRequest, aContext, aStatusCode) {
callback(null);
}
}, null);
} catch (error) {
callback(error);
}
}
exports.writeFile = writeFile;

0 comments on commit e6cc1d0

Please sign in to comment.