Загрузка двоичных данных

Код AS3:
package lib_loader
{
import flash.net.URLRequest;
import flash.display.Loader;
import flash.net.URLRequestMethod;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.errors.IOError;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.ErrorEvent;
public class LibContentLoader implements ILibLoader
{
// Адрес, откуда необходимо загрузить компонент
private var urlFrom:String;
// Передаваемые параметры методом POST
private var dataFrom:Object;
// Метод, выполняемый при наступлении события успешной инициализации загрузки
private var eventFunctionComplit:Function;
// Метод, выполняемый во время загрузки
private var eventFunctionProgress:Function;
// Метод, выполняемый при ошибке загрузки
private var eventFunctionError:Function;
// Метод, выполняемый при отмене загрузки
private var eventFunctionUndo:Function;
// Метод, выполняемый при наступлении события успешной загрузке
private var eventFunctionFinal:Function;
// Флаг выполнения загрузки
private var eventListener:Boolean = false;
// Объект передаваемых параметров загрузки
private var requestLoader:URLRequest;
// Объект загрузки
private var objectLoader:Loader;
// Флаг отмены загрузки
private var flagUnload:Boolean = false;
// Флаг уничтожения объекта по причине ошибки
private var destroy:Boolean = true;
/*////////////////////////////////////////////////////////////
Загрузка двоичных данных, таких как изображения
////////////////////////////////////////////////////////////*/
public function LibContentLoader()
{
// constructor code
}
// Установить параметры объекта
public function set SetParam(paramObject:Object):void
{
if(paramObject is Object)
{
// Адрес, откуда необходимо загрузить компонент
if(paramObject.urlLoc is String)
{
this.urlFrom = paramObject.urlLoc;
}
// Передаваемые параметры методом POST
if(paramObject.dataLoc is Object)
{
this.dataFrom = paramObject.dataLoc;
}
// Метод, выполняемый при наступлении события успешной инициализации загрузки
if(paramObject.functionComplitLoc is Function)
{
this.eventFunctionComplit = paramObject.functionComplitLoc;
}
// Метод, выполняемый при наступлении события успешной загрузке
if(paramObject.functionFinalLoc is Function)
{
this.eventFunctionFinal = paramObject.functionFinalLoc;
}
// Метод, выполняемый во время загрузки
if(paramObject.functionProgressLoc is Function)
{
this.eventFunctionProgress = paramObject.functionProgressLoc;
}
// Метод, выполняемый при ошибке загрузки
if(paramObject.functionErrorLoc is Function)
{
this.eventFunctionError = paramObject.functionErrorLoc;
}
// Метод, выполняемый при отмене загрузки
if(paramObject.functionUndoLoc is Function)
{
this.eventFunctionUndo = paramObject.functionUndoLoc;
}
// Начать/остановить загрузку true/false
if(paramObject.startLoc is Boolean)
{
if(this.eventListener && !paramObject.startLoc)
{
this.flagUnload = true;
this.objectLoader.unload();
}
this.EventListener = paramObject.startLoc;
}
// Флаг уничтожение объекта по причине ошибки
if (paramObject.destroyLoc is Boolean)
{
this.destroy = paramObject.destroyLoc;
}
} // Конец объекта
} // Конец метода установки параметров
// Подключение или отключение слушателей событий
private function set EventListener(flagEvent:Boolean):void
{
if(this.eventListener != flagEvent)
{
if(this.eventListener = flagEvent)
{
// Подключение слушателей событий
this.AddEventListener();
}
else
{
// Отключение слушателей событий
this.RemoveEventListener();
}
}
}
private function Builder():void
{
this.requestLoader = new URLRequest();
this.objectLoader = new Loader();
}
private function Destroy():void
{
this.EventListener = false;
this.objectLoader = null;
this.requestLoader = null;
this.urlFrom = null;
this.dataFrom = null;
this.eventFunctionUndo = null;
this.eventFunctionComplit = null;
//this.eventFunctionFinal = null;
this.eventFunctionError = null;
}
private function AddEventListener():void
{
this.Builder();
if(this.dataFrom is Object)
{
this.requestLoader.method = URLRequestMethod.POST;
this.requestLoader.data = this.dataFrom;
}
if((this.urlFrom is String) && this.urlFrom.length)
{
this.requestLoader.url = this.urlFrom;
try
{
this.objectLoader.load(this.requestLoader);
}
catch (e:ArgumentError)
{
this.FunctionError(e.message);
}
catch (e:SecurityError)
{
this.FunctionError(e.message);
}
catch (e:IOError)
{
this.FunctionError(e.message);
}
catch (e:Error)
{
this.FunctionError(e.message);
}
finally
{
}
this.objectLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR , EventFunctionError);
this.objectLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , EventFunctionError);
this.objectLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , EventFunctionProgress);
this.objectLoader.contentLoaderInfo.addEventListener(Event.INIT , EventFunctionInit);
}
else
{
var e:Error = new Error('url is null');
this.FunctionError(e.message);
}
}
private function RemoveEventListener():void
{
this.objectLoader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR , EventFunctionError);
this.objectLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR , EventFunctionError);
this.objectLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS , EventFunctionProgress);
this.objectLoader.contentLoaderInfo.removeEventListener(Event.INIT , EventFunctionInit);
this.objectLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE , EventFunctionComplete);
if(this.flagUnload)
this.objectLoader.contentLoaderInfo.addEventListener(Event.UNLOAD , EventFunctionUndo);
}
////////////////////////////////////////////////////////////
private function EventFunctionUndo(e:Event):void
{
this.flagUnload = false;
this.objectLoader.contentLoaderInfo.removeEventListener(Event.UNLOAD , EventFunctionUndo);
if (this.eventFunctionUndo is Function) this.eventFunctionUndo();
this.Destroy();
}
///////////////////////////////////////////////////////////
private function EventFunctionComplete(e:Event):void
{
if(this.eventFunctionFinal is Function) this.eventFunctionFinal();
this.Destroy();
}
// Инициализация прошла успешно
private function EventFunctionInit(e:Event):void
{
if(this.eventFunctionComplit is Function)
{
try
{
this.eventFunctionComplit(e.target.content);
this.objectLoader.contentLoaderInfo.addEventListener(Event.COMPLETE , EventFunctionComplete);
}
catch (t:ArgumentError)
{
this.FunctionError(t.message);
}
catch (t:SecurityError)
{
this.FunctionError(t.message);
}
catch (t:IOError)
{
this.FunctionError(t.message);
}
catch (t:Error)
{
this.FunctionError(t.message);
}
finally
{
}
}
else
{
var err:Error = new Error('function eventFunctionComplit not found');
this.FunctionError(err.message);
}
}
private function EventFunctionProgress(e:ProgressEvent):void
{
if(this.eventFunctionProgress is Function)
{
const thisLoaded:Number = Math.round((e.bytesLoaded/e.bytesTotal) * 1000)/10;
this.eventFunctionProgress(thisLoaded);
}
}
private function EventFunctionError(e:ErrorEvent):void
{
this.FunctionError(e.text);
}
private function FunctionError(err:String):void
{
this.EventListener = false;
if(this.eventFunctionError is Function) this.eventFunctionError(err);
//this.Destroy();
if (this.destroy)
{
this.Destroy();
}
}
} // Конец класса
} // Конец пакета
Загрузка текстовых данных

