home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Darbas / kidoz_v1.air / kidoz.swf / scripts / mx / utils / Base64Decoder.as < prev    next >
Encoding:
Text File  |  2009-05-06  |  3.9 KB  |  120 lines

  1. package mx.utils
  2. {
  3.    import flash.utils.ByteArray;
  4.    import mx.resources.IResourceManager;
  5.    import mx.resources.ResourceManager;
  6.    
  7.    public class Base64Decoder
  8.    {
  9.       private static const ESCAPE_CHAR_CODE:Number = 61;
  10.       
  11.       private static const inverse:Array = [64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64];
  12.       
  13.       private var filled:int = 0;
  14.       
  15.       private var data:ByteArray;
  16.       
  17.       private var count:int = 0;
  18.       
  19.       private var work:Array = [0,0,0,0];
  20.       
  21.       private var resourceManager:IResourceManager = ResourceManager.getInstance();
  22.       
  23.       public function Base64Decoder()
  24.       {
  25.          super();
  26.          data = new ByteArray();
  27.       }
  28.       
  29.       private static function copyByteArray(param1:ByteArray, param2:ByteArray, param3:uint = 0) : void
  30.       {
  31.          var _loc4_:int = int(param1.position);
  32.          param1.position = 0;
  33.          param2.position = 0;
  34.          var _loc5_:uint = 0;
  35.          while(param1.bytesAvailable > 0 && _loc5_ < param3)
  36.          {
  37.             param2.writeByte(param1.readByte());
  38.             _loc5_++;
  39.          }
  40.          param1.position = _loc4_;
  41.          param2.position = 0;
  42.       }
  43.       
  44.       public function flush() : ByteArray
  45.       {
  46.          var _loc1_:String = null;
  47.          if(count > 0)
  48.          {
  49.             _loc1_ = resourceManager.getString("utils","partialBlockDropped",[count]);
  50.             throw new Error(_loc1_);
  51.          }
  52.          return drain();
  53.       }
  54.       
  55.       public function reset() : void
  56.       {
  57.          data = new ByteArray();
  58.          count = 0;
  59.          filled = 0;
  60.       }
  61.       
  62.       public function decode(param1:String) : void
  63.       {
  64.          var _loc3_:Number = NaN;
  65.          var _loc2_:uint = 0;
  66.          for(; _loc2_ < param1.length; _loc2_++)
  67.          {
  68.             _loc3_ = Number(param1.charCodeAt(_loc2_));
  69.             if(_loc3_ == ESCAPE_CHAR_CODE)
  70.             {
  71.                var _loc4_:*;
  72.                work[_loc4_ = count++] = -1;
  73.             }
  74.             else
  75.             {
  76.                if(inverse[_loc3_] == 64)
  77.                {
  78.                   continue;
  79.                }
  80.                work[_loc4_ = count++] = inverse[_loc3_];
  81.             }
  82.             if(count == 4)
  83.             {
  84.                count = 0;
  85.                data.writeByte(work[0] << 2 | (work[1] & 0xFF) >> 4);
  86.                ++filled;
  87.                if(work[2] == -1)
  88.                {
  89.                   break;
  90.                }
  91.                data.writeByte(work[1] << 4 | (work[2] & 0xFF) >> 2);
  92.                ++filled;
  93.                if(work[3] == -1)
  94.                {
  95.                   break;
  96.                }
  97.                data.writeByte(work[2] << 6 | work[3]);
  98.                ++filled;
  99.             }
  100.          }
  101.       }
  102.       
  103.       public function toByteArray() : ByteArray
  104.       {
  105.          var _loc1_:ByteArray = flush();
  106.          reset();
  107.          return _loc1_;
  108.       }
  109.       
  110.       public function drain() : ByteArray
  111.       {
  112.          var _loc1_:ByteArray = new ByteArray();
  113.          copyByteArray(data,_loc1_,filled);
  114.          filled = 0;
  115.          return _loc1_;
  116.       }
  117.    }
  118. }
  119.  
  120.