Skip to content

Commit

Permalink
[1.186.*] Pre-release merge (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
tramline-github[bot] authored Jun 4, 2024
2 parents 079b606 + 434871d commit ddcfb23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ import io.ktor.http.URLBuilder
import io.ktor.http.URLProtocol
import io.ktor.http.set
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.pool.DefaultPool
import io.ktor.utils.io.pool.ObjectPool
import io.ktor.utils.io.readAvailable
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import me.tatarka.inject.annotations.Inject
import okio.internal.commonToUtf8String
import org.kobjects.ktxml.api.XmlPullParserException
import org.kobjects.ktxml.mini.MiniXmlPullParser

Expand Down Expand Up @@ -140,39 +136,22 @@ class FeedParser(private val dispatchersProvider: DispatchersProvider) {
private fun ByteReadChannel.toCharIterator(
context: CoroutineContext = EmptyCoroutineContext
): CharIterator {
val channel = this
return object : CharIterator() {

private val byteArrayPool = ByteArrayPool
private var currentIndex = 0
private var currentBuffer = ""

// Currently MiniXmlPullParser fails to parse XML if it contains
// the <?xml ?> tag in the first line. So we are removing it until
// the issue gets resolved.
// https://github.com/kobjects/ktxml/issues/5
private val xmlDeclarationPattern = Regex("<\\?xml .*\\?>")

private fun refillBuffer() {
val byteArray = byteArrayPool.borrow()
private val DEFAULT_BUFFER_SIZE = 1024L

runBlocking(context) {
val bytesRead = channel.readAvailable(byteArray)

if (bytesRead != -1) {
currentBuffer = byteArray.commonToUtf8String().replace(xmlDeclarationPattern, "")
currentIndex = 0
}
}
}
private var currentIndex = 0
private var currentBuffer = CharArray(0)

override fun hasNext(): Boolean {
return if (currentIndex == currentBuffer.length) {
refillBuffer()
currentIndex < currentBuffer.length
} else {
true
}
if (currentIndex < currentBuffer.size) return true
if (this@toCharIterator.isClosedForRead) return false

val packet = runBlocking(context) { this@toCharIterator.readRemaining(DEFAULT_BUFFER_SIZE) }
currentBuffer = packet.readText().toCharArray()
packet.release()
currentIndex = 0
return currentBuffer.isNotEmpty()
}

override fun nextChar(): Char {
Expand All @@ -182,9 +161,4 @@ private fun ByteReadChannel.toCharIterator(
}
}

val ByteArrayPool: ObjectPool<ByteArray> =
object : DefaultPool<ByteArray>(128) {
override fun produceInstance() = ByteArray(1024)
}

internal class HtmlContentException : Exception()
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
kotlin = "1.9.24"
kotlin = "1.9.23"
android_gradle_plugin = "8.4.1"
compose = "1.6.11"
compose = "1.6.2"

android_sdk_compile = "34"
android_sdk_target = "34"
Expand Down Expand Up @@ -29,7 +29,7 @@ coil = "3.0.0-alpha04"
spotless = "6.25.0"
ktfmt = "0.44"
kotlininject = "0.6.3"
ksp = "1.9.24-1.0.20"
ksp = "1.9.23-1.0.20"
material_color_utilities = "1.0.0-alpha01"
ksoup = "0.1.2"
sqliteAndroid = "3.45.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package dev.sasikanth.rss.reader

const val feedUrl = "https://example.com"
const val rssXmlContent =
"""
<?xml version="1.0" encoding="UTF-8"?>
"""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Feed title</title>
Expand Down Expand Up @@ -79,8 +78,7 @@ const val rssXmlContent =
"""

const val atomXmlContent =
"""
<?xml version="1.0" encoding="UTF-8"?>
"""<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Feed title</title>
<subtitle>Feed description</subtitle>
Expand Down

0 comments on commit ddcfb23

Please sign in to comment.