Код AS3:
package lib_loader
{
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.errors.IOError;
import flash.events.ProgressEvent;
import flash.events.Event;
import flash.events.ErrorEvent;
public class LibDataLoader implements ILibLoader
{
// Адрес, откуда необходимо загрузить компонент
private var urlFrom:String;
// Передаваемые параметры методом POST
private var dataFrom:Object;
// Метод, выполняемый при наступлении события успешной загрузки
private var eventFunctionComplit:Function;
// Метод, выполняемый во время загрузки
private var eventFunctionProgress:Function;
// Метод, выполняемый при ошибке загрузки
private var eventFunctionError:Function;
// Метод, выполняемый при отмене загрузки
private var eventFunctionUndo:Function;
// Флаг выполнения загрузки
private var eventListener:Boolean = false;
// Объект передаваемых параметров загрузки
private var requestLoader:URLRequest;
// Объект загрузки
private var objectLoader:URLLoader;
// Флаг уничтожения объекта по причине ошибки
private var destroy:Boolean = true;
/*////////////////////////////////////////////////////////////
Загрузка данных, таких как XML
////////////////////////////////////////////////////////////*/
public function LibDataLoader()
{
// constructor code
}
// Установить параметры объекта
public function set SetParam(paramObject:Object):void
{
if(paramObject is Object)
{
// Адрес, откуда необходимо загрузить компонент
if(paramObject.urlLoc is String)
{
this.urlFrom = paramObject.urlLoc;
}
// Передаваемые параметры методом POST
if(paramObject.dataLoc is Object)
{
this.dataFrom = paramObject.dataLoc;
}
// Метод, выполняемый при наступлении события успешной загрузки
if(paramObject.functionComplitLoc is Function)
{
this.eventFunctionComplit = paramObject.functionComplitLoc;
}
// Метод, выполняемый во время загрузки
if(paramObject.functionProgressLoc is Function)
{
this.eventFunctionProgress = paramObject.functionProgressLoc;
}
// Метод, выполняемый при ошибке загрузки
if(paramObject.functionErrorLoc is Function)
{
this.eventFunctionError = paramObject.functionErrorLoc;
}
// Метод, выполняемый при отмене загрузки
if(paramObject.functionUndoLoc is Function)
{
this.eventFunctionUndo = paramObject.functionUndoLoc;
}
// Начать загрузку true
if(paramObject.startLoc is Boolean)
{
this.EventListener = paramObject.startLoc;
}
// Флаг уничтожение объекта по причине ошибки
if (paramObject.destroyLoc is Boolean)
{
this.destroy = paramObject.destroyLoc;
}
} // Конец объекта
} // Конец метода установки параметров
private function Builder():void
{
this.requestLoader = new URLRequest();
this.objectLoader = new URLLoader();
this.objectLoader.dataFormat = URLLoaderDataFormat.BINARY;
}
private function Destroy():void
{
this.EventListener = false;
this.objectLoader = null;
this.requestLoader = null;
this.urlFrom = null;
this.dataFrom = null;
}
// Подключение или отключение слушателей событий
private function set EventListener(flagEvent:Boolean):void
{
if(this.eventListener != flagEvent)
{
if(this.eventListener = flagEvent)
{
// Подключение слушателей событий
this.AddEventListener();
}
else
{
// Отключение слушателей событий
this.RemoveEventListener();
}
}
}
private function AddEventListener():void
{
this.Builder();
if(this.dataFrom is Object)
{
this.requestLoader.method = URLRequestMethod.POST;
this.requestLoader.data = this.dataFrom;
}
if((this.urlFrom is String) && this.urlFrom.length)
{
this.requestLoader.url = this.urlFrom; // +'?' + Math.random();
try
{
this.objectLoader.load(this.requestLoader);
}
catch (e:ArgumentError)
{
this.FunctionError(e.message);
}
catch (e:SecurityError)
{
this.FunctionError(e.message);
}
catch (e:IOError)
{
this.FunctionError(e.message);
}
catch (e:Error)
{
this.FunctionError(e.message);
}
this.objectLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR , EventFunctionError);
this.objectLoader.addEventListener(IOErrorEvent.IO_ERROR , EventFunctionError);
this.objectLoader.addEventListener(ProgressEvent.PROGRESS , EventFunctionProgress);
this.objectLoader.addEventListener(Event.COMPLETE , EventFunctionComplit);
}
else
{
var e:Error = new Error('url is null');
this.FunctionError(e.message);
}
}
private function RemoveEventListener():void
{
this.objectLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR , EventFunctionError);
this.objectLoader.removeEventListener(IOErrorEvent.IO_ERROR , EventFunctionError);
this.objectLoader.removeEventListener(ProgressEvent.PROGRESS , EventFunctionProgress);
this.objectLoader.removeEventListener(Event.COMPLETE , EventFunctionComplit)
}
////////////////////////////////////////////////////////////
private function EventFunctionComplit(e:Event):void
{
if(this.eventFunctionComplit is Function)
{
this.eventFunctionComplit(e.target.data);
}
this.Destroy();
}
private function EventFunctionProgress(e:ProgressEvent):void
{
if(this.eventFunctionProgress is Function)
{
const thisLoaded:Number = Math.round((e.bytesLoaded/e.bytesTotal) * 1000)/10;
this.eventFunctionProgress(thisLoaded);
}
}
private function EventFunctionError(e:ErrorEvent):void
{
this.FunctionError(e.text);
}
private function FunctionError(err:String):void
{
if (this.eventFunctionError is Function)
{
this.eventFunctionError(err);
}
if (this.destroy)
{
this.Destroy();
}
}
} // Конец класса
} // Конец пакета
Интерфейс:

