Consider I have a cold source of UTF-8 bytes (e. g.: reading a file on disk, or the body of an HTTP response), in a form of a Flow<Byte>
. How do I convert the above source to a flow of strings?
In other words, I want the following behaviour:
/*
* A multi-line string, not terminated with a newline character.
*/
val string = """
first line
第二行
третья строка
""".trimIndent()
assertNotEquals('\n', string.last())
assertEquals(2, string.asSequence().count { it == '\n' })
val source: Flow<Byte> = string.toByteArray().asSequence().asFlow()
val transformed: Flow<String> = TODO()
val target = runBlocking {
transformed.toList(mutableListOf()).toTypedArray()
}
assertArrayEquals(
arrayOf("first line", "第二行", "третья строка"),
target
)
As an extra restriction, this is a Kotlin/JS project, so java.io
APIs can't be used.