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...