Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -489,7 +489,9 @@ class MigrationExporter

ZipOutputStream(FileOutputStream(exportFile)).use { zipOut ->
zipOut.putNextEntry(ZipEntry(MANIFEST_FILENAME))
zipOut.write(json.encodeToString(bundle).toByteArray())
// Stream the manifest into the zip entry so the full JSON
// text is never buffered in memory (COLUMBA-DS).
json.encodeToStream(MigrationBundle.serializer(), bundle, zipOut)
zipOut.closeEntry()
onProgress(0.8f)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package network.columba.app.migration

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import java.io.ByteArrayOutputStream
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
Expand Down Expand Up @@ -253,6 +255,36 @@ class MigrationDataTest {
assertEquals(bundle.settings, decoded.settings)
}

@Test
fun `encodeToStream produces the same manifest bytes as encodeToString`() {
val bundle =
MigrationBundle(
identities = listOf(createTestIdentity()),
conversations = listOf(createTestConversation()),
messages = listOf(createTestMessage()),
contacts = listOf(createTestContact()),
announces = listOf(createTestAnnounce()),
interfaces = listOf(createTestInterface()),
customThemes = listOf(createTestCustomTheme()),
settings = createTestSettings(),
attachmentManifest =
listOf(
AttachmentRef(
messageId = "msg_001",
fieldKey = "attachment",
relativePath = "attachments/msg_001_attachment.png",
sizeBytes = 12345L,
),
),
ratchetFiles = listOf(createTestRatchetRef(type = "own", filename = "a1b2c3d4e5f6")),
)

val streamed = ByteArrayOutputStream()
json.encodeToStream(MigrationBundle.serializer(), bundle, streamed)

assertEquals(json.encodeToString(bundle), String(streamed.toByteArray(), Charsets.UTF_8))
Comment thread
torlando-tech marked this conversation as resolved.
Outdated
}

@Test
fun `MigrationBundle with empty optional lists serializes correctly`() {
val bundle =
Expand Down
Loading