perf: optimize stream I/O buffering, regex compilation, and worker resource cleanup

pull/1365/head
Rayzien 3 days ago
parent 35e6985f77
commit d40ac6f229

@ -1,10 +1,10 @@
package com.deniscerri.ytdl.core.stream
import android.util.Log
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.Reader
import java.nio.charset.StandardCharsets
import java.util.regex.Pattern
@ -25,10 +25,11 @@ internal class StreamProcessExtractor(
override fun run() {
try {
val reader = BufferedReader(InputStreamReader(stream, StandardCharsets.UTF_8), 8192)
val currentLine = StringBuilder()
var nextChar: Int
while (stream.read().also { nextChar = it } != -1) {
while (reader.read().also { nextChar = it } != -1) {
val c = nextChar.toChar()
if (c == '\r' || c == '\n') {

@ -640,31 +640,35 @@ object Extensions {
downloadStartTime = timeInMillis
}
private val YOUTUBE_URL_PATTERN = Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?)|(^(https?)://(www.)?piped.video)")
private val YOUTUBE_CHANNEL_URL_PATTERN = Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/@[a-zA-Z]+")
private val YOUTUBE_WATCH_VIDEOS_URL_PATTERN = Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/watch_videos\\?video_ids=.*")
private val SOUNDCLOUD_URL_PATTERN = Pattern.compile("""^(https?://)?(www\.|m\.)?soundcloud\.com/[\w\-.]+(/[\w\-.]+)*/?$""")
private val GENERAL_URL_PATTERN = Pattern.compile("(http|ftp|https)://([\\w_-]+(?:\\.[\\w_-]+)+)([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])")
fun TextWithSubtitle(title: String, subtitle: String) : Spanned {
return HtmlCompat.fromHtml("<b><big>" + title + "</big></b>" + "<br />" +
"<small>" + subtitle + "</small>" + "<br />", HtmlCompat.FROM_HTML_MODE_LEGACY)
}
fun String.isYoutubeURL() : Boolean {
return Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?)|(^(https?)://(www.)?piped.video)").matcher(this).find()
return YOUTUBE_URL_PATTERN.matcher(this).find()
}
fun String.isYoutubeChannelURL() : Boolean {
return Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/@[a-zA-Z]+").matcher(this).find()
return YOUTUBE_CHANNEL_URL_PATTERN.matcher(this).find()
}
fun String.isYoutubeWatchVideosURL() : Boolean {
return Pattern.compile("((^(https?)://)?(www.)?(m.)?youtu(.be)?(be.com))/watch_videos\\?video_ids=.*").matcher(this).find()
return YOUTUBE_WATCH_VIDEOS_URL_PATTERN.matcher(this).find()
}
fun String.isSoundCloudURL() : Boolean {
return Pattern.compile("""^(https?://)?(www\.|m\.)?soundcloud\.com/[\w\-.]+(/[\w\-.]+)*/?$""").matcher(this).find()
return SOUNDCLOUD_URL_PATTERN.matcher(this).find()
}
fun String.extractURL() : String {
val res =
Pattern.compile("(http|ftp|https)://([\\w_-]+(?:\\.[\\w_-]+)+)([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])")
.matcher(this)
val res = GENERAL_URL_PATTERN.matcher(this)
return if (res.find()) {
res.group()
} else {
@ -673,7 +677,7 @@ object Extensions {
}
fun String.isURL(): Boolean {
return Pattern.compile("(http|ftp|https)://([\\w_-]+(?:\\.[\\w_-]+)+)([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])").matcher(this).find()
return GENERAL_URL_PATTERN.matcher(this).find()
}
fun <T1, T2, T3, T4, T5, T6, T7, R> combine(

@ -39,13 +39,12 @@ abstract class YTDLPCoroutineWorker(
runWork()
} catch (e: Exception) {
Result.failure()
} finally {
if (requiresServer) {
// Decrement job count & auto-stop service when 0 active jobs remain
BgUtilsPoTokenGeneratorUtil.releaseServer(context)
}
}
// finally {
// if (requiresServer) {
// // Decrement job count & auto-stop service when 0 active jobs remain
// BgUtilsPoTokenGeneratorUtil.releaseServer(context)
// }
// }
}
private suspend fun isBgUtilsServerAlive(): Boolean = withContext(Dispatchers.IO) {

Loading…
Cancel
Save