Analysis of Baidu Library Technology

Source: Internet
Author: User

Recently, people always ask me about Baidu Library technology or flex.
Paper technology. Haha. In fact, this is not as difficult as you think. For Baidu Library, it can prove that
Flash
Paper is still open source
Flash2print is not so intelligent. Why intelligence? You can analyze the cache in your Baidu Library in IE
SWF
File
. Each SWF file is spliced by several SWF files, and each CWS file is a SWF file. A {"totalpage": "210", "frompage": "1", "topage": "6"} field is displayed at the beginning of the file. This can be treated as JSON or as3. Of course, to use it, you must first serialize it.
Totalpage refers to the total number of pages in this article.
How many pages does frompage come from.
Topage is the number of pages to go.
Of course, we are incapable of ourselves.
Development
For such a software, we need to consider the user experience when using the open-source flash2print request. Therefore, when the server generates the SWF file, we usually choose to combine pages for implementation.
When the first page is requested, the server sends
Data
Yes. Why are 1-6 pages of bandwidth 1-6 pages of bandwidth in China. Haha. The data on the second page is 2-7 pages.
Then the corresponding request:

Page ID page number

1 1-6
2 2-7
..
..
10 6-15
This is a very simple rule, because we need to consider the user's page Jump, so we need to go to the first four pages and the last five pages of the user's page number, this is done to help users better experience and reduce the pressure on the server.
We can call and send the data in the background at the front end. Haha, write
Player
First of all, we need to consider the problem that the loader is the loader. We cannot use the conventional loader because the binary file is returned, so we cannot use the display package.
Loader
The parsing is done. Because it is not a normal SWF, the reason is that a custom object is added at the beginning of the binary file and needs to be parsed.
Excuse me, sir? You said you cannot use loader. What do you use? Urlstream? Urlloader ?, You can do whatever you like. Urlloader is the simplest. Haha. You can use it. The method of adding binary is actually equivalent to refining the data and letting AVM know you, so that you can play the video. But I remember it was silly when I was doing it. Haha. If you don't understand it, you can write it as soon as you know it, and the result efficiency is greatly reduced.
Source code
Below: Decline effect especially, do not think of the final program
If the efficiency is low, you can write it like this. Haha.
Package com. Loader
{
/**
* Load
SWF file operation class.
* A series of methods are provided to intercept the spliced SWF into bytearray data and then
* Distribute to ascode objects.
*/

Import com. display. displayloader;
Import com. Events. itemloadevent;
Import com. Events. swfinfocomplelateevent;
Import com.vow.vo;
Import flash. Events. eventdispatcher;
Public class swfloader extends eventdispatcher
{
Import flash. display. loader;
Import flash. display. Sprite;
Import flash. Events. event;
Import flash.net. URLRequest;
Import flash.net. urlstream;
Import flash. utils. bytearray;
Import com. Events. swfinfocomplelateevent;
Import com.vow.vo;


Public Function swfloader ()
{
Addeventlistener (swfinfocomplelateevent. load_complelate_dates, oncompletehandler );
}

/**
* Loading completed
* Serialize the object with the VO object.
*
*/

Public var VO: pdfvo;
Private function oncompletehandler (E: swfinfocomplelateevent): void
{E.tar get. removeeventlistener (swfinfocomplelateevent. load_complelate_dates, oncompletehandler );
Vo = new pdfvo (E. obj. Parent );
VaR Leng: Number = E. obj. position. Length-1;
For (var I: Int = 0; I <Leng; I ++)
{
VaR bytearray: bytearray=e.tar get. getstreampostiondatas (E. obj. Parent, E. obj. position. Start, E. obj. position. End );
VaR DS
Isplayloader = new displayloader (bytearray );
Boxes. Push (DS );
}
If (boxes [0]! = NULL) This. dispatchevent (New itemloadevent (itemloadevent. item_ OK, boxes ));
Return;
}

/**
* Loading Method
*
*/

Private var path: URLRequest;
Public Function load (Source: string): void
{
If (Stream) Close ();
Stream = new urlstream ();
Path = new URLRequest (source );
Try
{
Stream. Load (PATH );
}
Catch (E: Error)
{
Trace ("error ");
}
Stream. addeventlistener (event. Complete, completehandler );
}

/**
* Obtain the displayloader object at the index position.
*/

Private var boxes: array = [];
Public Function getdisplayyitemat (Index: INT)
Isplayloader
{
If (boxes [Index] = undefined) return NULL;
Return Boxes [Index];
}

/**
* Stream Loading completed
*/
Private var swfinfobox: array = [];
Private function completehandler (E: Event): void
{
Stream. removeeventlistener (event. Complete, completehandler );
VaR arr: bytearray = new bytearray ();
Stream. readbytes (ARR, 0, stream. bytesavailable );
VaR arrspeed: bytearray = arr;
Swfinfobox = splitswfinfo (ARR );
VaR OBJ: Object = {
Stream: stream,
Parent: arr,
Alsobyte: arrspeed,
Position: swfinfobox
}
VaR EVT: swfinfocomplelateevent = new swfinfocomplelateevent ("done", OBJ );
This. dispatchevent (EVT );
}

Private var stream: urlstream;

/**
* Obtain a piece of data
*/
Public Function getstreampostiondatas (Source: bytearray, index: Number, end: Number): bytearray
{
VaR temp: bytearray = new bytearray ();
VaR J: Number = index;
VaR length: Number = end-index;
While (j <= end)
{
Temp [J-Index] = source [J];
J ++;
}

Return temp;
}

/**
* Obtain the specified point information.
*/
Public Function getswfpostioncollection (parent: bytearray): Array
{
VaR swfinfobox: array = splitswfinfo (parent );
Return swfinfobox;
}

/**
* Truncate SWF information (data)
*
*/
Public Function splitswfinfo (parent: bytearray): Array
{
VaR Leng: Number = parent. length;
VaR endindex: array = [];
VaR startindex: array = [];
For (var I: Number = 0; I <= Leng; I ++)
{
If (parent = 0x43 & parent [I + 1] = 0x57 & parent [I + 2] = 0x53)
{
Startindex. Push (I );
Endindex. Push (I-1 );
}
}
VaR swfbox: array = [];
VaR Len: Number = startindex. length;
For (var j: Int = 0; j <= Len; j ++)
{
J = len-1? Swfbox [J] = {start: startindex [J], end: Leng }:
Swfbox [J] = {start: startindex [J], end: endindex [J + 1]};
}
Return swfbox;
}

/**
* Close Method
*/
Public Function close (): void
{
Stream. Close ();
Stream = NULL;
}
}
}

In fact, I don't need to write that. I spent half a day loading data. It may have been written in Flash ide at the time, so it was messy. Now I think it is not helpful. It is different to use urlloader. Much less effort, but be sure to set the data format loader. dataformat = urlloaderdataformat. binary; then, after reading the scriptures, find the file containing several CWS characters. split the SWF data. finally, put the split data in a data set or a large array, and use a loader to display them.

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.