views:

1183

answers:

1

Hi all,

I have a web application developed in Adobe Flex 3 and Python 2.5 (deployed on Google App Engine). A RESTful web service has been created in Python and its results are currently in an XML format which is being read by Flex using the HttpService object.

Now the main objective is to compress the XML so that there is as less a time between the HttpService send() method and result events. I looked up Python docs and managed to use zlib.compress() to compress the XML result.

Then I set the HttpService result type from "xml" to "text" and tried using ByteArrays to uncompress the string back to XML. Here's where I failed. I am doing something like this:

var byteArray:ByteArray = new ByteArray();
byteArray.writeUTF( event.result.toString() );
byteArray.uncompress();
var xmlResult:XML = byteArray.readUTF();

Its throwing an exception at byteArray.uncompress() and says unable to uncompress the byteArray. Also when I trace the length of the byteArray it gets 0.

Unable to figure out what I'm doing wrong. All help is appreciated.

-- Edit --

The code:

# compressing the xml result in Python
print zlib.compress(xmlResult)

# decompresisng it in AS3
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTF( event.result.toString() );
byteArray.uncompress()

Event is of type ResultEvent.

The error:

Error: Error #2058: There was an error decompressing the data.

The error could be because the value of byteArray.bytesAvailable = 0 which means the raw bytes python generated hasn't been written into byteArray properly..

-- Sri

+1  A: 

What is byteArray.writeUTF( event.result.toString() ); supposed to do? The result of zlib.compress() is neither unicode nor "UTF" (meaningless without a number after it!?); it is binary aka raw bytes; you should neither decode it nor encode it nor apply any other transformation to it. The receiver should decompress immediately the raw bytes that it receives, in order to recover the data that was passed to zlib.compress().

Update What documentation do you have to support the notion that byteArray.uncompress() is expecting a true zlib stream and not a deflate stream (i.e. a zlib stream after you've snipped the first 2 bytes and the last 4)?

The Flex 3 documentation of ByteArray gives this example:

bytes.uncompress(CompressionAlgorithm.DEFLATE);

but unhelpfully doesn't say what the default (if any) is. If there is a default, it's not documented anywhere obvious, so it would be a very good idea for you to use

bytes.uncompress(CompressionAlgorithm.ZLIB);

to make it obvious what you intend.

AND the docs talk about a writeUTFBytes method, not a writeUTF method. Are you sure that you copy/pasted the exact receiver code in your question?

Update 2

Thanks for the URL. Looks like I got hold of the "help", not the real docs :=(. A couple of points:

(1) Yes, there is an explicit inflate() method. However uncompress DOES have an algorithm arg; it can be either CompressionAlgorithm.ZLIB (the default) or CompressionAlgorithm.DEFLATE ... interestingly the latter is however only available in Adobe Air, not in Flash Player. At least we know the uncompress() call appears OK, and we can get back to the problem of getting the raw bytes onto the wire and off again into a ByteArray instance.

(2) More importantly, there are both writeUTF (Writes a UTF-8 string to the byte stream. The length of the UTF-8 string in bytes is written first, as a 16-bit integer, followed by the bytes representing the characters of the string) and writeUTFBytes (Writes a UTF-8 string to the byte stream. Similar to the writeUTF() method, but writeUTFBytes() does not prefix the string with a 16-bit length word).

Whatever the merits of supplying UTF8-encoded bytes (nil, IMHO), you don't want a 2-byte length prefix there; using writeUTF() is guaranteed to cause uncompress() to bork.

Getting it on to the wire: using Python print on binary data doesn't seem like a good idea (unless sys.stdout has been nobbled to run in raw mode, which you didn't show in your code).

Likewise doing event.result.toString() getting a string (similar to a Python unicode object, yes/no?) -- with what and then encoding it in UTF-8 seem rather unlikely to work.

Given I didn't know that flex existed until today, I really can't help you effectively. Here are some further suggestions towards self-sufficiency in case nobody who knows more flex comes along soon:

(1) Do some debugging. Start off with a minimal XML document. Show repr(xml_doc). Show repr(zlib_compress_output). In (a cut-down version of) your flex script, use the closest function/method to repr() that you can find to show: event.result, event.result.toString() and the result of writeUTF*(). Make sure you understand the effects of everything that can happen after zlib.compress(). Reading the docs carefully may help.

(2) Look at how you can get raw bytes out of event.result.

HTH, John

John Machin
zlib.compress( xml ) gives me raw bytes as a string? My question then would be how to "feed" these raw bytes into a ByteArray and then use the uncompress method to read the XML string.Or are you saying that I need to use base64.encode() to encode the raw bytes into string? Sorry if the question appears stupid.. :P
Srirangan
"neither decode it nor encode it" == I said that you "need to use base64.encode()"??? Quite simply, the receiver has to reverse out whatever the sender did. And we don't know for sure what the sender did because you didn't show the sending code. Please edit your question to include (a) the precise error message (b) the sender code. What type is event.result.toString()? Have you tried uncompressing that [i.e. what I thought I was saying]?
John Machin
Okay, I've added the reuqired information in the question. Thanks..
Srirangan
"Update What documentation do you have to support the notion that byteArray.uncompress() is expecting a true zlib stream and not (a) a deflate stream (i.e. a zlib stream after you've snipped the first 2 bytes and the last 4) or (b) something else?" --- The ByteArray has specific inflate() / deflate() methods for dealing with "deflated" streams...
Srirangan
Please supply a URL that points to that. Do you mean to say that the URL I gave in my answer is not the for the ByteArray class of the Adobe Flex 3 language?? BTW I have to go out now; let's hope someone who knows more about the language comes along :-)
John Machin
Check this out: http://www.adobe.com/livedocs/flex/3/langref/flash/utils/ByteArray.html
Srirangan