as3 之 Base64 共享
import flash.utils.ByteArray; public class Base64 { private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; public static const version:String = "1.0.0"; public static function encode(data:String):String { // Convert string to ByteArray var bytes:ByteArray = new ByteArray(); bytes.writeUTFBytes(data); // Return encoded ByteArray return encodeByteArray(bytes); } public static function encodeByteArray(data:ByteArray):String { // Initialise output var output:String = ""; // Create data and output buffers var dataBuffer:Array; var outputBuffer:Array = new Array(4); // Rewind ByteArray data.position = 0; // while there are still bytes to be processed while (data.bytesAvailable > 0) { // Create new data buffer and populate next 3 bytes from data dataBuffer = new Array(); for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) { dataBuffer[i] = data.readUnsignedByte(); } // Convert to data buffer Base64 character positions and // store in output buffer outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2; outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4); outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6); outputBuffer[3] = dataBuffer[2] & 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++) { outputBuffer[j + 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(outputBuffer[k]); } } // 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++) { dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j)); } // Decode data buffer back into bytes outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4); outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2); outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3]; // Add all non-padded bytes in output buffer to decoded data for (var k:uint = 0; k < outputBuffer.length; k++) { if (dataBuffer[k+1] == 64) break; output.writeByte(outputBuffer[k]); } } // Rewind decoded data ByteArray output.position = 0; // Return decoded data return output; } public function Base64() { throw new Error("Base64 class is static container only"); } }
內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。
相關文章
- as3 時間格式工具S3
- Java之Base64編碼解析Java
- AS3 js正規表示式 反向引用(backreference)S3JS
- 為嘛沒有as3 /flash技術咧。。S3
- 微信開發之data:image/png;base64,
- QT之共享記憶體QT記憶體
- Python學習之共享引用Python
- netty系列之:java中的base64編碼器NettyJava
- WebAssembly體驗之編碼base64(AssemblyScript使用教程)Web
- 由“正方”jiam、jiemi之逆向思及Base64之逆編碼表
- base64
- netty系列之:netty中的核心編碼器base64Netty
- 計算機編碼規則之:Base64編碼計算機
- 多執行緒之共享模型執行緒模型
- 微服務之間如何共享DTO?微服務
- Base64 原理
- Base64原理
- Android Base64Android
- image to/from base64
- Base64編碼
- base64隱寫
- Base64工具類
- base64 編碼
- python中base64Python
- 程式間通訊之共享記憶體記憶體
- 各種檔案用JS轉Base64之後的data型別JS型別
- java字串轉Base64Java字串
- JavaScript 實現 Base64JavaScript
- base64 編碼原理
- java BASE64處理Java
- PHP Base64 加密 & 解密PHP加密解密
- HarmonyOS-基礎之狀態資料共享
- Java 多執行緒共享模型之管程(上)Java執行緒模型
- 安全系列之:跨域資源共享CORS跨域CORS
- 防止快取擊穿之程式內共享呼叫快取
- Linux程式間通訊之共享記憶體Linux記憶體
- fragment之間相互傳資料、共享資料Fragment
- Golang併發之共享記憶體變數Golang記憶體變數