As3 self-Writing sort notes: byteloader class

Source: Internet
Author: User

Index.base.net. byteloader class description:
Basic functions: loading images and SwF by byte

Constructor
Public Function byteloader (URL: String = "")
If the parameter URL is input, load it immediately!

Load Method
Public Function load (_ URL: string): void
Start loading. _ URL is the loading address.

Updata data update Method
Public Function updata (): void
Updates the readable bytes of the buffer.

Close Method
Public Function close (): void
Class, clear all useless data, can also be used to forcibly disable data streams, stop download

Data attributes
Public var data: bytearray
Returns the loaded bytes.

URL attributes
Public var URL: String
Returns the loaded URL.

Isload attribute (read-only)
Public Function get isload (): Boolean
Whether data is being loaded

Progressevent. Progress event
Scheduling during loading with loading

Event. Complete Event
Load finished Scheduling

Example:

Import index.base.net. byteloader;

VaR Bl: byteloader = new byteloader;
BL. Load ("http://www.xiaos8.com/uploads/pro/50preso3a2.swf ");
BL. addeventlistener (event. Complete, completefun );
BL. addeventlistener (progressevent. Progress, progressfun );

Function completefun (E: Event): void {
VaR Loader: loader = new loader;
Loader. loadbytes (BL. data );
Addchild (loader );
BL. removeeventlistener (event. Complete, completefun );
BL. removeeventlistener (progressevent. Progress, progressfun );
BL. Close ();
BL = NULL;
}

Function progressfun (E: progressevent): void {
Trace (E. bytesloaded );
// If the JPEG image is in progressive format, read the byte when this event is published and load it with loader. loadbytes to form an edge loading and display.
}
Source code:

CopyCode The Code is as follows: Package index.base.net {

Import flash. Events. eventdispatcher;
Import flash. Events. progressevent;
Import flash. Events. event;
Import flash. utils. bytearray;
Import flash.net. urlstream;
Import flash.net. URLRequest;

Public class byteloader extends eventdispatcher {

Public var URL: string;
Public var data: bytearray;
Private var stream: urlstream;

Public Function byteloader (URL: String = ""){
If (URL! = ""){
Load (URL );
}
}

// Load
Public Function load (_ URL: string): void {
Url = _ URL;
Data = new bytearray;
Stream = new urlstream;
Stream. Load (New URLRequest (URL ));
Stream. addeventlistener (event. Complete, completefun );
Stream. addeventlistener (progressevent. Progress, progressfun );
}

// Loading
Private function progressfun (E: progressevent): void {
If (stream. bytesavailable = 0) return;
Updata ();
Dispatchevent (E );
}

// Loading completed
Private function completefun (E: Event): void {
Stream. removeeventlistener (event. Complete, completefun );
Stream. removeeventlistener (progressevent. Progress, progressfun );
Updata ();
If (isload) stream. Close ();
Dispatchevent (E );
}

// Update data
Public Function updata (): void {
If (isload) stream. readbytes (data, data. Length );
}

// Clear data
Public Function close (): void {
If (isload) stream. Close ();
Stream = NULL;
Data = NULL;
}

// Obtain whether data is being loaded
Public Function get isload (): Boolean {
If (Stream = NULL) return false;
Return stream. connected;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.