How to convert Source[ByteString, Any] to InputStream

kostya picture kostya · May 28, 2015 · Viewed 13.5k times · Source

akka-http represents a file uploaded using multipart/form-data encoding as Source[ByteString, Any]. I need to unmarshal it using Java library that expects an InputStream.

How Source[ByteString, Any] can be turned into an InputStream?

Answer

Bennie Krijger picture Bennie Krijger · Jan 18, 2016

As of version 2.x you achieve this with the following code:

import akka.stream.scaladsl.StreamConverters
...
val inputStream: InputStream = entity.dataBytes
        .runWith(
           StreamConverters.asInputStream(FiniteDuration(3, TimeUnit.SECONDS))
        )

See: http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.1/scala/migration-guide-1.0-2.x-scala.html

Note: was broken in version 2.0.2 and fixed in 2.4.2