I'm creating a ByteArrayOutputStream using ZIO Streams i.e.:
lazy val byteArrayOutputStream = new ByteArrayOutputStream()
val sink = ZSink.fromOutputStream(byteArrayOutputStream).contramapChunks[String](_.flatMap(_.getBytes)
val data = ZStream.unwrap(callToFunction).run(sink)
This works fine - now I need to stream this data back to the client using akka http. I can do this:
val arr = byteArrayOutputStream.toByteArray
complete(HttpEntity(ContentTypes.`application/octet-stream`, arr)
which works but of course the toByteArray brings the outputstream into memory i.e. I don't stream the data. I'm missing something obvious - is there an easy way to do this?
You can convert output stream to Akka Stream
Source
:and then simply create a chunked HTTP entity:
More about chunked transfer: https://datatracker.ietf.org/doc/html/rfc7230#section-4.1
For ZIO, you could probably use something like this:
However, you need to find a way to convert ZStream to Akka Stream Source.