Refine polling recycle

This commit is contained in:
tarsin committed 2023-06-18 16:33:54 +08:00
1 parent b695b06507
commit 47b97243c3
2 files changed
+19 -15

No files matched your search

@@ -57,22 +57,21 @@ class CronetRequest {
override fun onSucceeded(p0: UrlRequest, p1: UrlResponseInfo) {
val length = p1.receivedByteCount
// TODO: validate body length
pool.recycle(buffer)
readerCont.resume(Unit)
daemonCont.resume(true)
}
override fun onFailed(p0: UrlRequest, p1: UrlResponseInfo?, p2: HttpException) {
pool.recycle(buffer)
daemonCont.resumeWithException(p2)
}
override fun onCanceled(p0: UrlRequest, p1: UrlResponseInfo?) {
pool.recycle(buffer)
}
}
val buffer = pool.borrow()
fun dispose() = pool.recycle(buffer)
}
inline fun cronetRequest(url: String, conf: UrlRequest.Builder.() -> Unit) = CronetRequest().apply {
@@ -133,22 +133,27 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
notifyProgress: (Long, Long, Int) -> Unit,
): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && SdkExtensions.getExtensionVersion(Build.VERSION_CODES.S) >= 7) {
cronetRequest(url) {
val req = cronetRequest(url) {
referer?.let { addHeader("Referer", it) }
} execute { info ->
val headers = info.headers.asMap
val type = headers["Content-Type"]?.first()?.toMediaType()?.subtype ?: "jpg"
val length = headers["Content-Length"]!!.first().toLong()
saveResponseMeta(index, type, length) { file ->
file.openOutputStream().use {
val chan = it.channel
consumeBodyFully { info, buffer ->
val read = chan.write(buffer)
notifyProgress(length, info.receivedByteCount, read)
}
try {
req execute { info ->
val headers = info.headers.asMap
val type = headers["Content-Type"]?.first()?.toMediaType()?.subtype ?: "jpg"
val length = headers["Content-Length"]!!.first().toLong()
saveResponseMeta(index, type, length) { file ->
file.openOutputStream().use {
val chan = it.channel
consumeBodyFully { info, buffer ->
val read = chan.write(buffer)
notifyProgress(length, info.receivedByteCount, read)
}
}
file.length()
}
file.length()
}
} finally {
req.dispose()
}
} else {
ehRequest(url, referer).executeNonCache {