Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash as3 How to remove part of byteArray?

var fData:ByteArray = new ByteArray();

I need to remove some bytes in this array, but can't find any public method in Flash to do that. I searched something like fData.remove(start,length) but with no success.

here is a code

    function _dlProgressHandler(evt:ProgressEvent):void { //this is progressEvent for URLStream

............... ///some code

var ff:ByteArray = new ByteArray();

stream.readBytes(ff,0,stream.bytesAvailable);
fileData.writeBytes(ff,0,ff.length); //stream writes into fileData byteArray

//and here is cutter:

fileData.position=0;
fileData.writeBytes(ff,100,fileData.length);
fileData.length=fileData.length-100);

}

So, fileData cut itself unpredictably sometimes. Sometimes old blocks're found twice, sometimes they're not found at all.

like image 484
el Dude Avatar asked Feb 04 '26 13:02

el Dude


2 Answers

You can always just only read the bytes that you want, which will have the same effect as discarding the bytes that you don't want. As a very simple example, assume you have a ByteArray that is 10 bytes long and you want to discard the first 3 bytes:

var newBytes:ByteArray = new ByteArray();
newBytes.writeBytes(fData, 2, 7);  

So instead of removing the bytes that you don't want from fData, you just create a new ByteArray and only get they bytes that you want from fData.

Obviously, if the sequence of bytes you want to remove is not just a sequence from the beginning or end of fData it will be a little more complicated, but the method remains the same: read the bytes that you want, instead of removing the ones you don't.

like image 89
Pixel Elephant Avatar answered Feb 06 '26 04:02

Pixel Elephant


AS-3 is actually very nice sometimes. This removes bytes from your array anywhere you want. Beginning, middle or the end. Just need to check indices to avoid IndexOutOfBounds

var array: ByteArray = ...; // create the byte array or load one
var index: int = 4;
var count: int = 5;

array.position = index;
array.writeBytes(array, index + count, array.length - (index + count));
array.length = array.length - count;
  • I tested this and it works well, just the checks are missing
  • a byte array can write to itself
like image 24
n4pgamer Avatar answered Feb 06 '26 06:02

n4pgamer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!