Play Framework – Enumerator.outputStream

playframeworkiteratee

A few weeks ago, we’ve introduced a new feature in Play Framework: the Enumerator.outputStream method, allowing you to work with Java API requiring an OutputStream to generate content, for instance the java.util.zip API.

Now, let’s see how easy it is to serve a big Zip generated on-the-fly without memory load with Play Framework.

The Zip generation example

This demo shows how to generate a zip file on-the-fly and directly stream it to an HTTP client without loading it in memory or storing it in a file.

It uses an Enumerator created with the Enumerator.outputStream method.
The OutputStream provided by the method is then plugged to the Java’s ZipOutputStream.

For the example, we have generated a zip containing 100 text files, and each text files contains 100’000 random long numbers (yes, 100’000 !).

The zip size is approximatively 100 Mb. (and is generated in about 3Mb/s in my machine in localhost, but this can be improved)

The huge benefit of this is the download starts instantly, it means the Zip is streamed while it is generated.

Show me the code!

Internally, it is implemented with a Concurrent.unicast, and a simple implementation of an OutputStream pushing into the unicast’s channel:

About Iteratee and Enumerator

If you want to learn more about Iteratee concepts in Play Framework, I recommend you this article.

generative artist who uses code to make art, explores the frontier of abstract art with algorithms pushing forward to more realistic scenery. Explore physical art via 'Plotting', which consist of drawing with fountain pens on robot. I don't do prints, I do plots: Every physical outcome is truly unique!