Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Source[ByteString, Any] to InputStream

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?

like image 859
kostya Avatar asked May 28 '15 09:05

kostya


People also ask

How do you convert an InputStream to Bytearray in Java?

Example 1: Java Program to Convert InputStream to Byte Arraybyte[] array = stream. readAllBytes(); Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.

Is there any method InputStream read string directly?

The read() method of the InputStreamReader class accepts a character array as a parameter and reads the contents of the current Stream to the given array. To convert an InputStream Object int to a String using this method.


2 Answers

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

like image 92
Bennie Krijger Avatar answered Sep 23 '22 21:09

Bennie Krijger


You could try using an OutputStreamSink that writes to a PipedOutputStream and feed that into a PipedInputStream that your other code uses as its input stream. It's a little rough of an idea but it could work. The code would look like this:

import akka.util.ByteString import akka.stream.scaladsl.Source import java.io.PipedInputStream import java.io.PipedOutputStream import akka.stream.io.OutputStreamSink import java.io.BufferedReader import java.io.InputStreamReader import akka.actor.ActorSystem import akka.stream.ActorFlowMaterializer  object PipedStream extends App{   implicit val system = ActorSystem("flowtest")   implicit val mater = ActorFlowMaterializer()    val lines = for(i <- 1 to 100) yield ByteString(s"This is line $i\n")   val source = Source(lines)    val pipedIn = new PipedInputStream()   val pipedOut = new PipedOutputStream(pipedIn)         val flow = source.to(OutputStreamSink(() => pipedOut))   flow.run()    val reader = new BufferedReader(new InputStreamReader(pipedIn))   var line:String = reader.readLine   while(line != null){     println(s"Reader received line: $line")     line = reader.readLine   }            } 
like image 30
cmbaxter Avatar answered Sep 22 '22 21:09

cmbaxter