Migrate from deprecated ReceiveChannel.poll to ReceiveChannel.tryReceive

This commit is done to migrate to 1.6.0 coroutines 
(where this deprecation is a compilation error)

GitOrigin-RevId: af427c9b637fe9fe5d11a7cd6f432c497e251899
This commit is contained in:
Nikita Bobko
2022-01-03 18:44:51 +01:00
committed by intellij-monorepo-bot
parent d470865253
commit 53ef27adc7

View File

@@ -5,6 +5,7 @@ import com.google.protobuf.ByteString
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.onClosed
import kotlinx.coroutines.runBlocking
import java.io.IOException
import java.io.InputStream
@@ -51,14 +52,13 @@ class ChannelInputStream(private val readChannel: ReceiveChannel<ByteString>) :
var chunk = carryChunk
try {
do {
val nextChunk = readChannel.poll() ?: break
val nextChunk = readChannel.tryReceive()
.onClosed { if (it is CancellationException) throw IOException(it) }
.getOrNull() ?: break
chunk = chunk.concat(nextChunk)
}
while (true)
}
catch (e: CancellationException) {
throw IOException(e)
}
finally {
carryChunk = chunk
}