Flash AS3 .doc_第1页
Flash AS3 .doc_第2页
Flash AS3 .doc_第3页
Flash AS3 .doc_第4页
Flash AS3 .doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Flash AS3 Base64网上找到的Base64执行速度都不怎么快,原因是代码对Flash执行机制没有针对性优化。 下面给出优化过的Base64,其中inline 内联的find方法需要3的flex sdk补丁才难正常编译。package import flash.utils.ByteArray; /* * 优化Base64 * 主要优化执行方式,尽量减少调用函数和尽可能不使用循环 * 优化后提升执行速度是原来的10倍 * Directed by kiwiw3 * */ public class MyBase64 public static const version:String = 2.0.0; public static function encode(data:String):String var bytes:ByteArray = new ByteArray(); bytes.writeUTFBytes(data); return encodeByteArray(bytes); private static const BASE64_CHARARR:Vector. = new 65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47,61; public static function encodeByteArray(data:ByteArray):String var output:ByteArray = new ByteArray(); var opbuf0:int;var opbuf1:int;var opbuf2:int;var opbuf3:int;var dblen:int = 0;var resultlen:uint = data.length;var position:uint = 0;var oplen:uint = 0; while (resultlen 0) if (resultlen = 3) dblen = 3;resultlen -= 3;else dblen = resultlen;resultlen = 0; opbuf0 = (dataposition & 0xfc) 2;if(dblen=1) opbuf1 = (dataposition & 0x03) 4);elseopbuf1 = 64;if(dblen=2) opbuf2 = (dataposition + 1 & 0x0f) 6);elseopbuf2 = 64;if(dblen=3) opbuf3 = dataposition + 2 & 0x3f;elseopbuf3 = 64; outputoplen= BASE64_CHARARRopbuf0; outputoplen+1= BASE64_CHARARRopbuf1; outputoplen+2= BASE64_CHARARRopbuf2;outputoplen+3= BASE64_CHARARRopbuf3; position += 3;oplen += 4; return output.readUTFBytes(oplen); public static function decode(data:String):String var bytes:ByteArray = decodeToByteArray(data); return bytes.readUTFBytes(bytes.length); public static function decodeToByteArray(_data:String):ByteArray var output:ByteArray = new ByteArray();var data:ByteArray = new ByteArray();data.writeUTFBytes(_data);var databuf0:int;var databuf1:int;var databuf2:int;var databuf3:int;var resultlen:uint = data.length;var position:uint = 0;var oplen:uint = 0; while (resultlen0) find(databuf0,dataposition);if (resultlen = 1)find(databuf1,dataposition+ 1);if (resultlen = 2)find(databuf2,dataposition+ 2);if (resultlen = 3) find(databuf3,dataposition+3); if (databuf1 != 64) outputoplen = (databuf0 4);if (databuf2 != 64) outputoplen + 1 = (databuf1 & 0x0f) 2); if (databuf3 != 64) outputoplen+ 2 = (databuf2 & 0x03) = 65 & val= 97 & val= 48 & val 0) / Create new data buffer and populate next 3 bytes from data dataBuffer = new Array(); for (var i:uint = 0; i 0; i+) dataBufferi = data.readUnsignedByte(); / Convert to data buffer Base64 character positions and / store in output buffer outputBuffer0 = (dataBuffer0 & 0xfc) 2; outputBuffer1 = (dataBuffer0 & 0x03) 4); outputBuffer2 = (dataBuffer1 & 0x0f) 6); outputBuffer3 = dataBuffer2 & 0x3f; / If data buffer was short (i.e not 3 characters) then set / end character indexes in data buffer to index of = symbol. / This is necessary because Base64 data is always a multiple of / 4 bytes and is basses with = symbols. for (var j:uint = dataBuffer.length; j 3; j+) outputBufferj + 1 = 64; / Loop through output buffer and add Base64 characters to / encoded data string for each character. for (var k:uint = 0; k outputBuffer.length; k+) output += BASE64_CHARS.charAt(outputBufferk); / Return encoded data return output; public static function decode(data:String):String / Decode data to ByteArray var bytes:ByteArray = decodeToByteArray(data); / Convert to string and return return bytes.readUTFBytes(bytes.length); public static function decodeToByteArray(data:String):ByteArray / Initialise output ByteArray for decoded data var output:ByteArray = new ByteArray(); / Create data and output buffers var dataBuffer:Array = new Array(4); var outputBuffer:Array = new Array(3); / While there are data bytes left to be processed for (var i:uint = 0; i data.length; i += 4) / Populate data buffer with position of Base64 characters for / next 4 bytes from encoded data for (var j:uint = 0; j 4 & i + j data.length; j+) dataBufferj = BASE64_CHARS.indexOf(data.charAt(i + j); / Decode data buffer back into bytes outputBuffer0 = (dataBuffer0 4); outputBuffer1 = (dataBuffer1 & 0x0f) 2); outputBuffer2 = (dataBuffer2 & 0x03) 6) + dataBuffer3; / Add all non-padded bytes in output buffer to decoded data for (var k:uint = 0; k outputBuffer.length; k+) if (dataB

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论