FileLoader
TextFileLoader is a implementation of FileLoader for text resources.
Any ASCII/Unicode readable resource is ment as "Text resource". TextFileLoader
is a implementation for those resources and generates a TextFile
implementation with the loaded content.
TextFileFactory allows to generate different TextFile
implementations for the loaded content.
TextFileLoader represents the time consuming part of accessing files
( TextFile is the handleable part} and therefore contains a event system
to add listeners to listen to the concrete events. It is possible to add
listeners using addListener.
Example listener:
import org.as2lib.io.file.AbstractFileLoader;
import org.as2lib.io.file.LoadProgressListener;
import org.as2lib.io.file.LoadStartListener;
import org.as2lib.io.file.LoadCompleteListener;
import org.as2lib.io.file.LoadErrorListener;
import org.as2lib.io.file.FileLoader;
import org.as2lib.io.file.TextFile;
class MyFileListener implements
LoadProgressListener, LoadStartListener,
LoadCompleteListener, LoadErrorListener {
public function onLoadComplete(fileLoader:FileLoader):Void {
var file:TextFile = TextFile(fileLoader.getFile());
if (file != null) {
// Proper file available
} else {
// Wrong event handled
}
}
public function onLoadError(fileLoader:FileLoader, errorCode:String, error):Void {
if (errorCode == AbstractFileLoader.FILE_NOT_FOUND) {
var notExistantUrl = error;
// Use that url
}
}
public function onLoadStart(fileLoader:FileLoader) {
// show that this file just gets loaded
}
public function onLoadProgress(fileLoader:FileLoader) {
// update the percentage display with resourceLoader.getPercentage();
}
}
Example of the usage:
import org.as2lib.io.file.TextFileLoader;
var fileLoader:TextFileLoader = new TextFileLoader();
fileLoader.addListener(new MyFileListener());
fileLoader.load("test.txt");
new TextFileLoader()public function TextFileLoader(textFileFactory:TextFileFactory)
Constructs a new TextFileLoader.
textFileFactory | (optional) TextFileFactory to create theimplementations, SimpleTextFileFactory gets used if no custom TextFileFactory gets passed-in
|
public function load(uri:String, method:String, parameters:Map, callBack:Executable):VoidLoads a certain text file by a http request.
It sends http request by using the passed-in uri, method
and parameters. The responding file will be passed to the set
TextFileFactory.
If you only need to listen if the TextFile finished loading you can
apply a callBack that gets called if the TextFile is loaded.
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.
|
parameters | (optional) parameters for loading the resource |
callBack | (optional) Executable to be executed after the the resource was loaded. |
load() in org.as2lib.io.file.FileLoader
public function getFile(Void):File
Returns the loaded File.
File that has been loaded
| FileNotLoadedException | if the resource has not been loaded yet |
getFile() in org.as2lib.io.file.FileLoader
public function getTextFile(Void):TextFile
Returns the loaded TextFile.
TextFile that has been loaded
| FileNotLoadedException | if the resource has not been loaded yet |
public function getBytesLoaded(Void):ByteReturns 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
getBytesLoaded() in org.as2lib.io.file.FileLoader
public function getBytesTotal(Void):ByteReturns 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
getBytesTotal() in org.as2lib.io.file.FileLoader