Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/storage.js

Issue 8402021: Crawler frontend (Closed)
Patch Set: Created Sept. 21, 2012, 1:16 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/storage.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/storage.js
@@ -0,0 +1,45 @@
+Cu.import("resource://gre/modules/FileUtils.jsm");
+
+let outputStream;
+let converterOutputStream;
+
+function createTemporaryFile(name)
+{
+ let file = FileUtils.getFile("TmpD", [name]);
+ file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
+ return file;
+}
+
+function openOutputStream(file)
+{
+ let outputStream = FileUtils.openSafeFileOutputStream(file);
+ let converterOutputStream = Cc["@mozilla.org/intl/converter-output-stream;1"]
+ .createInstance(Ci.nsIConverterOutputStream);
+ converterOutputStream.init(outputStream, "UTF-8", 0, 0);
+ return [outputStream, converterOutputStream];
+}
+
+let Storage = exports.Storage = {};
+
+Storage.init = function()
+{
+ Storage.dataFile = createTemporaryFile("crawler-data");
+ [outputStream, converterOutputStream] = openOutputStream(Storage.dataFile);
+};
+
+Storage.write = function(data)
+{
+ let line = JSON.stringify(data) + "\n";
Wladimir Palant 2012/09/21 15:36:18 Looking at that again - you shouldn't need the con
+ converterOutputStream.writeString(line);
+};
+
+Storage.finish = function()
+{
+ converterOutputStream.flush();
+ FileUtils.closeSafeFileOutputStream(outputStream);
+};
+
+Storage.destroy = function()
+{
+ Storage.dataFile.remove(true);
+};

Powered by Google App Engine
This is Rietveld