Wondering if there is a better way to deal with th...
# codereview
j
Wondering if there is a better way to deal with the interop between Java Rx and Kotlin specifically for the Java BodyPublisher. I need to get a ByteArray from the body publisher to pass into an AWS SDK for signing, am currently doing this but it feels really gross:
Copy code
val body = HttpRequest.BodyPublishers.ofByteArray(ByteArray(0))
val returnedBody = body.let {
    bodyByteArray = request.bodyPublisher().orElseGet { HttpRequest.BodyPublishers.noBody() }.let {
        // Default to empty ByteArray to send on
        var buffer = ByteArray(0)

        runBlocking {
            // Convert an Apache BodyPublisher (which is a Java Flow), convert it to a ReactiveStream publisher so that I could use the "proper" extension class in kotlinx coroutines to run... .collect() on it. Yikes
            FlowAdapters.toPublisher(it).asFlow().collect {
                // The underlying Publisher emits a ByteBuffer. Kotlin lets use convert to an array which allows us to add them up easily. KOTLIN!
                buffer += it.array()
            }
        }
        return@let buffer
}