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
?
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.
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.
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
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With