單線程加載文件工具類,主要加載txt,xml,swf,png,jpg,gif文件 工具
package tunie.util { import flash.display.Loader; import flash.display.LoaderInfo; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; import flash.system.ApplicationDomain; import flash.system.LoaderContext; import tunie.error.BaseError; import tunie.error.BaseErrorType; /** * Author:Tunie * Date: Jul 9, 2013 11:47:32 AM * Synopsis:單線程加載文件工具類,主要加載txt,xml,swf,png,jpg,gif文件 * <li>addLoadFile:往加載列表中添加欲加載的文件,若沒有處於加載文件狀態,則加載啓動</li> * <li>start:開始加載</li> * <li>stop:中止加載</li> * <li>mode:設置文件加載的模式</li> */ public class LoadFileUtil { /** * 懶惰模式,在這種模式下加載文件時,一旦有文件出錯,或者中止加載,將會終止 * 已經加入加載列表中的欲加載項 */ public static const LAZY:String="lazy"; /** * 貪婪模式,在這種模式下加載文件時,當文件出錯時,將會跳過些文件,繼續加載下 * 一個文件,中止加載時,只是暫停加載而已 */ public static const GREEDY:String="greedy"; private var _mode:String="lazy"; private var _loadFiles:Vector.<LoadFile>; private var _loading:Boolean; /** *用於加載文本之類的文件 */ private var _bitLoader:URLLoader; /** *用於加載圖片之類的文件 */ private var _mapLoader:Loader; public function LoadFileUtil(sign:Sign) { if(!sign) throw new BaseError(BaseErrorType.CANT_INSTANTIATION); _loadFiles=new Vector.<LoadFile>(); _loading=false; } /** * 添加須要加載的文件 * @param url 文件路徑 * @param loadComp 加載完成 * @param loadProgress 加載進度 * @param loadError 加載失敗 * @param vars 參數 * */ public function addLoadFile(url:String,loadComp:Function=null,vars:URLVariables=null,loadProgress:Function=null,loadError:Function=null):void { _loadFiles.push(new LoadFile(url,loadComp,vars,loadError,loadProgress)); start(); } /** * 添加須要加載的文件 * @param url 文件路徑 * @param loadComp 加載完成 * @param loadProgress 加載進度 * @param loadError 加載失敗 * @param vars 參數 * */ public static function addLoadFile(url:String,loadComp:Function=null,vars:URLVariables=null,loadProgress:Function=null,loadError:Function=null):void { instance.addLoadFile(url,loadComp,vars,loadProgress,loadError); } /** *只有是_loading爲true時才能調用 */ private function load():void { if(_loadFiles.length<=0) { stop(); return; } switch(_loadFiles[0].type) { case LoadFile.BIT_TYPE: bitLoad(); break; case LoadFile.MAP_TYPE: mapLoad(); break; default: throw new BaseError(BaseErrorType.DISALLOWED_VALUE); } } private function bitLoad():void { checkBitLoader(); setupEvent(_bitLoader); _bitLoader.load(createRequest()); } private function mapLoad():void { checkMapLoader(); setupEvent(_mapLoader.contentLoaderInfo); _mapLoader.load(createRequest(),new LoaderContext(false,ApplicationDomain.currentDomain)); } private function setupEvent(load:EventDispatcher):void { load.addEventListener(Event.COMPLETE,loadComp); load.addEventListener(ProgressEvent.PROGRESS,loadProgress); load.addEventListener(IOErrorEvent.IO_ERROR,loadError); } private function removeEvent(load:EventDispatcher):void { load.removeEventListener(Event.COMPLETE,loadComp); load.removeEventListener(ProgressEvent.PROGRESS,loadProgress); load.removeEventListener(IOErrorEvent.IO_ERROR,loadError); } private function loadComp(e:Event):void { removeEvent(e.target as EventDispatcher); if(_loadFiles[0].compCallback!=null) { var b_data:Object; if(e.target is LoaderInfo) b_data=e.target.content; else if(e.target is URLLoader) b_data=e.target.data; _loadFiles[0].compCallback.call(null,b_data); } next(); } private function loadProgress(e:ProgressEvent):void { if(_loadFiles[0].progressCallback!=null) { _loadFiles[0].progressCallback.call(null,e); } } private function loadError(e:IOErrorEvent):void { removeEvent(e.target as EventDispatcher); if(_loadFiles[0].errorCallback!=null) { _loadFiles[0].errorCallback.call(null,e); } if(_mode==LAZY) { stop(); } else if(_mode==GREEDY) { next(); } } private function checkBitLoader():void { if(!_bitLoader) _bitLoader=new URLLoader(); else { removeEvent(_bitLoader); _bitLoader.close(); } } private function checkMapLoader():void { if(!_mapLoader) _mapLoader=new Loader(); else { removeEvent(_mapLoader.contentLoaderInfo); try { _mapLoader.close(); } catch(error:Error) { // } } } private function createRequest():URLRequest { var b_loadFile:LoadFile=_loadFiles[0]; var b_path:String=b_loadFile.path; var b_request:URLRequest=new URLRequest(b_path); if(b_loadFile.vars) { b_request.data=b_loadFile.vars; b_request.method=URLRequestMethod.POST; } return b_request; } /** *準備加載下一個 */ private function next():void { _loadFiles.shift(); loading=true; } /** *清空狀態 */ private function clear():void { _loading=false; if(_bitLoader){ removeEvent(_bitLoader); _bitLoader.close(); _bitLoader=null; } if(_mapLoader){ removeEvent(_mapLoader.contentLoaderInfo); try { _mapLoader.close(); } catch(error:Error) { } finally { _mapLoader=null; } } if(_mode==LAZY) { _loadFiles.length=0; } else if(_mode==GREEDY) { } } /** *開始加載 */ public function start():void { if(!_loading) loading=true; } /** *開始加載 */ public static function start():void { instance.start(); } /** *中止加載文件,預備加載的文件都將取消加載 */ public function stop():void { clear(); } /** *中止加載文件,預備加載的文件都將取消加載 */ public static function stop():void { instance.stop(); } /** *加載模式 */ public function set mode(value:String):void { _mode = value; } /** * 加載模式 */ public static function set mode(value:String):void { instance.mode=value; } /** *標記是否處於加載過程當中 */ protected function set loading(value:Boolean):void { _loading = value; if(_loading) { load(); } else stop(); } //------------------------------single pattern private static var _instanse:LoadFileUtil; public static function get instance():LoadFileUtil { if(!_instanse) _instanse=new LoadFileUtil(new Sign()); return _instanse; } } } import flash.net.URLVariables; class Sign{} /** * 加載的文件 * @author Tunie * */ class LoadFile { /** *txt,xml */ public static const BIT_TYPE:String="bitType"; /** *swf,png,jpg,gif */ public static const MAP_TYPE:String="mapType"; public var path:String; public var compCallback:Function; public var errorCallback:Function; public var progressCallback:Function; public var vars:URLVariables; /** *文件類型 */ public var type:String; public function LoadFile(path:String,fComp:Function=null,vars:URLVariables=null,fError:Function=null,fProgress:Function=null) { this.path=path; compCallback=fComp; errorCallback=fError; progressCallback=fProgress; this.vars=vars; analysisPath(); } private function analysisPath():void { var b_index:int=path.lastIndexOf("."); var b_type:String=path.substr(b_index+1).toLowerCase(); switch(b_type) { case "txt": case "xml": type=BIT_TYPE; break; case "swf": case "png": case "jpg": case "gif": type=MAP_TYPE; break; default: type=""; } } }