/*Base64-1.1.0copyright (c) 2006 Steve Websterpermission is hereby granted, free of charge, to any person obtaining a copy Ofthis software and associated documentation files (the "Software"), to deal inthe software without restriction, Includin g without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ofthe Soft Ware, and to permit persons to whom the software are furnished to does so,subject to the following conditions:the above copy Right notice and this permission notice shall being included in allcopies or substantial portions of the software.the Softwar E is provided ' as is ', without WARRANTY of any KIND, EXPRESS orimplied, including and not LIMITED to the warranties of MER Chantability, Fitnessfor A particular PURPOSE and noninfringement.  In NO EVENT shall the AUTHORS orcopyright holders is liable for all CLAIM, damages OR other liability, whetherin an ACTION of contract, TORT or OTHERWISE, arising from, out of or inconnection with theSoftware or the use or other dealings in the software. Modified to fix a string to ByteArray bug.*/Package Com.dynamicflash.util {ImportFlash.utils.ByteArray; /** * @private*/     Public classBase64 {Private StaticConst BASE64_CHARS:STRING ="Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";  Public StaticConst VERSION:STRING ="1.0.0";  Public Static functionencode (data:string): String {//Convert string to ByteArray            varBytes:bytearray =NewByteArray (); varLen:int =data.length;  for(varI:int = 0; i < Len; ++i) {bytes.writebyte (Data.charcodeat (i)); }            //Return encoded ByteArray            returnEncodebytearray (bytes); }                 Public Static functionEncodebytearray (Data:bytearray): String {//initialise Output            varOutput:string =""; //Create data and output buffers            varDatabuffer:array; varOutputbuffer:array =NewArray (4); //Rewind ByteArraydata.position = 0; //While there is still bytes to be processed             while(Data.bytesavailable > 0) {                //Create new data buffer and populate next 3 bytes from dataDataBuffer =NewArray ();  for(varI:uint = 0; I < 3 && data.bytesavailable > 0; i++) {Databuffer[i]=Data.readunsignedbyte (); }                                //Convert to data buffer Base64 character positions and                //store in output bufferOutputbuffer[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 is short (i.e not 3 characters) then set                //end character indexes in data buffer to index of ' = ' symbol.                //This is necessary because Base64 data are always a multiple of                //4 bytes and is basses with ' = ' symbols.                 for(varJ: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(varK:uint = 0; K < Outputbuffer.length; k++) {Output+=Base64_chars.charat (Outputbuffer[k]); }            }                        //Return encoded Data            returnoutput; }                 Public Static functiondecode (data:string): String {//Decode data to ByteArray            varBytes:bytearray =Decodetobytearray (data); //Convert to string and return            returnbytes.readutfbytes (bytes.length); }                 Public Static functionDecodetobytearray (data:string): ByteArray {//initialise output ByteArray for decoded data            varOutput:bytearray =NewByteArray (); //Create data and output buffers            varDatabuffer:array =NewArray (4); varOutputbuffer:array =NewArray (3); //While there is data bytes left-to-be processed             for(varI:uint = 0; i < data.length; i + = 4) {                //Populate data buffer with position of Base64 characters for                //next 4 bytes from encoded data                 for(varJ:uint = 0; J < 4 && i + J < data.length; J + +) {Databuffer[j]= Base64_chars.indexof (Data.charat (i +j)); }                                    //Decode Data buffer back into bytesOutputbuffer[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(varK:uint = 0; K < Outputbuffer.length; k++) {                    if(databuffer[k+1] = = 64) Break;                Output.writebyte (Outputbuffer[k]); }            }                        //Rewind decoded Data ByteArrayoutput.position = 0; //Return Decoded Data            returnoutput; }                 Public functionBase64 () {Throw NewError ("Base64 class is static container only"); }    }}
Flex Strings and Base64 conversions