Red5 Vector Support
August 3rd, 2011 by Paul Gregoire
It all started for me last year, I noticed Jean-Philippe Auclair blogged about reading AS3 Vector’s in Java and provided byte-level examples. Since the blogged examples were written in Java, I saw this as an invitation to implement them in Red5. I knew of Vector’s in Java but had no idea they had been implemented in actionscript and with what looked to me like generics. In AS3 the generic notation denotes the base type for each element in the Vector, which is also how it works in Java. The main differences between the implementations are that a Vector in AS3 is basically a faster version of Array and in Java it is a synchronized List-type collection. Similarly each element must be either “null” or an instance of the base type. Java is a lot more flexible with the instances in this case, because the element merely needs to implement or extend the base type. In AS3 handling is different, a Vector of DisplayObject type will not accept a Sprite instance.
Before I get down to the byte-level stuff, I want to note that this information may not be exactly correct in terms of what or how these items are constructed by the flash player; they are however the result of reverse engineering the data. Anyone with more information or a correction is welcome to send it my way.
The following examples show how the Vector is constructed in AS3 and then its representation in bytes. The byte arrays are encoded as big endian and are not compressed.
Vector with numbers and a string
A Vector containing two Number objects and a simple repeating string:
var v:Vector.<Object> = new Vector.<Object>(); v[0] = 1.0; v[1] = 3.0; v[2] = "aaa";
Trace:
[1.0, 3.0, aaa]
Raw bytes:
10 07 00 01 04 01 04 03 06 07 61 61 61 0 = Vector 1..3 = Length == 3 4 = Type == Number 5 = Value == 1.0 6 = Type == Number 7 = Value == 3.0 8 = Type == String 9 = String length == 3 10..12 = "aaa"
Vector inside a Vector with other objects
A Vector containing the previous examples Vector in addition to another Vector of int type:
var v:Vector.<Object> = new Vector.<Object>(); v[0] = 1.0; v[1] = 3.0; v[2] = "aaa"; var v2:Vector.<int> = new Vector.<int>(); v2.push(1); v2.push(2); v[3] = v2;
Trace:
[1.0, 3.0, aaa, [1, 2]]
Raw bytes:
10 09 00 01 04 01 04 03 06 07 61 61 61 0d 05 00 00 00 00 01 00 00 00 02 0 = Vector 1..3 = Length == 4 4 = Type == Number 5 = Value == 1.0 6 = Type == Number 7 = Value == 3.0 8 = Type == String 9 = String length == 3 10..12 = "aaa" 13 = Type == Vector 14 = Length == 2 15 = Reference 16..19 = Value == 1 20..23 = Value == 2
Mixed Vector with class instance member
A Vector containing a simple String, a “null”, and the instance of a custom class:
var v:Vector.<Object> = new Vector.<Object>(); v[0] = "foo"; v[1] = null; v[2] = new org.red5.test.Foo3();
Trace:
[foo,null,[Foo3=3]]
Raw bytes:
10 07 00 01 06 07 66 6f 6f 01 0a 13 25 6f 72 67 2e 72 65 64 35 2e 74 65 73 74 2e 46 6f 6f 33 00 04 03
The “Foo3″ classes
Actionscript
package org.red5.test {
[RemoteClass(alias="org.red5.test.Foo3")]
public class Foo3 {
public var foo:int;
public function Foo3() {
this.foo = 3;
}
public function toString() : String {
return "[Foo3=" + foo + "]";
}
}
}
Java
public class Foo3 implements IExternalizable {
private int foo;
public void setFoo3(int foo) {
this.foo = foo;
}
public int getFoo() {
return foo;
}
public void readExternal(IDataInput input) {
this.foo = input.readInt();
}
public void writeExternal(IDataOutput output) {
output.writeInt(foo);
}
}
The original post does not contain details for writing back to Flash Player so I simply reversed the process once I had read working in the server. To test round-trip (deserialization/serialization), I used this method in my Red5 application:
public Vector<?> echo(Vector<Object> vector) {
log.debug("echo - vector arg: {}", vector);
return vector;
}
The method accepts a Vector containing any type of object and returns it back to the client as a response. On the client you’ll need a pair of methods to both hit the server and to get the response.
public function echo() : void {
var v:Vector.<int> = new Vector.<int>();
v.push(42);
nc.call("echo", new Responder(echoRecv), v);
}
public function echoRecv(o:Object) : void {
var vectorIn;
if (o is Vector.<int>) {
vectorIn = Vector.<intt>(o);
} else if (o is Vector.<uint>) {
vectorIn = Vector.<uint>(o);
} else if (o is Vector.<Number>) {
vectorIn = Vector.<Number>(o);
} else if (o is Vector.<Object>) {
vectorIn = Vector.<Object>(o);
}
trace("Got vector:" + vectorIn);
}
Lastly, to use Vector’s with Red5 you will need the latest trunk with a revision of 4264 or newer.
For your reference, the AS3 doc page for Vector may be found here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html
Note: Vector’s are available to Flash Player starting with the version 10 beta.
- 2 Comments »
- Posted in Flash, Flex, Red5

Thanks for the link
Very useful post indeed!
I wanted to post on this for a long time now…
Thanks for doing it !
August 3rd, 2011 at 2:35 pm
[...] Please refer to this: http://blog.infrared5.com/2011/08/red5-vector-support/ [...]