james
02/11/2020, 8:36 PMval 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
}