Bug 943422 - Implement FileUtils.openAtomicFileOutputStream. r=Yoric

This commit is contained in:
Peiyong Lin
2013-12-17 10:57:03 -05:00
parent 097b39e3a2
commit 0a3e994786
2 changed files with 119 additions and 57 deletions

View File

@@ -94,6 +94,24 @@ this.FileUtils = {
return this._initFileOutputStream(fos, file, modeFlags);
},
/**
* Opens an atomic file output stream for writing.
* @param file
* The file to write to.
* @param modeFlags
* (optional) File open flags. Can be undefined.
* @returns nsIFileOutputStream to write to.
* @note The stream is initialized with the DEFER_OPEN behavior flag.
* See nsIFileOutputStream.
* OpeanAtomicFileOutputStream is generally better than openSafeFileOutputStream
* baecause flushing is not needed in most of the issues.
*/
openAtomicFileOutputStream: function FileUtils_openAtomicFileOutputStream(file, modeFlags) {
var fos = Cc["@mozilla.org/network/atomic-file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
return this._initFileOutputStream(fos, file, modeFlags);
},
/**
* Opens a safe file output stream for writing.
* @param file
@@ -117,6 +135,23 @@ this.FileUtils = {
return fos;
},
/**
* Closes an atomic file output stream.
* @param stream
* The stream to close.
*/
closeAtomicFileOutputStream: function FileUtils_closeAtomicFileOutputStream(stream) {
if (stream instanceof Ci.nsISafeOutputStream) {
try {
stream.finish();
return;
}
catch (e) {
}
}
stream.close();
},
/**
* Closes a safe file output stream.
* @param stream