4 Replies Latest reply on Jan 9, 2012 10:24 AM by Claudiu Ursica

    Traversing a ByteArray

    mitz11 Level 1

      Hi everyone, I'm trying to do something that I thought would be pretty simple, but I'm having some trouble. 

      I mean to read chunks out of a bytearray, and store them in another bytearray.  The catch is, those chunks need to come off the end of the first bytearray, but not reversed.

       

      So, I have:

       

      var chunkSize:int=1000;

      for (var chunkNum:uint = 1; chunkNum  <= chunkSize; chunkNum ++)

                                              {

        originalByteArray.readBytes(chunkByteArray, bytes.length - (chunkSize*chunkNum ), chunkSize);

         //process the row down here

      }

       

       

      What I mean to do is take chunks of the original bytearray off of the end, and store them in the second bytearray.  The second bytearray shouldn't ever be any bigger than 1 "chunk".

       

      What the above code does, is read a chunk from the end of the original byte array, and allocate the WHOLE SIZE of that ENTIRE array into chunkByteArray so that the data can be written to the same position. 

      That's really not useful to me.  So what can I do?

       

      To illustrate what I'm trying to do, I have a bytearray with data that looks like:

       

      1, 2, 3, 4, 5, 6, 7, 8, 9

       

      And I want to pull out "chunks", starting from the end, so:
      Chunk 1:  7, 8, 9
      Chunk 2:  4, 5, 6
      Chunk 3:  1, 2, 3

       

      Of course the actual array is much, much bigger.

      Ideas?

        • 1. Re: Traversing a ByteArray
          mitz11 Level 1

          Okay, I found a way (but I kind of hate it). 

           


          for (var chunkNum:uint = 1; chunkNum <= totalChunks; chunkNum++)

          {

               chunkByteArray.clear();

               for (var byteNum:uint = 0; byteNum< ChunkSize;byteNum++)

               {

                    chunkByteArray.writeByte(originalByteArray[byteNum + (bytes.length - (totalChunks*chunkNum))]);

                    //process chunk here

               }

          }

           

           

          Sorry to bother you guys. 

          • 2. Re: Traversing a ByteArray
            Claudiu Ursica Level 4

            1, 2, 3, 4, 5, 6, 7, 8, 9

             

            This will get you there is not optimized though just played with it.

             

                            var ba:ByteArray = new ByteArray();

                           

                            for (var i:int = 1; i < 10; i++)

                            {

                                ba.writeByte(i);   

                            }

                           

                            ba.position = 0;

                            trace("I have ", ba.bytesAvailable, " bytes");

                           

                            var totalBytesInArray:uint = ba.bytesAvailable;

                            var chunkSize:int = 3;

                            var chunkNum:Number = ba.bytesAvailable / chunkSize;

                           

                            trace("Chunks to read ", chunkNum);

                            var j:uint = chunkNum;

                            while (j > 0)

                            {   

                                ba.position = ba.bytesAvailable - chunkSize * (chunkNum + 1 - j);

             

                                var destByteArray:ByteArray = new ByteArray();

                                ba.readBytes(destByteArray, 0, chunkSize);

             

                                destByteArray.position = 0;

                                 while (destByteArray.bytesAvailable)

                                {

                                    var byte:uint = destByteArray.readByte();

                                    trace("byte ", byte);

                                    //todo save the destination bytes for later usage

                                }

                               

                                ba.position = 0;

                                j--;

                            }

             

             

            C

            • 3. Re: Traversing a ByteArray
              mitz11 Level 1

              This came down to me not understanding exactly how the readbytes() function worked.  I see now that the parameters in that function determine the position in the array you are writing to, and not the one you are reading from.  The documentation seems ambiguous on this to me.  Thank you for the help!

              • 4. Re: Traversing a ByteArray
                Claudiu Ursica Level 4

                Makes the 2 of us took me a second try to get it right. Also found the docs a bit ambiguous.

                C