Add library snapshot header codec

This commit is contained in:
jay
2026-11-13 20:50:47 -05:00
parent cc88b1e7eb
commit 8d29fd9038
2 changed files with 83 additions and 0 deletions
@@ -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 <https://www.gnu.org/licenses/>.
*/
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())
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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)
}
}