home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / filC5FBE547B3B4874572A8DCB7B5CEC060 < prev    next >
Text File  |  2010-07-12  |  2KB  |  82 lines

  1. var G_CacheWrapper = {
  2.   _clientID: "YandexCacheWrapper",
  3.   _expirationTime: 55,
  4.   
  5.   _cacheSession: null,
  6.   
  7.   get cacheSession() {
  8.     if (!this._cacheSession) {
  9.       const CACHE_SERVICE = Cc["@mozilla.org/network/cache-service;1"].getService(Ci.nsICacheService);
  10.       this._cacheSession = CACHE_SERVICE.createSession(this._clientID, Ci.nsICache.STORE_ANYWHERE, true);
  11.       this._cacheSession.doomEntriesIfExpired = true;
  12.     }
  13.     return this._cacheSession;
  14.   },
  15.   
  16.   _openCacheEntry: function(aKey, aAccess) {
  17.     try {
  18.       if (this.cacheSession)
  19.         return this.cacheSession.openCacheEntry(aKey, aAccess, true);
  20.     } catch(e) {}
  21.     
  22.     return null;
  23.   },
  24.   
  25.   _doomEntry: function(aKey) {
  26.     var entry = this._openCacheEntry(aKey, Ci.nsICache.ACCESS_WRITE);
  27.     if (entry) {
  28.       entry.doom();
  29.       entry.close();
  30.     }
  31.   },
  32.   
  33.   writeData: function(aKey, aData) {
  34.     var result = false;
  35.     
  36.     this._doomEntry(aKey);
  37.     
  38.     if (aData) {
  39.       var entry = this._openCacheEntry(aKey, Ci.nsICache.ACCESS_READ_WRITE);
  40.       if (entry && entry.accessGranted == Ci.nsICache.ACCESS_WRITE) {
  41.         var data = UConverter.ConvertFromUnicode(aData);
  42.         
  43.         var entryOutputStream = entry.openOutputStream(0);
  44.         entryOutputStream.write(data, data.length);
  45.         entryOutputStream.close();
  46.         
  47.         entry.setExpirationTime(parseInt(Date.now() / 1000, 10) + this._expirationTime);
  48.         entry.markValid();
  49.         
  50.         result = true;
  51.       }
  52.       
  53.       entry.close();
  54.     }
  55.     
  56.     return result;
  57.   },
  58.   
  59.   readData: function(aKey) {
  60.     var result = null;
  61.     
  62.     var entry = this._openCacheEntry(aKey, Ci.nsICache.ACCESS_READ);
  63.     
  64.     if (entry) {
  65.       var entryInputStream = entry.openInputStream(0);
  66.       
  67.       var binaryInputStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
  68.       binaryInputStream.setInputStream(entryInputStream);
  69.       
  70.       var data = binaryInputStream.readBytes(binaryInputStream.available());
  71.       
  72.       entryInputStream.close();
  73.       binaryInputStream.close();
  74.       
  75.       entry.close();
  76.       
  77.       result = UConverter.ConvertToUnicode(data);
  78.     }
  79.     
  80.     return result;
  81.   }
  82. }