views:

144

answers:

1

I've been tinkering with IExternalizable, but I've noticed some unexpected behavior. I've got this class:

public function readExternal(input:IDataInput):void {
    input.readObject();
    input.readObject();
    input.readObject();
}

public function writeExternal(output:IDataOutput):void {
    output.writeObject("first string");
    output.writeObject(424242);
    output.writeObject("second string");
}

But when I try to serialize this class using AMF and send it to a remote server (via RemoteObject), Charles shows me that the request looks like this:
unexpected result

But it seems wrong that my serialized object is leaking out into the rest of the request.

So, what am I doing wrong? Is there some part of the documentation I've missed?

+1  A: 

You code seems fine, however you should serialize using the proper methods (writeUTF for strings, writeInt for int etc). Anyway Charles seems to not work properly with objects implementing IExternalizable (I'm using version 3.4.1), so you should not rely on what it is showing.

Not directly related to your question - do you really need to use IExternalizable? You are going to lose some benefits related to AMF compression algorithm (unless you are not going to implement all this things in your writeExternal method).

Cornel Creanga
What is the benefit of using the "proper" serialization methods? And if you're going to make claims like "it's more efficient", can you provide evidence to support the claim?
David Wolever
Also, what compression benefits am I losing with IExternalizable?
David Wolever
Thanks for the tip about Charles, though. It looks like, due to the way `IExternalizable` is implemented, Charles has no way of knowing where the object stops and the rest of the request continues.
David Wolever
Regarding the serialization methods - in my opinion the code looks better. I assume also some small improvements compared with readObject, but I didn't looked yet in FP source code.
Cornel Creanga
The benefits are related to the size of the AMF packet. The default serialization algorithm is able to detect the duplicate strings andobjects and to use references when the objects are found again in the stream. If you use your custom serialization you will lose this benefit. I wrote some time ago a blog post about that http://cornelcreanga.com/2009/09/blazeds-amf-and-read-only-properties/
Cornel Creanga