Skip to content

Instantly share code, notes, and snippets.

@shuhankuang
Created November 20, 2013 05:37
Show Gist options
  • Save shuhankuang/7558275 to your computer and use it in GitHub Desktop.
Save shuhankuang/7558275 to your computer and use it in GitHub Desktop.
air 下载然后缓存文件
public static function HasCache(CacheKey:String):Boolean {
var f:File = File.applicationStorageDirectory.resolvePath("cache/" + CacheKey);
return f.exists;
f = null;
}
public static function PutInCache(CacheKey:String, data:ByteArray):void {
var f:File = File.applicationStorageDirectory.resolvePath("cache/" + CacheKey);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeBytes(data, 0, data.bytesAvailable);
fs.close();
f = null;
fs = null;
}
public static function PutInCacheUTF(CacheKey:String, data:String):void {
var f:File = File.applicationStorageDirectory.resolvePath("cache/" + CacheKey);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeUTFBytes(data);
fs.close();
f = null;
fs = null;
}
public static function PutInCacheEncodedImage(CacheKey:String, bitmapData:BitmapData, quality:uint=80):void {
var data:ByteArray = new JPGEncoder(quality).encode(bitmapData);
var f:File = File.applicationStorageDirectory.resolvePath("cache/" + CacheKey);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeBytes(data, 0, data.bytesAvailable);
fs.close();
data = null;
f = null;
fs = null;
}
public static function GetFromCache(CacheKey:String):ByteArray {
var data:ByteArray = new ByteArray();
var f:File = File.applicationStorageDirectory.resolvePath("cache/" + CacheKey);
trace("Getting from cache: " + f.url);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.READ);
fs.readBytes(data, 0, fs.bytesAvailable);
fs.close();
f = null;
fs = null;
return data;
}
public static function GetFromCacheUTF(CacheKey:String):String {
var data:String = "";
var f:File = File.applicationStorageDirectory.resolvePath("cache/" + CacheKey);
trace("Getting from cache: " + f.url);
var fs:FileStream = new FileStream();
fs.open(f, FileMode.READ);
data = fs.readUTFBytes(fs.bytesAvailable);
fs.close();
f = null;
fs = null;
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment