fix: decoding and encoding.

Signed-off-by: KaiserBh <kaiserbh@proton.me>
This commit is contained in:
KaiserBh 2024-01-10 19:59:01 +11:00
parent 205d34358c
commit f02a9de4db
No known key found for this signature in database
GPG Key ID: 14D73B142042BBA9

View File

@ -137,7 +137,8 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
try { try {
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray())) val gzipInputStream = GZIPInputStream(ByteArrayInputStream(outputStream.toByteArray()))
val syncData = json.decodeFromString<SyncData>(gzipInputStream.reader(Charsets.UTF_8).readText()) val jsonString = gzipInputStream.bufferedReader(Charsets.UTF_8).use { it.readText() }
val syncData = json.decodeFromString(SyncData.serializer(), jsonString)
logcat(LogPriority.DEBUG) { "JSON deserialized successfully" } logcat(LogPriority.DEBUG) { "JSON deserialized successfully" }
syncData syncData
} catch (e: Exception) { } catch (e: Exception) {
@ -157,10 +158,10 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync
val byteArrayOutputStream = ByteArrayOutputStream() val byteArrayOutputStream = ByteArrayOutputStream()
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val jsonData = json.encodeToString(syncData)
val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream) val gzipOutputStream = GZIPOutputStream(byteArrayOutputStream)
gzipOutputStream.writer(Charsets.UTF_8).use { writer -> gzipOutputStream.write(jsonData.toByteArray(Charsets.UTF_8))
writer.write(json.encodeToString(syncData)) gzipOutputStream.close()
}
logcat(LogPriority.DEBUG) { "JSON serialized successfully" } logcat(LogPriority.DEBUG) { "JSON serialized successfully" }
} }