Request: fix coroutine pool for server data

This commit is contained in:
dece 2022-02-15 17:09:25 +01:00
parent 59e664ec5c
commit fcf12f09d2

View file

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.BufferedInputStream import java.io.BufferedInputStream
@ -46,13 +47,12 @@ class Request(private val uri: Uri, private val keyManager: KeyManager? = null)
socket.outputStream.write("$uri\r\n".toByteArray()) socket.outputStream.write("$uri\r\n".toByteArray())
val channel = Channel<ByteArray>() val channel = Channel<ByteArray>()
scope.launch { scope.launch(Dispatchers.IO) {
val buffer = ByteArray(1024) val buffer = ByteArray(1024)
var numRead: Int var numRead: Int
socket.inputStream.use { socket_input_stream -> socket.inputStream.use { socket_input_stream ->
BufferedInputStream(socket_input_stream).use { bis -> BufferedInputStream(socket_input_stream).use { bis ->
try { try {
@Suppress("BlockingMethodInNonBlockingContext") // what u gonna do
while ((bis.read(buffer).also { numRead = it }) >= 0) { while ((bis.read(buffer).also { numRead = it }) >= 0) {
val received = buffer.sliceArray(0 until numRead) val received = buffer.sliceArray(0 until numRead)
channel.send(received) channel.send(received)