Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Play Framework essentials

You're reading from  Play Framework essentials

Product type Book
Published in Sep 2014
Publisher
ISBN-13 9781783982400
Pages 200 pages
Edition 1st Edition
Languages
Author (1):
Julien R Foy Julien R Foy
Author Profile Icon Julien R Foy
Julien R Foy
Toc

Table of Contents (14) Chapters Close

Play Framework Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Building a Web Service 2. Persisting Data and Testing 3. Turning a Web Service into a Web Application 4. Integrating with Client-side Technologies 5. Reactively Handling Long-running Requests 6. Leveraging the Play Stack – Security, Internationalization, Cache, and the HTTP Client 7. Scaling Your Codebase and Deploying Your Application Index

Streaming results using enumerators


Iteratees make it possible to consume streams of data in a non-blocking way. Conversely, enumerators produce data streams in a non-blocking way. They are useful when you need to send large results, or if your result is built from an intermittent data source. An Enumerator[A] object defines a stream of values of type A.

The simplest way to create an enumerator is to use one of the methods of the Enumerator object. For instance, you can easily convert a java.io.InputStream class or a java.io.File class to an Enumerator[Array[Byte]] object as follows:

import play.api.libs.iteratee.Enumerator
Enumerator.fromStream(inputStream)
Enumerator.fromFile(file)

To send a stream of data as a response body, it is better to explicitly set the Content-Length response header so that the connection can be kept alive to serve further requests. Alternatively, you can use the chunked transfer encoding as follows:

Ok.chunked(Enumerator.fromFile(new File("foo.txt")))

To use the chunked...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime