001 工具类 加载文件工具LoadFileUtil

LoadFileUtil

单线程加载文件工具类,主要加载txt,xml,swf,png,jpg,gif文件

  • addLoadFile:往加载列表中添加欲加载的文件,若没有处于加载文件状态,则加载启动
  • start:开始加载
  • stop:停止加载
  • mode:设置文件加载的模式
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="";
		}
	}
}

你可能感兴趣的:(as3)