Hi,
You've got the source / destination backwards.
readBytes reads *from* the object on which the method is called. And it
writes *to* the ByteArray you pass in the first parameter.
So this:
var newBA:ByteArray = new ByteArray();
newBA.readBytes(_ba);
_ba = newBA;
Is copying data *from* newBA *to* _ba.
You want the opposite, so it should read:
var newBA:ByteArray = new ByteArray();
_ba.readBytes(newBA);
_ba = newBA;
You can also do the read / write in place. It's a just bit more contrived
but I'm not sure if it's worth it (it's less readable, I think). Here's how
you'd do it, anyway:
// let's assume for brevity that you've read 6 bytes from your original
buffer and you still have data left (i.e., len > 6)
var offset:int = 6;
// how much to read? All of the remaining data, which is the current length,
minus your current position (we said it was 6)
var newLen:int = _ba.length - offset;
// Read the data starting at current position and write it to the same
buffer, starting to write at the first byte.
// 0 is the offset in the *destination*. We want to copy the 6th byte into
the 0th position, that's why we pass 0.
// the third parameter tells the method how much to read from the *source*
// for what you want you don't actually need to pass these two last
parameters, since by default readBytes will write to *dest* at offset 0
// and will read from *source* all available data (i.e. from current
position to length -1)
_ba.readBytes(_ba,0,newLen);
// you do need to set this since this read/wirte is in place and you want to
shrink the buffer. Othewise, you'll end up with garbage after at the end of
the byte array.
_ba.length = newLen;
And that's it.
Hope it helps.
Cheers
Juan Pablo Califano
2010/2/23 Alexander Farber
> Hello,
>
> sorry, but I'll try to start another thread to explain my problem.
>
> I read data from a socket into a ByteArray _ba and keep inspecting it.
> When there is enough data, I'd like to extract and process it and
> remove the processed bytes from the _ba.
>
> Here is how I try to do the removal, but it doesn't work (newBA is empty)
>
> var newBA:ByteArray = new ByteArray();
> newBA.readBytes(_ba);
> _ba = newBA;
>
> My complete code is at: http://pastebin.com/esz4As6D
>
> Thank you
> Alex
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders