0 Replies Latest reply on Jan 11, 2008 9:09 AM by Pete Mackie

    AS3 Data Coercion Problem

    Pete Mackie Level 1
      AS3 Data Coercion Problem

      From PHP server RemoteObject AMF3 serialization, I receive a photo object per below into my Flex client:

      package WPhoto {

      public class PhotoObj {
      public var photo:*;
      public var photoWidth:int;
      public var photoHeight:int;

      The above 'photo' property String of a one-byte (UTF-8) '.jpg' photo data. Due to PHP's rather limited set of primitive data types this is the best I can do per returning photo data to the Flex client. (Extreme tries at PHP data structure chicanery hasn't worked for me.)

      I need photo property in a DisplayObject format such that I can render photo as a child of my App's DisplayObject. The AS3 Loader class performs this task, when sourced from an AS3 ByteArray.

      o Loader.loadBytes(bytes:ByteArray, context:LoaderContext = null):void
      Loads from binary data stored in a ByteArray object.

      My dilemma is "how to" convert a UTF-8 string of photo data into a ByteArray. I've been through the ByteArray.writeXXX() class methods without finding anything close to meeting my requirements. AS3 String readers expect UTF-16 data, so reading AS3 string reading is not a solution. AS3 has no Byte data type, accordingly reading my photo data UTF-8 string with a "for loop" into a AS3 UTF-16 string, or whatever, is not viable. Is there a single-byte reader to 'whatever' writer I'm not aware of?

      If I was sourcing photos from a Java based server, my photo rendering dilemma would be over. The server to client packets would be "java.lang.Byte[]" => flash.utils.ByteArray and voilà we could easily and directly render photos via the Loader.loadBytes() method.

      I can tentatively extend one of the AS3 ByteArray class write methods if I thought there was a way to read UTF-8 streams. Before digging down into the ByteArray class on a feasibility basis, I thought it first best to ask around the experts per 1) can ByteArray be extended for my requirement, or 2) is there a better way to approach this, which I have not thought of?

      Pete Mackie
      Seaquest Software
      Adobe Community Expert - Flex