kt线程和分片优化

This commit is contained in:
lushunming 2025-07-14 08:21:18 +08:00
parent da70913280
commit da0834e46e
4 changed files with 33 additions and 37 deletions

View File

@ -9,15 +9,14 @@ import com.github.catvod.utils.ProxyVideo.proxy
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.Response import okhttp3.Response
import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils
import java.io.ByteArrayInputStream import java.io.InputStream
import java.io.ByteArrayOutputStream import java.io.SequenceInputStream
import java.util.Vector
import kotlin.math.min import kotlin.math.min
object DownloadMT { object DownloadMT {
@ -90,9 +89,9 @@ object DownloadMT {
// 存储执行结果的List // 存储执行结果的List
val jobs = mutableListOf<Job>() val jobs = mutableListOf<Job>()
val channels = List(threadNum) { Channel<ByteArray>() } val channels = List(threadNum) { Channel<ByteArray>() }
var inputStreams = mutableListOf<InputStream>()
for ((index, part) in partList.withIndex()) { for ((index, part) in partList.withIndex()) {
val newRange = "bytes=" + part[0] + "-" + part[1] val newRange = "bytes=" + part[0] + "-" + part[1]
SpiderDebug.log("下载开始;newRange:$newRange") SpiderDebug.log("下载开始;newRange:$newRange")
@ -103,42 +102,39 @@ object DownloadMT {
jobs += CoroutineScope(Dispatchers.IO).launch { jobs += CoroutineScope(Dispatchers.IO).launch {
val res = downloadRange(url, headerNew) val res = downloadRange(url, headerNew)
if (res != null) { if (res != null) {/* val buffer = ByteArray(1024)
val buffer = ByteArray(1024) var bytesRead: Int = 0
var bytesRead: Int = 0
while (res.body()?.byteStream()?.read(buffer).also { while (res.body()?.byteStream()?.read(buffer).also {
if (it != null) { if (it != null) {
bytesRead = it bytesRead = it
} }
} != -1) { } != -1) {
// 处理读取的数据 // 处理读取的数据
channels[index].send(buffer.copyOfRange(0, bytesRead)) channels[index].send(buffer.copyOfRange(0, bytesRead))*/
inputStreams.add(index, res.body()?.byteStream()!!)
}
channels[index].close() // 发送完成后关闭通道
SpiderDebug.log("---第" + index + "块下载完成" + ";Content-Range:" + res.headers()["Content-Range"]) SpiderDebug.log("---第" + index + "块下载完成" + ";Content-Range:" + res.headers()["Content-Range"])
} }
} }
} }
val outputStream = ByteArrayOutputStream(); var contentType: String? = ""/* val outputStream = ByteArrayOutputStream();
var pipedInputStream: ByteArrayInputStream? = null var pipedInputStream: ByteArrayInputStream? = null
var contentType: String? = ""
val res = CoroutineScope(Dispatchers.Default).async {
repeat(jobs.size) { index ->
for (bytes in channels[index]) { val res = CoroutineScope(Dispatchers.Default).async {
// 处理读取的数据 repeat(jobs.size) { index ->
outputStream.write(bytes);
}
} for (bytes in channels[index]) {
// 等待所有下载完成 // 处理读取的数据
jobs.joinAll() outputStream.write(bytes);
} }
res.await()
}
// 等待所有下载完成
jobs.joinAll()
}
res.await()*/
// SpiderDebug.log(" ++proxy res data:" + Json.toJson(response.body())); // SpiderDebug.log(" ++proxy res data:" + Json.toJson(response.body()));
contentType = resHeader["Content-Type"] contentType = resHeader["Content-Type"]
@ -165,10 +161,10 @@ object DownloadMT {
SpiderDebug.log("----proxy res contentType:$contentType") SpiderDebug.log("----proxy res contentType:$contentType")
// SpiderDebug.log("++proxy res body:" + response.body()); // SpiderDebug.log("++proxy res body:" + response.body());
SpiderDebug.log("----proxy res respHeaders:" + Json.toJson(resHeader)) SpiderDebug.log("----proxy res respHeaders:" + Json.toJson(resHeader))
pipedInputStream = ByteArrayInputStream(outputStream.toByteArray()); val sequenceInputStream = SequenceInputStream(Vector(inputStreams).elements());
outputStream.close()
return arrayOf(206, contentType, pipedInputStream, resHeader)
return arrayOf(206, contentType, sequenceInputStream, resHeader)
} catch (e: Exception) { } catch (e: Exception) {

Binary file not shown.

View File

@ -1 +1 @@
693338eb7794c8953b3fd0a8bed3a855 5cb212c16ee71528fd75f507a5aa91d4

View File

@ -1,5 +1,5 @@
{ {
"spider": "https://ghproxy.net/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;693338eb7794c8953b3fd0a8bed3a855", "spider": "https://ghproxy.net/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;5cb212c16ee71528fd75f507a5aa91d4",
"lives": [ "lives": [
{ {
"name": "电视直播", "name": "电视直播",