diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt index f72d7f6bb..86e990580 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/fetcher/FeedFetcher.kt @@ -32,7 +32,6 @@ import io.ktor.http.HttpStatusCode import io.ktor.http.URLBuilder import io.ktor.http.URLProtocol import io.ktor.http.Url -import io.ktor.http.charset import io.ktor.http.contentType import me.tatarka.inject.annotations.Inject @@ -98,8 +97,7 @@ class FeedFetcher(private val httpClient: HttpClient, private val feedParser: Fe } } else { val content = response.bodyAsChannel() - val feedPayload = - feedParser.parse(content = content, charset = response.charset(), feedUrl = url) + val feedPayload = feedParser.parse(content = content, feedUrl = url) FeedFetchResult.Success(feedPayload) } diff --git a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt index ea78dc356..431a5a6b2 100644 --- a/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt +++ b/core/network/src/commonMain/kotlin/dev/sasikanth/rss/reader/core/network/parser/FeedParser.kt @@ -24,14 +24,13 @@ 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.charsets.Charset -import io.ktor.utils.io.charsets.Charsets -import io.ktor.utils.io.charsets.decode +import io.ktor.utils.io.core.readBytes 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 @@ -42,12 +41,10 @@ class FeedParser(private val dispatchersProvider: DispatchersProvider) { suspend fun parse( content: ByteReadChannel, feedUrl: String, - charset: Charset?, ): FeedPayload { return try { withContext(dispatchersProvider.io) { - val parser = - MiniXmlPullParser(source = content.toCharIterator(charset = charset ?: Charsets.UTF_8)) + val parser = MiniXmlPullParser(source = content.toCharIterator()) parser.nextTag() @@ -142,7 +139,6 @@ class FeedParser(private val dispatchersProvider: DispatchersProvider) { } private fun ByteReadChannel.toCharIterator( - charset: Charset, context: CoroutineContext = EmptyCoroutineContext ): CharIterator { return object : CharIterator() { @@ -157,9 +153,7 @@ private fun ByteReadChannel.toCharIterator( if (this@toCharIterator.isClosedForRead) return false val packet = runBlocking(context) { this@toCharIterator.readRemaining(DEFAULT_BUFFER_SIZE) } - val decoder = charset.newDecoder() - - currentBuffer = decoder.decode(packet).toCharArray() + currentBuffer = packet.readBytes().commonToUtf8String().toCharArray() packet.release() currentIndex = 0 return currentBuffer.isNotEmpty() diff --git a/shared/src/commonTest/kotlin/dev/sasikanth/rss/reader/FeedParserTest.kt b/shared/src/commonTest/kotlin/dev/sasikanth/rss/reader/FeedParserTest.kt index bb97ae478..a69b1509a 100644 --- a/shared/src/commonTest/kotlin/dev/sasikanth/rss/reader/FeedParserTest.kt +++ b/shared/src/commonTest/kotlin/dev/sasikanth/rss/reader/FeedParserTest.kt @@ -19,7 +19,6 @@ import dev.sasikanth.rss.reader.core.model.remote.FeedPayload import dev.sasikanth.rss.reader.core.model.remote.PostPayload import dev.sasikanth.rss.reader.core.network.parser.FeedParser import io.ktor.utils.io.ByteReadChannel -import io.ktor.utils.io.charsets.Charsets import io.ktor.utils.io.core.toByteArray import kotlin.test.Test import kotlin.test.assertEquals @@ -114,7 +113,7 @@ class FeedParserTest { // when val content = ByteReadChannel(rssXmlContent.toByteArray()) - val payload = feedParser.parse(content, feedUrl, Charsets.UTF_8) + val payload = feedParser.parse(content, feedUrl) // then assertEquals(expectedFeedPayload, payload) @@ -191,7 +190,7 @@ class FeedParserTest { // when val content = ByteReadChannel(atomXmlContent.toByteArray()) - val payload = feedParser.parse(content, feedUrl, Charsets.UTF_8) + val payload = feedParser.parse(content, feedUrl) // then assertEquals(expectedFeedPayload, payload)