score:4
In general you can't safely use any OutputStream
with the Enumerator/Iteratee framework because OutputStream
doesn't support non-blocking pushback. However, if you can control the writing to the OutputStream
you can hack together something like:
val baos = new ByteArrayOutputStream
val zos = new ZipOutputStream(baos)
val enumerator = Enumerator.generateM {
Future.successful {
if (moreDateToWrite) {
// Write data into zos
val r = Some(baos.toByteArray)
baos.reset()
r
} else None
}
}
If all you need is compression, take a look at the Enumeratee
instances provided in play.filters.gzip.Gzip
and the play.filters.gzip.GzipFilter
filter.
score:0
The only backpressure mechanism for OutputStream
is blocking the thread. So one way or another, there will have to be a thread that is able to be blocked.
One way is to use piped streams.
import java.io.OutputStream
import java.io.PipedInputStream
import java.io.PipedOutputStream
import play.api.libs.iteratee.Enumerator
import scala.concurrent.ExecutorContext
def outputStream2(a: OutputStream => Unit, bufferSize: Int)
(implicit ec1: ExecutionContext, ec2: ExecutionContext) = {
val outputStream = new PipedOutputStream
Future(a(outputStream))(ec1)
val inputStream = new PipedInputStream(pipedOutputStream, bufferSize)
Enumerator.fromStream(inputStream)(ec2)
}
Since the operations are blocking, you must take care to prevent deadlock.
Either use two different thread pools, or used a cached (unbounded) thread pool.
Source: stackoverflow.com
Related Query
- How to use OutputStreams with chunked response in Play 2.1
- How to stream Anorm large query results to client in chunked response with Play 2.5
- How to show streams inline as HTTP chunked response with play framework
- How to use IntelliJ with Play Framework and Scala
- How to use Joda DateTime with Play Json
- In Play 2.4 with DI, how to use a service class in "Secured" trait?
- Chunked Response from an Iterator with Play Framework in Scala
- How to use play-plugins-mailer with Play 2.3 and Scala 2.11?
- How to use the Play Framework with Google App Engine with locally installed Java 7?
- How to use SORM framework with Play Framework?
- how can i use lift-mongo-record with play framework?
- How to send large response with play scala
- How do I use Zookeeper as "Database" with Play Framework instead of traditional JDBC?
- How to use nullable columns with Anorm and Play Framework?
- How to use spark and mongo with play to calculate prediction?
- How to use Memcached with the Scala Play Framework 2.2?
- How to use fcm(firebase cloud messaging) with scala play framework to send notification?
- How to use cdn url with play framework and scala for image display?
- How to use jQuery UI with Play Scala
- How to use Column.isin with list?
- How to use third party libraries with Scala REPL?
- In Scala, how to use Ordering[T] with List.min or List.max and keep code readable
- How to use orderby() with descending order in Spark window functions?
- How to use scala trait with `self` reference?
- How to create a custom 404 page handler with Play 2.0?
- How to load JSON file using Play with Scala
- How to use s3 with Apache spark 2.2 in the Spark shell
- How to use an Akka Streams SourceQueue with PlayFramework
- How to use stackable trait pattern with Akka actors?
- How to use sbt with Google App Engine?
More Query from same tag
- Is multiple assignment of tuples slower than multiple assignment statements?
- Implicit conversion of a generic container for an implicit parameter in Scala
- Checking authentication with play2-auth
- How to express this existential type from Haskell in Scala?
- Orika class com.sun.proxy is not accessible
- Algorithm mixing
- How to correctly verify scheduled invocations in ZIO Test
- Covariant type FParam occurs in contravariant position in type Seq[FParam] of value guesses
- Summing a list of OptionT[Future, BigDecimal] in Scalaz
- reading zip file from s3 bucket using scala spark
- How to look up the type of a method returning a type parameter with scala reflection?
- Why use Collection.empty[T] instead of new Collection[T]()
- Where's the error in this Scala code?
- Guice - bind interface to the only implementation available
- Find product of every digit in the given number
- Covariance and Contravariance in Scala
- Scala functional interface traits
- Using Streams ... still got Out of Memory
- Applying an argument list to curried function using foldLeft in Scala
- Cannot use the futures-util crate with Actix because the trait Future is not implemented
- How can I set up a Scala/Java workflow to remote deploy jar and remote debug THROUGH a jumpserver?
- group by list on same field with different case classes scala
- Can't unmarshall json HttpEntity with spray-json
- Exploding multiple array columns in spark for a changing input schema
- Scalacheck prop type mismatch
- Delaying trait initialization
- Calling from/until/range twice on a mutable TreeSet results in empty sequence
- Having trouble with getting basic auth from play framework
- Scala: get outer class from inner class in constructor
- Array-Languages like Code Reuse in Scala