MozillaZine

Why does writting to hard drive drops characters

Talk about add-ons and extension development.
nickhere
 
Posts: 27
Joined: September 29th, 2009, 1:57 pm

Post Posted October 22nd, 2018, 5:53 pm

I can write to console log but when i write to hard drive the data dont match
Code: Select all
Components.utils.import('resource://gre/modules/FileUtils.jsm');
Components.utils.import('resource://gre/modules/NetUtil.jsm');
Components.utils.import('resource://gre/modules/osfile.jsm')
function checkSubFolders(aFolder) {
  if (!aFolder.hasSubFolders) {
    return;
  }
  var enumerator = aFolder.subFolders;
  while (enumerator.hasMoreElements()) {
    var folder = enumerator.getNext().QueryInterface(Components.interfaces.nsIMsgFolder);
    writeFile(file, folder.prettyName + '\n', false);
    //console.log(folder.prettyName);
    if (!(folder instanceof Components.interfaces.nsIMsgNewsFolder)) {
      var folders = GetSelectedMsgFolders();
      for (var i = 0; i < folders.length; i++) {
        var enumerator1 = folders[i].messages;
        while (enumerator1.hasMoreElements()) {
          var hdr = enumerator1.getNext().QueryInterface(Components.interfaces.nsIMsgDBHdr);
          console.log(hdr.folder.name + " " +  hdr.mime2DecodedSubject +" " + hdr.mime2DecodedAuthor);
         
        }
      }
    }
    checkSubFolders(folder);
  }
}
function writeFile(nsiFile, data, overwrite) {
  //overwrite is true false, if false then it appends
  //nsiFile must be nsiFile
  if (overwrite) {
    var openFlags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
  } else {
    var openFlags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_APPEND;
  } //data is data you want to write to file
  //if file doesnt exist it is created
 
  console.log(data);
  var ostream = FileUtils.openFileOutputStream(nsiFile, openFlags)
  var converter = Cc['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Ci.nsIScriptableUnicodeConverter);
  converter.charset = 'UTF-8';
  var istream = converter.convertToInputStream(data);
  NetUtil.asyncCopy(istream, ostream);

 }
 
 
  var acctMgr = Components.classes['@mozilla.org/messenger/account-manager;1'].getService(Components.interfaces.nsIMsgAccountManager);
  var accounts = acctMgr.accounts;
  var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsIFile);
  file.initWithPath('d:\\ttt.txt');
  for (var i = 0; i < accounts.length; i++) {
    var account = accounts.queryElementAt(i, Components.interfaces.nsIMsgAccount);
    //writeFile(file,account.incomingServer.prettyName + "\n",false);
    //console.log(account.incomingServer.prettyName);
    checkSubFolders(account.incomingServer.rootFolder);
  }


nickhere
 
Posts: 27
Joined: September 29th, 2009, 1:57 pm

Post Posted October 24th, 2018, 9:40 am

has anybody check this on there machine.
the output to disk drop characters
the instream log is fine the write to hard drive is not (ostream)

nickhere
 
Posts: 27
Joined: September 29th, 2009, 1:57 pm

Post Posted October 29th, 2018, 7:55 pm

I fixed it by getting rid of the async
Code: Select all
function writeFile(nsiFile, data, overwrite) {
  //overwrite is true false, if false then it appends
  //nsiFile must be nsiFile
  if (overwrite) {
    var openFlags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE;
  } else {
    var openFlags = FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_APPEND;
  } //data is data you want to write to file
  //if file doesnt exist it is created
  var ostream = FileUtils.openFileOutputStream(nsiFile, openFlags);
  ostream.write(data,data.length);
  ostream.close();
 

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 1 guest