/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */constEXPORTED_SYMBOLS=["IOUtils"];var{Services}=ChromeUtils.import("resource://gre/modules/Services.jsm");varkStringBlockSize=4096;varkStreamBlockSize=8192;varIOUtils={/** * Read a file containing ASCII text into a string. * * @param aFile An nsIFile representing the file to read or a string containing * the file name of a file under user's profile. * @returns A string containing the contents of the file, presumed to be ASCII * text. If the file didn't exist, returns null. */loadFileToString(aFile){letfile;if(!(aFileinstanceofCi.nsIFile)){file=Services.dirsvc.get("ProfD",Ci.nsIFile);file.append(aFile);}else{file=aFile;}if(!file.exists()){returnnull;}letfstream=Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);// PR_RDONLYfstream.init(file,0x01,0,0);letsstream=Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);sstream.init(fstream);letdata="";while(sstream.available()){data+=sstream.read(kStringBlockSize);}sstream.close();fstream.close();returndata;},/** * Save a string containing ASCII text into a file. The file will be overwritten * and contain only the given text. * * @param aFile An nsIFile representing the file to write or a string containing * the file name of a file under user's profile. * @param aData The string to write. * @param aPerms The octal file permissions for the created file. If unset * the default of 0o600 is used. */saveStringToFile(aFile,aData,aPerms=0o600){letfile;if(!(aFileinstanceofCi.nsIFile)){file=Services.dirsvc.get("ProfD",Ci.nsIFile);file.append(aFile);}else{file=aFile;}letfoStream=Cc["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);// PR_WRONLY + PR_CREATE_FILE + PR_TRUNCATEfoStream.init(file,0x02|0x08|0x20,aPerms,0);// safe-file-output-stream appears to throw an error if it doesn't write everything at once// so we won't worry about looping to deal with partial writes.// In case we try to use this function for big files where buffering// is needed we could use the implementation in saveStreamToFile().foStream.write(aData,aData.length);foStream.QueryInterface(Ci.nsISafeOutputStream).finish();foStream.close();},/** * Saves the given input stream to a file. * * @param aIStream The input stream to save. * @param aFile The file to which the stream is saved. * @param aPerms The octal file permissions for the created file. If unset * the default of 0o600 is used. */saveStreamToFile(aIStream,aFile,aPerms=0o600){if(!(aIStreaminstanceofCi.nsIInputStream)){thrownewError("Invalid stream passed to saveStreamToFile");}if(!(aFileinstanceofCi.nsIFile)){thrownewError("Invalid file passed to saveStreamToFile");}letfstream=Cc["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);letbuffer=Cc["@mozilla.org/network/buffered-output-stream;1"].createInstance(Ci.nsIBufferedOutputStream);// Write the input stream to the file.// PR_WRITE + PR_CREATE + PR_TRUNCATEfstream.init(aFile,0x04|0x08|0x20,aPerms,0);buffer.init(fstream,kStreamBlockSize);buffer.writeFrom(aIStream,aIStream.available());// Close the output streams.if(bufferinstanceofCi.nsISafeOutputStream){buffer.finish();}else{buffer.close();}if(fstreaminstanceofCi.nsISafeOutputStream){fstream.finish();}else{fstream.close();}// Close the input stream.aIStream.close();returnaFile;},/** * Returns size of system memory. */getPhysicalMemorySize(){returnServices.sysinfo.getPropertyAsInt64("memsize");},};