使用方法在注释中
/*
* ForcibleLoader
*
* Licensed under the MIT License
*
* Copyright (c) 2007-2009 BeInteractive! (www.be-interactive.org) and
* Spark project (www.libspark.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package
{
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.utils.Endian;
import flash.errors.EOFError;
/**
* Loads a SWF file as version 9 format forcibly even if version is under 9.
*
* Usage:
* <pre>
* var loader:Loader = Loader(addChild(new Loader()));
* var fLoader:ForcibleLoader = new ForcibleLoader(loader);
* fLoader.load(new URLRequest('swf7.swf'));
* </pre>
*
* @author yossy:beinteractive
* @see http://www.be-interactive.org/?itemid=250
* @see http://fladdict.net/blog/2007/05/avm2avm1swf.html
*/
public class ForcibleLoader
{
public function ForcibleLoader(loader:Loader)
{
this.loader = loader;
_stream = new URLStream();
_stream.addEventListener(Event.COMPLETE, completeHandler);
_stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
_stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
private var _loader:Loader;
private var _stream:URLStream;
public function get loader():Loader
{
return _loader;
}
public function set loader(value:Loader):void
{
_loader = value;
}
public function load(request:URLRequest):void
{
_stream.load(request);
}
private function completeHandler(event:Event):void
{
var inputBytes:ByteArray = new ByteArray();
_stream.readBytes(inputBytes);
_stream.close();
inputBytes.endian = Endian.LITTLE_ENDIAN;
if (isCompressed(inputBytes)) {
uncompress(inputBytes);
}
var version:uint = uint(inputBytes[3]);
if (version < 9) {
updateVersion(inputBytes, 9);
}
if (version > 7) {
flagSWF9Bit(inputBytes);
}
else {
insertFileAttributesTag(inputBytes);
}
loader.loadBytes(inputBytes);
}
private function isCompressed(bytes:ByteArray):Boolean
{
return bytes[0] == 0x43;
}
private function uncompress(bytes:ByteArray):void
{
var cBytes:ByteArray = new ByteArray();
cBytes.writeBytes(bytes, 8);
bytes.length = 8;
bytes.position = 8;
cBytes.uncompress();
bytes.writeBytes(cBytes);
bytes[0] = 0x46;
cBytes.length = 0;
}
private function getBodyPosition(bytes:ByteArray):uint
{
var result:uint = 0;
result += 3; // FWS/CWS
result += 1; // version(byte)
result += 4; // length(32bit-uint)
var rectNBits:uint = bytes[result] >>> 3;
result += (5 + rectNBits * 4) / 8; // stage(rect)
result += 2;
result += 1; // frameRate(byte)
result += 2; // totalFrames(16bit-uint)
return result;
}
private function findFileAttributesPosition(offset:uint, bytes:ByteArray):uint
{
bytes.position = offset;
try {
for (;;) {
var byte:uint = bytes.readShort();
var tag:uint = byte >>> 6;
if (tag == 69) {
return bytes.position - 2;
}
var length:uint = byte & 0x3f;
if (length == 0x3f) {
length = bytes.readInt();
}
bytes.position += length;
}
}
catch (e:EOFError) {
}
return NaN;
}
private function flagSWF9Bit(bytes:ByteArray):void
{
var pos:uint = findFileAttributesPosition(getBodyPosition(bytes), bytes);
if (!isNaN(pos)) {
bytes[pos + 2] |= 0x08;
}
}
private function insertFileAttributesTag(bytes:ByteArray):void
{
var pos:uint = getBodyPosition(bytes);
var afterBytes:ByteArray = new ByteArray();
afterBytes.writeBytes(bytes, pos);
bytes.length = pos;
bytes.position = pos;
bytes.writeByte(0x44);
bytes.writeByte(0x11);
bytes.writeByte(0x08);
bytes.writeByte(0x00);
bytes.writeByte(0x00);
bytes.writeByte(0x00);
bytes.writeBytes(afterBytes);
afterBytes.length = 0;
}
private function updateVersion(bytes:ByteArray, version:uint):void
{
bytes[3] = version;
}
private function ioErrorHandler(event:IOErrorEvent):void
{
loader.contentLoaderInfo.dispatchEvent(event.clone());
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
loader.contentLoaderInfo.dispatchEvent(event.clone());
}
}
}
转自http://www.imemment.com/post/82.html
分享到:
相关推荐
这是一个外国人写的类,可以将AVM1影片转换为MovieClip来操作。
swf文件结构及avm原理 对理解flash及其运行具有一定的指导意义, 真正理解flash 的swf文件里面存储的内容,以及 avm如何更具这些内容来运行swf程序
Avm2虚拟机浅析与as3性能优化 Adobe
剖析SWF文件结构_探秘AVM运行原理剖析SWF文件结构_探秘AVM运行原理剖析SWF文件结构_探秘AVM运行原理
倍加福编码器AVM58资料 对与AVM58有初步的了解
NULL 博文链接:https://fykyx521.iteye.com/blog/383374
flash avm2 虚拟机说明,DOABC解析
基于Erlang VM的语言基于Erlang基于Erlang VM的语言 VM的语言基于Erlang VM的语言
惊喜!!Flash开源时代降临!AVM2所有源代码将作为Mozilla的开源项;这也给喜欢adobe的朋友们研究AVM2提供了可能,还等什么,赶快去下载吧
Actionscript Virtual Machine avm2
flash的虚拟机源码-AVM2 avmplus Adobe System Incorporated
公司内部的ppt,大致介绍了flash的文件格式和AVM运行原理
PIAB 真空泵P6010AVM 选型册pdf,PIAB 真空泵P6010AVM 选型册
计数问题 AVM竞赛练习 C语言 基础算法
士研电机保护继电器AVM-NB说明书pdf,士研电机保护继电器AVM-NB说明书:相位顺序不对(逆相)及电压过高或电压不足时自动跳脱保护,设定值可以从额定电压的±5%到±29%,如果电压超过或是不足,内部继电器立即回到原始...
高级验证方法学(AVM)中文版,做verification很好的学习资料,pdf格式,适于阅读
编码器VM-2CC
多端技术基于 avm.js 语法进行代码编写,通过 APICloud Studio3 或云编译生成 APP 以及小程序代码。AVM 跨端框架(APICloud-View-Model)是一个跨端的高性能 JavaScript 框架,更趋近于原生的编程体验。它提供简洁的...
为提升在日趋复杂的电子对抗环境中对雷达信号识别的准确率,提出了一种基于启发式采样搜索(Heuristic Sampling Search,HSS)改进S3VM的雷达辐射源信号识别算法。根据模糊函数理论,通过对雷达信号的模糊函数进行...
拟牛顿法求解S3VM的Python实现代码转载