diff --git a/musikr/src/main/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCodec.kt b/musikr/src/main/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCodec.kt new file mode 100644 index 000000000..6e9364ade --- /dev/null +++ b/musikr/src/main/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCodec.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2026 Auxio Project + * LibrarySnapshotCodec.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.snapshot + +import java.io.DataInputStream +import java.io.DataOutputStream +import java.io.InputStream +import java.io.OutputStream + +object LibrarySnapshotCodec { + const val MAGIC = "AUXSNAP" + private const val SCHEMA_VERSION = 1 + private val MAGIC_BYTES = MAGIC.toByteArray(Charsets.US_ASCII) + + data class Header(val magic: String, val schemaVersion: Int) + + fun writeHeader(output: OutputStream) { + val dataOutput = DataOutputStream(output) + dataOutput.write(MAGIC_BYTES) + dataOutput.writeInt(SCHEMA_VERSION) + dataOutput.flush() + } + + fun readHeader(input: InputStream): Header { + val dataInput = DataInputStream(input) + val magic = ByteArray(MAGIC_BYTES.size) + dataInput.readFully(magic) + return Header(String(magic, Charsets.US_ASCII), dataInput.readInt()) + } +} diff --git a/musikr/src/test/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCodecTest.kt b/musikr/src/test/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCodecTest.kt new file mode 100644 index 000000000..8af805a5d --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/snapshot/LibrarySnapshotCodecTest.kt @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2026 Auxio Project + * LibrarySnapshotCodecTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.snapshot + +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import org.junit.Assert.assertEquals +import org.junit.Test + +class LibrarySnapshotCodecTest { + @Test + fun header_roundTrip_preservesMagicAndSchemaVersion() { + val output = ByteArrayOutputStream() + + LibrarySnapshotCodec.writeHeader(output) + val header = LibrarySnapshotCodec.readHeader(ByteArrayInputStream(output.toByteArray())) + + assertEquals(LibrarySnapshotCodec.MAGIC, header.magic) + assertEquals(1, header.schemaVersion) + } +}