Emmanuel Oga
10/01/2020, 7:40 AMOutputStream
, and some code that needs that data but wants to read from an InputStream
. I came up with this but I was wondering if maybe thre's a more straightforward solution:
/**
* Pipe output to an input stream. Uses a thread to write from output to input.
* See: <https://stackoverflow.com/questions/5778658/how-to-convert-outputstream-to-inputstream>
*/
fun ByteArrayOutputStream.asInputStream(): InputStream = PipedInputStream().also { istream ->
PipedOutputStream(istream).let { ostream ->
Thread { ostream.use { writeTo(ostream) } }.start()
}
}
Thanks!Emmanuel Oga
10/01/2020, 7:41 AMVampire
10/01/2020, 7:51 AMByteArrayOutputStream
, you can also simply use a ByteArrayInputStream
. The data is fully in RAM already anyway.Emmanuel Oga
10/01/2020, 8:03 AMEmmanuel Oga
10/01/2020, 8:05 AMByteArrayInputStream(prevOutput.toByteArray())
(doens't seem to be a way to do it without the copy or the pipe)Vampire
10/01/2020, 8:12 AMEmmanuel Oga
10/01/2020, 8:22 AM