Create convertStreamToString.

Change-Id: I07dbb58813bc891f407773fddab7f1487f1ed24f
pull/1837/head
Jaehwa Noh 1 year ago
parent a3ad43a40e
commit 6af76eeea5

@ -30,7 +30,11 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import okio.use
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.nio.charset.StandardCharsets
import javax.inject.Inject
import kotlin.coroutines.coroutineContext
/**
* [NiaNetworkDataSource] implementation that provides static news resources to aid development
@ -67,6 +71,21 @@ class DemoNiaNetworkDataSource @Inject constructor(
override suspend fun getNewsResourceChangeList(after: Int?): List<NetworkChangeList> =
getNewsResources().mapToChangeList(NetworkNewsResource::id)
private suspend fun convertStreamToString(inputStream: InputStream): String = withContext(
coroutineContext,
) {
val result = ByteArrayOutputStream()
val buffer = ByteArray(1024)
var length = 0
while (true) {
length = inputStream.read(buffer)
if (length == -1) break
result.write(buffer, 0, length)
}
result.toString(StandardCharsets.UTF_8.name())
}
companion object {
private const val NEWS_ASSET = "news.json"
private const val TOPICS_ASSET = "topics.json"

Loading…
Cancel
Save