--- AS3: ByteArrays ---
You've most likely heard of or seen ByteArrays before, by the time you've gotten to this stage in Flash programming, but the question is: what the hell are they?
To answer it simply: An array of bytes.
That doesn't help much, I know, so i'll start at the basics: what is a byte?
The data on your hard-drive is made up of what are called "bits." Bits can be either 0 or 1, on or off; 0 being off, and 1 being on.
It's called "base 2" or "machine code" because that's the native language your processor "speaks."
Fun fact: Processor speed is measured by "bit flips" (the process of bit flipping is known as "bit manipulation"), or how many times per second x amount of bits can be changed. Obviously, the more bit flips per second, the more powerful the processor.
So what do bits have to do with bytes? Aside from the fact that they sound similar, there are 8 bits in a byte. So:
"01101001" is one byte in machine language. A byte can hold any number from 0 to 255, leaving 255! possibilities (the exclamation point is mathematical, meaning 255 * 254 * 253 * 252, etc)
how does a byte hold a number? The answer looks complicated, but once understood it's quite simple. Every bit in the byte holds a number value. This number starts at 1 and grows exponentially, reading from right to left.
0 0 0 0 0 0 0 0
128 64 32 16 8 4 2 1
a 1 in the place means add the value to the total number, a 0 means add nothing. So, to calculate the previous example (01101001):
0 1 1 0 1 0 0 1
128 64 32 16 8 4 2 1
0 + 64 + 32 + 0 + 8 + 0 + 0 + 1 = 105
so 01101001 (in base 2) is 105 (in base 10).
Fun fact: Hexadecimal is base 16, meaning it uses the number 0-9 and the letters a-f.
you can experiment with different values if you want, but here's the basics of data:
There's 4 bits in a nibble
There's 2 nibbles in a byte (or 8 bits. Nibbles generally aren't talked about, let alone used)
There's 1024 bytes in a kilobyte
There's 1024 kilobytes in a megabyte
There's 1024 megabytes in a gigabyte
There's 1024 gigabytes in a terabyte
And so on.
There's the "byte" part of "byte array" - the second part should be straightforward enough; think of it as a vector of bytes, with different functions to help you move through the vector.
There are several functions to help you add and remove values from a byte array, as well as move through it. Unfortunately, you can't simply call myByteArray and expect it to work, since most of what goes into a byte array is larger than a single byte.
A floating-point value (a float, or in AS3 a Number) is four bytes long.
Fortunately, AS3 comes equipped with several functions to read and write to byte arrays from normal primitives so we don't need to memorize all of this.
So what are these useful functions?
There most common ones are (in order of how they appear on the FlashDevelop list):
as you can see, there are many ways to insert a primitive into a byte array, depending on what that primitive is. Be careful, though, because some primitives take up quite a lot of space (*cough*Object*cough*)
since ByteArrays are stored in variables, they use RAM. You can very, very easily reach a byte array that takes several hundred MB of RAM; maybe even a GB (ever use the Sound.extract() method with the total sound object loaded in?)
so what do each of these methods do, and why are ByteArrays useful in the first place?
we'll start with the former: what so each of these do?
byteArrays have a "position" value. This value has both a getter and a setter, so you can change it as you please. The position tell us where in the byte array it is currently.
If we use a read* method, it will read however many bytes it needs to, starting with the "position" position. Think of it like adding a function to your class with a vector in it to get the next value in the vector. You would have a "position" variable to save your current spot, and then the readNext() method to return the element in the current spot + 1.
you can read too much of the byte array (try to read beyond its bounds), and it will throw an exception.
so any read* method will change the position value. Fortunately, again, the position has a setter and thus can be changed at will.
bytesAvailable is a read-only value that tells us how many bytes are remaining in the array. Think of it (using our last example) as returning the vector's length minus the current position.
return _myVector.length - _currentPosition;
length is simply that: the number if bytes in the byte array (or, again in our example, the length of the vector)
clear is also fairly self-explanatory, removing all data from the byte array and setting its position to 0.
the write* methods, compared to everything else in AS3, are a bit strange. When you write something to the byte array, it will stretch the array if needed, but it will also overwrite everything from "position" onward. If the position is at its max, and you write a float (Number) in, it will stretch the array four slots to accept the additional data. If the position is at 0 and you already have an Object in there while trying to add in a float, it will overwrite the first four bytes of the Object with the float. Not good.
Make sure you're aware of this, and set the position accordingly when dealing with writing bytes.
and finally: why are byte arrays useful?
I've found many uses for them myself. One of them is sending data back and forth from a client to a server using the Socket and ServerSocket classes. AS3 can only send data in byteArray form, and the easiest way to do so is to not use the built-in functions for sending different primitives, but instead send a single byte array with everything you need in it.
Fun fact: As you may have already figured out, byte arrays can handle more than one data type in them. You just have to remember which order they went in so you don't try to read a string from a float.
Manipulating sound data and generating visual elements from sounds are also great uses of byte arrays, as well as manipulating data in SQLite databases (and encrypting them as well).
Basically anything cool uses byte arrays. Knowing how to use them is a great tool in your swiss army knife of AS3. They're small (and can be compressed even further with the inflate and deflate methods) and one of the most useful things i've yet learned in AS3.