Advertisement
Drakim

ByteArray Alchemy

Aug 18th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ByteArray {
  2.   public var pointer:platform.FlashPlatform.FlashMemoryPointer;
  3.   public function new(_length:Int) {
  4.     pointer = platform.FlashPlatform.FlashMemoryManager.alloc(_length,true);
  5.   }
  6.   public inline function writeBytes(_bytearray:ByteArray,_offset1:Int,_offset2:Int,_length:Int):Void {
  7.     if(_length<300) {
  8.       var i = 0;
  9.       while(i<_length-4) {write64(_bytearray.read64(_offset1+i),_offset2+i);i+=8;}
  10.       if(i<_length) {write32(_bytearray.read32(_offset1+i),_offset2+i);}
  11.     } else {
  12.       platform.FlashPlatform.FlashMemoryManager.bytearray.position = pointer.address+_offset2;
  13.       platform.FlashPlatform.FlashMemoryManager.bytearray.writeBytes(platform.FlashPlatform.FlashMemoryManager.bytearray,_bytearray.pointer.address+_offset1,_length);
  14.     }
  15.   }
  16.   public inline function read64(_address:Int):Float {
  17.     return flash.Memory.getDouble(pointer.address+_address);
  18.   }
  19.   public inline function read32(_address:Int):Int {
  20.     return flash.Memory.getI32(pointer.address+_address);
  21.   }
  22.   public inline function read31(_address:Int):Int {
  23.     return flash.Memory.getI32(pointer.address+_address)&0x7FFFFFFF;
  24.   }
  25.   public inline function read24(_address:Int):Int {
  26.     return flash.Memory.getI32(pointer.address+_address)&0x00FFFFFF;
  27.   }
  28.   public inline function read16(_address:Int):Int {
  29.     return flash.Memory.getUI16(pointer.address+_address);
  30.   }
  31.   public inline function read8(_address:Int):Int {
  32.     return flash.Memory.getByte(pointer.address+_address);
  33.   }
  34.   public inline function write64(_v:Float,_address:Int):Void {
  35.     flash.Memory.setDouble(pointer.address+_address,_v);
  36.   }
  37.   public inline function write32(_v:Int,_address:Int):Void {
  38.     flash.Memory.setI32(pointer.address+_address,_v);
  39.   }
  40.   public inline function write31(_v:Int,_address:Int):Void {
  41.     flash.Memory.setI32(pointer.address+_address,(flash.Memory.getI32(pointer.address+_address)&0x80000000)|(_v&0x7FFFFFFF));
  42.   }
  43.   public inline function write24(_v:Int,_address:Int):Void {
  44.     flash.Memory.setI32(pointer.address+_address,(flash.Memory.getI32(pointer.address+_address)&0xFF000000)|(_v&0x00FFFFFF));
  45.   }
  46.   public inline function write16(_v:Int,_address:Int):Void {
  47.     flash.Memory.setI16(pointer.address+_address,_v);
  48.   }
  49.   public inline function write8(_v:Int,_address:Int):Void {
  50.     flash.Memory.setByte(pointer.address+_address,_v);
  51.   }
  52.   public inline function readSigned32(_address:Int):Int {
  53.     return read32(_address);
  54.   }
  55.   public inline function readSigned31(_address:Int):Int {
  56.     var v:Int = flash.Memory.getI32(pointer.address+_address)&0x7FFFFFFF;
  57.     return v - (0x7FFFFFFF+1)*(v>>30);
  58.   }
  59.   public inline function readSigned24(_address:Int):Int {
  60.     var v:Int = flash.Memory.getI32(pointer.address+_address)&0x00FFFFFF;
  61.     return v - (0xFFFFFF+1)*(v>>23);
  62.   }
  63.   public inline function readSigned16(_address:Int):Int {
  64.     var v:Int = flash.Memory.getUI16(pointer.address+_address);
  65.     return v - (0xFFFF+1)*(v>>15);
  66.   }
  67.   public inline function readSigned8(_address:Int):Int {
  68.     var v:Int = flash.Memory.getByte(pointer.address+_address);
  69.     return v - (0xFF+1)*(v>>7);
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement