ResourceCache.as 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (c) 2009, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.air.net
  28. {
  29. import com.adobe.crypto.MD5;
  30. import com.adobe.net.DynamicURLLoader;
  31. import flash.events.Event;
  32. import flash.events.EventDispatcher;
  33. import flash.events.IOErrorEvent;
  34. import flash.filesystem.File;
  35. import flash.filesystem.FileMode;
  36. import flash.filesystem.FileStream;
  37. import flash.net.URLLoaderDataFormat;
  38. import flash.net.URLRequest;
  39. import flash.utils.ByteArray;
  40. import com.adobe.air.net.events.ResourceCacheEvent;
  41. //todo: add event metadata
  42. public class ResourceCache extends EventDispatcher
  43. {
  44. private var _cacheName:String;
  45. //maybe rename to make it clearer it loads data
  46. public function ResourceCache(cacheName:String)
  47. {
  48. _cacheName = cacheName;
  49. }
  50. public function get cacheName():String
  51. {
  52. return _cacheName;
  53. }
  54. private function getStorageDir():File
  55. {
  56. return File.applicationStorageDirectory.resolvePath(_cacheName);
  57. }
  58. public function itemExists(key:String):Boolean
  59. {
  60. return getItemFile(key).exists;
  61. }
  62. public function clearCache():void
  63. {
  64. var cacheDir:File = getStorageDir();
  65. try
  66. {
  67. cacheDir.deleteDirectory(true);
  68. }
  69. catch (e:IOErrorEvent)
  70. {
  71. // we tried!
  72. }
  73. }
  74. public function getItemFile(key:String):File
  75. {
  76. var dir:File = getStorageDir();
  77. var fName:String = generateKeyHash(key);
  78. var file:File = dir.resolvePath(fName);
  79. return file;
  80. }
  81. public function retrieve(url:String):void
  82. {
  83. var key:String = generateKeyHash(url);
  84. var file:File = getItemFile(key);
  85. //todo: do we need to check if the dir exists?
  86. if(file.exists)
  87. {
  88. var e:ResourceCacheEvent = new ResourceCacheEvent(ResourceCacheEvent.ITEM_READY);
  89. e.key = key;
  90. e.file = file;
  91. dispatchEvent(e);
  92. return;
  93. }
  94. var loader:DynamicURLLoader = new DynamicURLLoader();
  95. loader.file = file;
  96. loader.key = key;
  97. loader.addEventListener(Event.COMPLETE, onDataLoad);
  98. loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
  99. loader.dataFormat = URLLoaderDataFormat.BINARY;
  100. loader.load(new URLRequest(url));
  101. }
  102. private function onLoadError(event:IOErrorEvent):void
  103. {
  104. trace("onLoadError : could not cache item");
  105. }
  106. private function onDataLoad(event:Event):void
  107. {
  108. var loader:DynamicURLLoader = DynamicURLLoader(event.target);
  109. var f:File = File(loader.file);
  110. var key:String = String(loader.key);
  111. var fileStream:FileStream = new FileStream();
  112. fileStream.open(f, FileMode.WRITE);
  113. fileStream.writeBytes(loader.data as ByteArray);
  114. fileStream.close();
  115. var g:ResourceCacheEvent = new ResourceCacheEvent(ResourceCacheEvent.ITEM_CACHED);
  116. g.key = key;
  117. g.file = f;
  118. dispatchEvent(g);
  119. var e:ResourceCacheEvent = new ResourceCacheEvent(ResourceCacheEvent.ITEM_READY);
  120. e.key = key;
  121. e.file = f;
  122. dispatchEvent(e);
  123. }
  124. private function generateKeyHash(key:String):String
  125. {
  126. return MD5.hash(key);
  127. }
  128. }
  129. }