Re: [Flashcoders] How to remove bytes from the beginning of a ByteArray?

2010-02-24 Thread Alexander Farber
In my desperation I've asked the same question in a german and a
russian forum last night.

And where do I find the useful replies in the morning? This mailing
list is priceles :-)
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


[Flashcoders] How to remove bytes from the beginning of a ByteArray?

2010-02-23 Thread 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


Re: [Flashcoders] How to remove bytes from the beginning of a ByteArray?

2010-02-23 Thread Juan Pablo Califano
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 alexander.far...@gmail.com

 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


Re: [Flashcoders] How to remove bytes from the beginning of a ByteArray?

2010-02-23 Thread Steven Sacks
FWIW, flush() does nothing in AS3.  Bytes written to the Socket are sent 
immediately.  Anyone who tells you otherwise has never tested it and you can 
safely ignore them! :)


Adobe has acknowledged this as a documentation bug (whatever that means).  For 
more info:


http://www.stevensacks.net/2009/12/08/flash-socket-class-does-not-wait-for-flush/
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] How to remove bytes from the beginning of a ByteArray?

2010-02-23 Thread Steven Sacks

private var buffer:ByteArray;

private function onData(event:ProgressEvent):void
{
socket.readBytes(buffer, buffer.length);
var bytes:ByteArray = new ByteArray();
buffer.readBytes(bytes, 0, messageLength);
buffer.position = bytes.length;
trim();
// deserialize the message in bytes now
}

private function trim():void
{
var bytes:ByteArray = new ByteArray();
bytes.writeBytes(buffer, buffer.position);
buffer = bytes;
buffer.position = 0;
}
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders