Problem with WebORB and ByteArray's as parameters

I recently ran into a problem with a drawing application I am working on. It wasn’t a major problem but I am sure someone else will run into it. The problem is with sending ByteArray's through WebORB as a parameter or a variable in a ValueObject.

The project uses Flex, MySQL, PHP and WebORB. A user creates a vector drawing and that drawing can be saved to the database. My approach was to simply serialize the drawing using the ByteArray class and send it through WebORB to a PHP service to insert it into the database. This seemed to work fine until I tried to reload the drawing into the application in another session. I kept getting an error say that the array could not be converted to a ByteArray.

It turns out that if you send a ByteArray through WebORB as a parameter it gets converted to an array of numbers. This kind of makes sense because that is what a ByteArray is. The problem is that the ByteArray I sent is no longer an array of bytes - it is an array of numbers and gets written to the database that way When it was retrieved from the database to be reloaded WebORB recognizes it as an array and doesn't convert it back. When Flex receives it back it is no longer recognized as an array or bytes.

The solution to this problem that I eventually used was to encode the ByteArray as a Base64 string using the Base64Encoder class in the mx.utils package of the framework. Then when it was reloaded I decoded it with the Base64Decoder class.

This is the code snippet I used

var drawing:Drawing = new Drawing();
// other values added to drawing here
var ba:ByteArray = new ByteArray();
ba.writeUTF(VERSION);
ba.writeObject(drawingData);
ba.compress();
ba.position = 0;
var encoder:Base64Encoder = new Base64Encoder();
encoder.encodeBytes(ba);
drawing.drawingData = encoder.toString();
return drawing;

To decode the string when it was reloaded it used this

var decoder:Base64Decoder = new Base64Decoder();
decoder.decode(Drawing(item).drawingData.toString());
var ba:ByteArray = decoder.toByteArray();
ba.uncompress();
var version:String = ba.readUTF();
drawingData = ba.readObject();

VERSION is the version number of the drawing. I use this so that I know if a drawing needs to be updated to a new version when it is reloaded. Also you will notice that I am compressing the ByteArray to make it smaller. In my case it cutes the size in half.

This works like a charm because now to WebORB the parameter is a string and it is passed to the PHP service as is.