home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / mx / utils / Base64Encoder.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  4.8 KB  |  181 lines

  1. package mx.utils
  2. {
  3.    import flash.utils.ByteArray;
  4.    
  5.    public class Base64Encoder
  6.    {
  7.       public static const CHARSET_UTF_8:String = "UTF-8";
  8.       
  9.       public static var newLine:int = 10;
  10.       
  11.       public static const MAX_BUFFER_SIZE:uint = 32767;
  12.       
  13.       private static const ESCAPE_CHAR_CODE:Number = 61;
  14.       
  15.       private static const ALPHABET_CHAR_CODES:Array = [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];
  16.       
  17.       private var _line:uint;
  18.       
  19.       private var _count:uint;
  20.       
  21.       private var _buffers:Array;
  22.       
  23.       public var insertNewLines:Boolean = true;
  24.       
  25.       private var _work:Array = [0,0,0];
  26.       
  27.       public function Base64Encoder()
  28.       {
  29.          super();
  30.          reset();
  31.       }
  32.       
  33.       public function flush() : String
  34.       {
  35.          if(_count > 0)
  36.          {
  37.             encodeBlock();
  38.          }
  39.          var _loc1_:String = drain();
  40.          reset();
  41.          return _loc1_;
  42.       }
  43.       
  44.       public function toString() : String
  45.       {
  46.          return flush();
  47.       }
  48.       
  49.       public function reset() : void
  50.       {
  51.          _buffers = [];
  52.          _buffers.push([]);
  53.          _count = 0;
  54.          _line = 0;
  55.          _work[0] = 0;
  56.          _work[1] = 0;
  57.          _work[2] = 0;
  58.       }
  59.       
  60.       public function encodeBytes(param1:ByteArray, param2:uint = 0, param3:uint = 0) : void
  61.       {
  62.          if(param3 == 0)
  63.          {
  64.             param3 = param1.length;
  65.          }
  66.          var _loc4_:uint = param1.position;
  67.          param1.position = param2;
  68.          var _loc5_:uint = param2;
  69.          var _loc6_:uint = param2 + param3;
  70.          if(_loc6_ > param1.length)
  71.          {
  72.             _loc6_ = param1.length;
  73.          }
  74.          while(_loc5_ < _loc6_)
  75.          {
  76.             _work[_count] = param1[_loc5_];
  77.             ++_count;
  78.             if(_count == _work.length || _loc6_ - _loc5_ == 1)
  79.             {
  80.                encodeBlock();
  81.                _count = 0;
  82.                _work[0] = 0;
  83.                _work[1] = 0;
  84.                _work[2] = 0;
  85.             }
  86.             _loc5_++;
  87.          }
  88.          param1.position = _loc4_;
  89.       }
  90.       
  91.       public function encode(param1:String, param2:uint = 0, param3:uint = 0) : void
  92.       {
  93.          if(param3 == 0)
  94.          {
  95.             param3 = uint(param1.length);
  96.          }
  97.          var _loc4_:uint = param2;
  98.          var _loc5_:uint = param2 + param3;
  99.          if(_loc5_ > param1.length)
  100.          {
  101.             _loc5_ = uint(param1.length);
  102.          }
  103.          while(_loc4_ < _loc5_)
  104.          {
  105.             _work[_count] = param1.charCodeAt(_loc4_);
  106.             ++_count;
  107.             if(_count == _work.length || _loc5_ - _loc4_ == 1)
  108.             {
  109.                encodeBlock();
  110.                _count = 0;
  111.                _work[0] = 0;
  112.                _work[1] = 0;
  113.                _work[2] = 0;
  114.             }
  115.             _loc4_++;
  116.          }
  117.       }
  118.       
  119.       private function encodeBlock() : void
  120.       {
  121.          var _loc1_:Array = _buffers[_buffers.length - 1] as Array;
  122.          if(_loc1_.length >= MAX_BUFFER_SIZE)
  123.          {
  124.             _loc1_ = [];
  125.             _buffers.push(_loc1_);
  126.          }
  127.          _loc1_.push(ALPHABET_CHAR_CODES[(_work[0] & 0xFF) >> 2]);
  128.          _loc1_.push(ALPHABET_CHAR_CODES[(_work[0] & 3) << 4 | (_work[1] & 0xF0) >> 4]);
  129.          if(_count > 1)
  130.          {
  131.             _loc1_.push(ALPHABET_CHAR_CODES[(_work[1] & 0x0F) << 2 | (_work[2] & 0xC0) >> 6]);
  132.          }
  133.          else
  134.          {
  135.             _loc1_.push(ESCAPE_CHAR_CODE);
  136.          }
  137.          if(_count > 2)
  138.          {
  139.             _loc1_.push(ALPHABET_CHAR_CODES[_work[2] & 0x3F]);
  140.          }
  141.          else
  142.          {
  143.             _loc1_.push(ESCAPE_CHAR_CODE);
  144.          }
  145.          if(insertNewLines)
  146.          {
  147.             if((_line = _line + 4) == 76)
  148.             {
  149.                _loc1_.push(newLine);
  150.                _line = 0;
  151.             }
  152.          }
  153.       }
  154.       
  155.       public function encodeUTFBytes(param1:String) : void
  156.       {
  157.          var _loc2_:ByteArray = new ByteArray();
  158.          _loc2_.writeUTFBytes(param1);
  159.          _loc2_.position = 0;
  160.          encodeBytes(_loc2_);
  161.       }
  162.       
  163.       public function drain() : String
  164.       {
  165.          var _loc3_:Array = null;
  166.          var _loc1_:String = "";
  167.          var _loc2_:uint = 0;
  168.          while(_loc2_ < _buffers.length)
  169.          {
  170.             _loc3_ = _buffers[_loc2_] as Array;
  171.             _loc1_ += String.fromCharCode.apply(null,_loc3_);
  172.             _loc2_++;
  173.          }
  174.          _buffers = [];
  175.          _buffers.push([]);
  176.          return _loc1_;
  177.       }
  178.    }
  179. }
  180.  
  181.