OLD | NEW |
(Empty) | |
| 1 Cu.import("resource://gre/modules/FileUtils.jsm"); |
| 2 |
| 3 let outputStream; |
| 4 let converterOutputStream; |
| 5 |
| 6 function createTemporaryFile(name) |
| 7 { |
| 8 let file = FileUtils.getFile("TmpD", [name]); |
| 9 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); |
| 10 return file; |
| 11 } |
| 12 |
| 13 function openOutputStream(file) |
| 14 { |
| 15 let outputStream = FileUtils.openSafeFileOutputStream(file); |
| 16 let converterOutputStream = Cc["@mozilla.org/intl/converter-output-stream;1"] |
| 17 .createInstance(Ci.nsIConverterOutputStream); |
| 18 converterOutputStream.init(outputStream, "UTF-8", 0, 0); |
| 19 return [outputStream, converterOutputStream]; |
| 20 } |
| 21 |
| 22 let Storage = exports.Storage = {}; |
| 23 |
| 24 Storage.init = function() |
| 25 { |
| 26 Storage.dataFile = createTemporaryFile("crawler-data"); |
| 27 [outputStream, converterOutputStream] = openOutputStream(Storage.dataFile); |
| 28 }; |
| 29 |
| 30 Storage.write = function(data) |
| 31 { |
| 32 let line = JSON.stringify(data) + "\n"; |
| 33 converterOutputStream.writeString(line); |
| 34 }; |
| 35 |
| 36 Storage.finish = function() |
| 37 { |
| 38 converterOutputStream.flush(); |
| 39 FileUtils.closeSafeFileOutputStream(outputStream); |
| 40 }; |
| 41 |
| 42 Storage.destroy = function() |
| 43 { |
| 44 Storage.dataFile.remove(true); |
| 45 }; |
OLD | NEW |