diff --git a/app/src/main/java/com/github/catvod/utils/DownloadMT.kt b/app/src/main/java/com/github/catvod/utils/DownloadMT.kt index be923c94..7562b090 100644 --- a/app/src/main/java/com/github/catvod/utils/DownloadMT.kt +++ b/app/src/main/java/com/github/catvod/utils/DownloadMT.kt @@ -9,15 +9,15 @@ import com.github.catvod.utils.ProxyVideo.proxy import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.async import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import okhttp3.Response import org.apache.commons.lang3.StringUtils import java.io.ByteArrayInputStream -import java.io.InputStream -import java.io.SequenceInputStream -import java.util.Vector +import java.io.ByteArrayOutputStream import kotlin.math.min object DownloadMT { @@ -89,10 +89,10 @@ object DownloadMT { // 存储执行结果的List val jobs = mutableListOf() - - val inputStreams = MutableList(threadNum) { ByteArrayInputStream( ByteArray(1024)) } + val channels = List(threadNum) { Channel() } for ((index, part) in partList.withIndex()) { + val newRange = "bytes=" + part[0] + "-" + part[1] SpiderDebug.log("下载开始;newRange:$newRange") @@ -103,39 +103,42 @@ object DownloadMT { jobs += CoroutineScope(Dispatchers.IO).launch { val res = downloadRange(url, headerNew) - if (res != null) {/* val buffer = ByteArray(1024) - var bytesRead: Int = 0 + if (res != null) { + val buffer = ByteArray(1024) + var bytesRead: Int = 0 - while (res.body()?.byteStream()?.read(buffer).also { - if (it != null) { - bytesRead = it - } - } != -1) { - // 处理读取的数据 - channels[index].send(buffer.copyOfRange(0, bytesRead))*/ - inputStreams.add(index, res.body()?.byteStream()!!) + while (res.body()?.byteStream()?.read(buffer).also { + if (it != null) { + bytesRead = it + } + } != -1) { + // 处理读取的数据 + channels[index].send(buffer.copyOfRange(0, bytesRead)) + + } + channels[index].close() // 发送完成后关闭通道 SpiderDebug.log("---第" + index + "块下载完成" + ";Content-Range:" + res.headers()["Content-Range"]) } } } - var contentType: String? = ""/* val outputStream = ByteArrayOutputStream(); - var pipedInputStream: ByteArrayInputStream? = null + val outputStream = ByteArrayOutputStream(); + var pipedInputStream: ByteArrayInputStream? = null + var contentType: String? = "" + val res = CoroutineScope(Dispatchers.Default).async { + repeat(jobs.size) { index -> - val res = CoroutineScope(Dispatchers.Default).async { - repeat(jobs.size) { index -> + for (bytes in channels[index]) { + // 处理读取的数据 + outputStream.write(bytes); + } - for (bytes in channels[index]) { - // 处理读取的数据 - outputStream.write(bytes); - } - - } - // 等待所有下载完成 - jobs.joinAll() - } - res.await()*/ + } + // 等待所有下载完成 + jobs.joinAll() + } + res.await() // SpiderDebug.log(" ++proxy res data:" + Json.toJson(response.body())); contentType = resHeader["Content-Type"] @@ -162,10 +165,10 @@ object DownloadMT { SpiderDebug.log("----proxy res contentType:$contentType") // SpiderDebug.log("++proxy res body:" + response.body()); SpiderDebug.log("----proxy res respHeaders:" + Json.toJson(resHeader)) - val sequenceInputStream = SequenceInputStream(Vector(inputStreams).elements()); + pipedInputStream = ByteArrayInputStream(outputStream.toByteArray()); + outputStream.close() - - return arrayOf(206, contentType, sequenceInputStream, resHeader) + return arrayOf(206, contentType, pipedInputStream, resHeader) } catch (e: Exception) { diff --git a/jar/custom_spider.jar b/jar/custom_spider.jar index 759036a8..aa893278 100644 Binary files a/jar/custom_spider.jar and b/jar/custom_spider.jar differ diff --git a/jar/custom_spider.jar.md5 b/jar/custom_spider.jar.md5 index 2eee87dd..ebc951d9 100644 --- a/jar/custom_spider.jar.md5 +++ b/jar/custom_spider.jar.md5 @@ -1 +1 @@ -0fdb7b55ab057fc9f8d455bf438ec1fd +8b6de828883275e559c19d37c8dff9d1 diff --git a/json/test.json b/json/test.json index 27974391..c153ca40 100644 --- a/json/test.json +++ b/json/test.json @@ -1,5 +1,5 @@ { - "spider": "https://ghproxy.net/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;0fdb7b55ab057fc9f8d455bf438ec1fd", + "spider": "https://ghproxy.net/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;8b6de828883275e559c19d37c8dff9d1", "lives": [ { "name": "电视直播",