Play / Akka Integration with Java OutputStreams

I am writing a game! An application that provides a REST API that allows users to create PDF reports. I am limited by the requirements to use the old Java API to create the actual report. This library has a generate(OutputStream out, ...) method, that is, it accepts java.io.OutputStream , where it writes the resulting report.

My problem is integrating this with Play / Akka to serve content in Chunked Encoding. To do this, I need to create an Enumerator[Array[Byte]] , which somehow contains an OutputStream from the Java library. I came up with a working solution that uses the PipedOutputSteam / PipedInputStream duo to transfer the output from the library to Enumerator using Enumerator.fromStream .

I am wondering if there is a better way to achieve this, but I cannot find an explicit example in Akka or Play! documentation that integrates Enumerators with OutputStreams. I know that locking the Java IO library is a limiting factor when developing a better solution, but there may be a more accurate way to do this. Any thoughts?

Following actions

Assuming I'm using Enumerator.outputStream , what would be a safe way to move the actual generation of reports to another player (maybe on another machine)? For example, I assume that sending an OutputStream is unsafe (and will only work locally).

+4
source share
1 answer

Play 2.1 has the following method: Enumerator.outputStream(a: (OutputStream) ⇒ Unit): Enumerator[Array[Byte]] , which probably does exactly what you want.

The implementation specifically uses other classes of Play 2.1, so if you are using Play 2.0, you will need to do additional digging to achieve the same.

+1
source

All Articles