FileLoader
is built to handle the loading of a external files.
A FileLoader
allows to get a resource and create a representation
of the certain file to allow proper access.
It is build to handle the loading of one file. But it can be executed twice or more often. This is because its possible to handle more than one request parallel in any system.
Example to handle the loading of a resource:
import org.as2lib.io.file.AbstractFileLoader;
import org.as2lib.io.file.FileLoader;
import org.as2lib.io.file.LoadStartListener;
import org.as2lib.io.file.LoadCompleteListener;
import org.as2lib.io.file.LoadProgressListener;
import org.as2lib.io.file.LoadErrorListener;
class Main implements
LoadStartListener,
LoadCompleteListener,
LoadErrorListener,
LoadProgressListener {
public function main(loader:FileLoader):Void {
loader.addListener(this);
loader.load("test.txt");
}
public function onLoadComplete(loader:FileLoader):Void {
var resource = loader.getFile();
// Do anything you like....
}
public function onLoadError(loader:FileLoader, errorCode:String, error):Boolean {
if (errorCode == AbstractFileLoader.FILE_NOT_FOUND_ERROR) {
trace("Resource could not be found"+error);
}
return false
}
public function onLoadProgress(loader:FileLoader):Void {
trace("loaded: "+loader.getPercentage()+"% of "+loader.getUri());
}
public function onLoadStart(loader:FileLoader):Void {
trace("started loading: "+loader.getUri());
}
}
public function load(uri:String, method:String, params:Map, callBack:Executable):Void
Loads a certain resource.
It sends http request by using the passed-in uri
, method
and parameters
.
If you only need to listen if the File
finished loading you can
apply a callBack
that gets called if the File
is loaded.
Example of using the callback:
import org.as2lib.io.file.FileLoader;
import org.as2lib.app.exec.Call;
class Main {
public function main(loader:FileLoader) {
loader.load("test.txt", null, null, new Call(this, resource);
}
public function finish(resource:Resource):Void {
// Processing the resource ...
}
}
uri | location of the resource to load |
method | (optional) POST/GET as method for submitting the parameters,
default method used if method was not passed-in is POST.
|
callBack | (optional) Executable to be executed after the the resource was loaded. |
public function getUri(Void):String
Returns the URI of the resource that was requested to load.
URI of the resource to load
public function getParameterSubmitMethod(Void):String
Returns the method
to pass request parameters for request.
method to pass request parameters
public function getParameters(Void):Map
Sets the parameters
for the request to the resource.
Returns null
if no parameters has been set.
parameters to be passed with the resource request
public function getFile(Void):File
Returns the loaded file.
the loaded file
org.as2lib.io.file.ResourceNotLoadedException | if the resource has not been loaded yet |
public function getPercentage(Void):Number
Returns the percentage of the execution of null
if its not evaluable.
public function getBytesLoaded(Void):Byte
Returns the total amount of bytes that has been loaded.
Returns null
if its not possible to get the loaded bytes.
amount of bytes that has been loaded
public function getBytesTotal(Void):Byte
Returns the total amount of bytes that will approximately be loaded.
Returns null
if its not possible to get the total amount of bytes.
amount of bytes to load
public function getTransferRate(Void):Bit
Returns the current transfer rate for the execution.
transfer rate in bit (per second)
public function getEstimatedRestTime(Void):Time
Estimates the approximate time until the resource was loaded.
estimated time until finish of loading
public function getEstimatedTotalTime(Void):Time
Estimates the approximate time for the complete loading.
estimated duration at the end of the loading
public function getDuration(Void):Time
Returns the duration it loads the certain resource.
time difference between start time and end time/current time.