Код AS3:
package lib_loader
{
public interface ILibLoader
{
// Interface methods:
// Установить параметры объекта
function set SetParam(paramObject:Object):void;
}
}
Добавлено через 20 минут
Инструкция для применения:

Код:
private var libDataLoader:ILibLoader;
private var libContentLoader:ILibLoader;
this.libDataLoader = new LibDataLoader();
// Данные, доступные после полной загрузки
this.libDataLoader.SetParam =
{
urlLoc:url , // Адрес загружаемых данных
dataLoc: Object , // Данные, передаваемые методом POST
functionComplitLoc: Function(e:*):void , // Загрузка завершена
functionErrorLoc: Function(error:String):void , // Ошибка при загрузке
functionProgressLoc: Function(uint:uint):void , // Прогресс загрузки
startLoc:true // Стартовать загрузку
}
this.libContentLoader = new LibContentLoader();
// Данные, доступные в процессе загрузки, после их инициализации
this.libContentLoader.SetParam =
{
urlLoc:url , // Адрес загружаемого объекта
dataLoc: Object // Данные, передаваемые методом POST
functionFinalLoc: Function(e:*):void , // Контент готов для добавления в список отображения
functionComplitLoc: Function():void, // Загрузка завершена
functionErrorLoc: Function(error:String):void , // Ошибка при загрузке
functionProgressLoc: Function(Number:Number):void , // Прогресс загрузки
functionUndoLoc: Function():void ,// Отмена загрузки
startLoc:true // Стартовать загрузку
}