Be a Supporter!
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
AS3: ByteArrays Jan. 25th, 2013 @ 04:26 PM Reply

---
AS3: Main

Category: Intermediate
---

--- 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?

Bytes:
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.

Byte Arrays:
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[0] and expect it to work, since most of what goes into a byte array is larger than a single byte.
For example:
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):
bytesAvailable
clear
length
position
read*
write*

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.

readNext():Number {
	return _myVector[_currentPosition++];
}

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.

available():uint {
	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.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to AS3: ByteArrays Jan. 25th, 2013 @ 05:34 PM Reply

At 1/25/13 04:26 PM, egg82 wrote: A byte can hold any number from 0 to 255

An unsigned byte perhaps.
A signed byte can represent -128 to 127 (assuming two's compliment).

Not that that really applies to AS3, but it's an important distinction.

leaving 255! possibilities (the exclamation point is mathematical, meaning 255 * 254 * 253 * 252, etc)

That's called a factorial. :)
Not sure what you're referring to by "possibilities" though. There is certainly not 255! permutations available in a byte (there's 256).

You're aware of how massive of a number 255! is, right?

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to AS3: ByteArrays Jan. 25th, 2013 @ 05:43 PM Reply

At 1/25/13 05:34 PM, Diki wrote: An unsigned byte perhaps.
A signed byte can represent -128 to 127 (assuming two's compliment).

Not that that really applies to AS3, but it's an important distinction.

that's a good point that I forgot. It's been a while since I learned bytes.

That's called a factorial. :)

math is not my strongest point, i'll admit :P

Not sure what you're referring to by "possibilities" though. There is certainly not 255! permutations available in a byte (there's 256).

I forgot 0. How could I forget 0?

You're aware of how massive of a number 255! is, right?

right again! I actually though about that one while I was writing it, and I went with a gut decision on keeping it without actually checking first. Though I probably should have remembered something as basic as that.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Jin
Jin
  • Member since: Sep. 9, 2006
  • Offline.
Forum Stats
Supporter
Level 49
Blank Slate
Response to AS3: ByteArrays Jan. 25th, 2013 @ 10:34 PM Reply

Talking about ByteArrays without mentioning bitwise operations and flags.

I can imagine how messy your socket information is.


BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to AS3: ByteArrays Jan. 25th, 2013 @ 10:43 PM Reply

At 1/25/13 10:34 PM, Jin wrote: Talking about ByteArrays without mentioning bitwise operations and flags.

I can imagine how messy your socket information is.

Diki's already got that covered.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature