{"id":260,"date":"2011-02-08T16:00:41","date_gmt":"2011-02-08T14:00:41","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=260"},"modified":"2011-02-08T16:00:41","modified_gmt":"2011-02-08T14:00:41","slug":"flex-java-frankenstein","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2011\/02\/08\/flex-java-frankenstein\/","title":{"rendered":"Flex + Java = Frankenstein?"},"content":{"rendered":"<p>Lately I&#8217;ve been working on a small upload tool in Flex\/AIR.<br \/>\nTo verify that an upload has succeeded, I created a hash (md5) of the entire file and sent that to the backend for verification. The <a href=\"https:\/\/github.com\/mikechambers\/as3corelib\" target=\"_blank\">AS3CoreLib<\/a> contains a nice MD5 class, MD5Stream, but it&#8217;s much too slow for creating a checksum of files.<\/p>\n<p>Actually, it&#8217;s so slow that when I was uploading a 5+ MB file the application would slow down so much that the upload speed dropped to a third of the speed without feeding the MD5Stream data during uploading.<br \/>\nLuckily, with AIR 2.0 <a href=\"http:\/\/help.adobe.com\/en_US\/FlashPlatform\/beta\/reference\/actionscript\/3\/flash\/desktop\/NativeProcess.html\" target=\"_blank\">NativeProcess<\/a> has been introduced, it made it much easier to &#8220;outsource&#8221; complicated calculation to a process that can deal with that much better, i.e. Java.<\/p>\n<p>The nice thing about Java is that it&#8217;s cross-platform just as AIR, and these days many people will have the JRE installed.\u00c2\u00a0 So first of all, I wrote a small Java class that would print out an MD5 hash of a file based on the path that I passed. I jarred the class and added it to my AIR project.<br \/>\nThe second step was to call my Java class instead of the AS3 MD5Stream class. This is surprisingly easy. The NativeProcess documentation comes with a pretty clear example and <a href=\"http:\/\/www.riaspace.com\/\" target=\"_blank\">Piotr Walczyszyn<\/a> has created a small framework, <a href=\"http:\/\/www.riaspace.com\/2010\/01\/flerry-flex-java-bridge-for-adobe-air-2-0\/\" target=\"_blank\">Flerry<\/a>, to let Flex talk to Java and vice versa.<\/p>\n<p>Looking at how Flerry is built will give you some more insight on how to deal with NativeProcess.<br \/>\nFor my application, I only use the <a href=\"http:\/\/code.google.com\/p\/flerry\/source\/browse\/trunk\/flerry\/src-as3\/net\/riaspace\/flerry\/BaseStartupInfoProvider.as\" target=\"_blank\">BaseStartupInfoProvider<\/a> class from Flerry to find where Java is installed on the user&#8217;s machine. After that I set up all event listeners as per the NativeProcess documentation and call NativeProcess.start() with a NativeProcessStartupInfo as passed in parameter.<br \/>\nNow my uploads are fast again and I can generate a checksum in a fraction of the time I could before.<\/p>\n<p>There are unfortunately some downsides to this approach. You have to deploy your AIR application as <a href=\"http:\/\/help.adobe.com\/en_US\/air\/build\/WS789ea67d3e73a8b22388411123785d839c-8000.html\">native installer<\/a> to be able to use the NativeProcess class. So instead of having one AIR file, you&#8217;ll end up with four native installers (.exe, .dmg, .deb, .rpm). On the plus side, you can create the native installers from an AIR file without needing to resign the application.<br \/>\nAnother downside is that now I&#8217;m relying on two runtimes being installed on the end user&#8217;s machine, AIR and Java.<br \/>\nMy final thought is that now that I am outsourcing some calculations to Java, why not built the whole thing in Java? Then again, building a decent UI can take ages using Java. To me, my application feels like I&#8217;m creating a bit of a Frankenstein.<\/p>\n<p>All in all I&#8217;m glad I now have a fast way to generate a checksum and my uploads aren&#8217;t slowed down by it any more.<\/p>\n<p>Links:<\/p>\n<ul>\n<li><a href=\"http:\/\/help.adobe.com\/en_US\/FlashPlatform\/beta\/reference\/actionscript\/3\/flash\/#desktop\/NativeProcess.html\" target=\"_blank\">NativeProcess<\/a><\/li>\n<li><a href=\"http:\/\/help.adobe.com\/en_US\/air\/build\/WS789ea67d3e73a8b22388411123785d839c-8000.html\">Creating a native installer<\/a><\/li>\n<li><a href=\"http:\/\/www.riaspace.com\/2010\/01\/flerry-flex-java-bridge-for-adobe-air-2-0\/\" target=\"_blank\">Flerry<\/a><\/li>\n<li><a href=\"http:\/\/code.google.com\/p\/flerry\/\" target=\"_blank\">Flerry source code<\/a><\/li>\n<\/ul>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Lately I&#8217;ve been working on a small upload tool in Flex\/AIR. To verify that an upload has succeeded, I created a hash (md5) of the entire file and sent that to the backend for verification. The AS3CoreLib contains a nice &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2011\/02\/08\/flex-java-frankenstein\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,93,65,52],"tags":[],"class_list":["post-260","post","type-post","status-publish","format-standard","hentry","category-actionscript-30","category-air","category-flex","category-java"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":3,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"predecessor-version":[{"id":263,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/260\/revisions\/263"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}