Downloading large files in Adobe AIR with Flex

19 June, 2008 (21:35) | Adobe AIR / Flex | By: Jarin Udom

Everyone is probably familiar with the method for downloading files provided in the Flex documentation:

  1. var urlString:String = "http://example.com/myfile.zip";
  2. var urlReq:URLRequest = new URLRequest(urlString);
  3. var urlStream:URLStream = new URLStream();
  4. var fileData:ByteArray = new ByteArray();
  5. urlStream.addEventListener(Event.COMPLETE, loaded);
  6. urlStream.load(urlReq);
  7.  
  8. function loaded(event:Event):void {
  9. urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
  10. writeFile();
  11. }
  12.  
  13. function writeFile():void {
  14. var file:File = File.desktopDirectory.resolvePath("myfile.zip");
  15. var fileStream:FileStream = new FileStream();
  16. fileStream.openAsync(file, FileMode.WRITE);
  17. fileStream.writeBytes(fileData, 0, fileData.length);
  18. fileStream.close();
  19. }

The only problem with this is the file data is stored in memory until you write it to disk. Obviously this will not work well for large files (on the order of hundreds of megabytes).

Here's a better way to download large files that works by using the progress event to write bytes to disk as they come in:

  1. var urlString:String = "http://example.com/myfile.zip";
  2. var urlReq:URLRequest = new URLRequest(urlString);
  3. private var urlStream:URLStream = new URLStream();
  4. private var fileStream:FileStream = new FileStream();
  5.  
  6. urlStream.addEventListener(Event.COMPLETE, loaded);
  7. urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
  8.  
  9. var file:File = File.desktopDirectory.resolvePath("myfile.zip");
  10. fileStream.addEventListener(Event.CLOSE, fileClosed);
  11. fileStream.openAsync(file, FileMode.WRITE);
  12.  
  13. urlStream.load(urlReq);
  14.  
  15. function writeFile(event:ProgressEvent):void {
  16. // only write every 50k or so downloaded
  17. if (urlStream.bytesAvailable > 51200) {
  18. // Read the buffer into a ByteArray and write it to disk
  19. var data:ByteArray = new ByteArray();
  20. urlStream.readBytes(data, 0, urlStream.bytesAvailable);
  21. fileStream.writeBytes(data, 0, data.length);
  22. }
  23. }
  24.  
  25. function loaded(event:Event):void {
  26. // Write any remaining data to the file before closing it
  27. var data:ByteArray = new ByteArray();
  28. urlStream.readBytes(data, 0, urlStream.bytesAvailable);
  29. fileStream.writeBytes(data, 0, data.length);
  30. fileStream.close();
  31. }

Enjoy!

Things You Might Want to Think About Putting on Your Resume

31 May, 2008 (11:56) | Business | By: Jarin Udom

Your Linkedin profile

I'm not sure why more people don't do this. It's the most concentrated way to see how connected and involved you are in business and networking. Get your contacts to write you good recommendations first, if possible.

Your reading list

Delicious Library (Mac), Libra (Windows), and LibraryThing are all good for this. With the desktop apps you'll need to publish your library to your own site. Don't forget to add any relevant Audible books too. This shows how interested you are in learning, and can provide potential employers with insight into other skills you may have outside of the job you are applying for.

Your blogroll

Same idea as the reading list. If you're a programmer, you should at least be reading Joel on Software, Coding Horror, The Daily WTF, and Signal vs. Noise. Find some well-known specialist blogs in your field and add them to your favorite feed reader (Google Reader, NetNewsWire). It's good for you anyway.

Your social bookmark profile

Think Ma.gnolia or Del.icio.us. Same idea as the reading list, except it shows a potential employer what kinds of things you care about enough to want to refer to later. It's all about involvement. Be careful that you mark private anything you wouldn't want a potential employer to see.

Open-source projects

If you haven't contributed to any, start now. Not only is it good for your own skills, it shows potential employers that you're passionate about coding. If you're not a programmer, you can get the same effect by volunteering.

The overarching theme here is that you want to show that you're deeply involved in your field and improving your skills, and the best way to do that is to actually do it. If you're having trouble getting hired at the salary level or position you want, it might be time to start building up your base skills and increasing your involvement in your field.

Now, do like Daniel-san and start painting the fence.

Cheat Gem - Cheat Sheets for Ruby and Rails

31 May, 2008 (01:35) | Ruby / Rails | By: Jarin Udom

Alright, kickin this new blog off with a nice little gem from Err the Blog called Cheat.

Cheat, as you might have guessed from the title of this post, gives you quick access to quick and dirty cheat sheets for Ruby and Rails.

It goes a little like this:

Install the gem

$ gem install cheat

Run the cheat command to see a cheat sheet

$ cheat belongs_to
belongs_to:
  Options for belongs_to

  :class_name - specify the class name of the association. Use it only if that
  name can’t be inferred from the association name. So has_one :author will by
  default be linked to the Author class, but if the real class name is Person,
  you’ll have to specify it with this option.

  :conditions - specify the conditions that the associated object must meet in
  order to be included as a "WHERE" sql fragment, such as "authorized = 1".
  ...

It's easy to add new cheat sheets at http://cheat.errtheblog.com/, so in that spirit I added cheat sheets for Ruby and Rails assertions (they may have changed since I added them).

Just try

$ cheat assertions

or

$ cheat assert_select

You can also add Cheat support in TextMate, thanks to a snippet from Ed Silva:

  1. Open TextMate's bundle editor
  2. Create a new command
  3. Put the following code in the body
    word=${TM_SELECTED_TEXT:-$TM_CURRENT_WORD}
    echo "<html><head><title>Cheat Sheet for $word</title></head><body><pre>";
    /usr/local/bin/cheat $word;
    echo "</pre></body></html>";
    
  4. Change the output to Show as HTML
  5. Click on Activation and set it to a keyboard shortcut (such as ctrl-c)

You can now highlight a word and hit ctrl-c to get the cheat sheet! Crescent fresh!