Add domain library snapshot codecs
This commit is contained in:
@@ -25,6 +25,12 @@ import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.CodingErrorAction
|
||||
import java.util.UUID
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.tag.Date
|
||||
import org.oxycblt.musikr.tag.Disc
|
||||
import org.oxycblt.musikr.tag.ReleaseType
|
||||
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||
|
||||
object LibrarySnapshotCodec {
|
||||
const val MAGIC = "AUXSNAP"
|
||||
@@ -127,7 +133,14 @@ object LibrarySnapshotCodec {
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readBoolean(input: InputStream) = DataInputStream(input).readBoolean()
|
||||
fun readBoolean(input: InputStream): Boolean {
|
||||
val marker = DataInputStream(input).readUnsignedByte()
|
||||
return when (marker) {
|
||||
0 -> false
|
||||
1 -> true
|
||||
else -> throw IOException("Invalid snapshot boolean marker: $marker")
|
||||
}
|
||||
}
|
||||
|
||||
fun writeNullableFloat(output: OutputStream, value: Float?) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
@@ -161,4 +174,113 @@ object LibrarySnapshotCodec {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
fun writeUid(output: OutputStream, value: Music.UID) {
|
||||
writeString(output, value.toString())
|
||||
}
|
||||
|
||||
fun readUid(input: InputStream): Music.UID {
|
||||
val encoded = readString(input)
|
||||
return Music.UID.fromString(encoded) ?: throw IOException("Invalid snapshot UID")
|
||||
}
|
||||
|
||||
fun writeDate(output: OutputStream, value: Date) {
|
||||
writeString(output, value.toString())
|
||||
}
|
||||
|
||||
fun readDate(input: InputStream): Date {
|
||||
val encoded = readString(input)
|
||||
return Date.from(encoded) ?: throw IOException("Invalid snapshot date")
|
||||
}
|
||||
|
||||
fun writeDisc(output: OutputStream, value: Disc) {
|
||||
writeInt(output, value.number)
|
||||
writeNullableString(output, value.name)
|
||||
}
|
||||
|
||||
fun readDisc(input: InputStream): Disc {
|
||||
return Disc(readInt(input), readNullableString(input))
|
||||
}
|
||||
|
||||
fun writeReleaseType(output: OutputStream, value: ReleaseType) {
|
||||
when (value) {
|
||||
is ReleaseType.Album -> {
|
||||
writeInt(output, 0)
|
||||
writeNullableString(output, value.refinement?.name)
|
||||
}
|
||||
is ReleaseType.EP -> {
|
||||
writeInt(output, 1)
|
||||
writeNullableString(output, value.refinement?.name)
|
||||
}
|
||||
is ReleaseType.Single -> {
|
||||
writeInt(output, 2)
|
||||
writeNullableString(output, value.refinement?.name)
|
||||
}
|
||||
is ReleaseType.Compilation -> {
|
||||
writeInt(output, 3)
|
||||
writeNullableString(output, value.refinement?.name)
|
||||
}
|
||||
ReleaseType.Soundtrack -> writeInt(output, 4)
|
||||
ReleaseType.Mix -> writeInt(output, 5)
|
||||
ReleaseType.Mixtape -> writeInt(output, 6)
|
||||
ReleaseType.Demo -> writeInt(output, 7)
|
||||
}
|
||||
}
|
||||
|
||||
fun readReleaseType(input: InputStream): ReleaseType {
|
||||
return when (readInt(input)) {
|
||||
0 -> ReleaseType.Album(readRefinement(input))
|
||||
1 -> ReleaseType.EP(readRefinement(input))
|
||||
2 -> ReleaseType.Single(readRefinement(input))
|
||||
3 -> ReleaseType.Compilation(readRefinement(input))
|
||||
4 -> ReleaseType.Soundtrack
|
||||
5 -> ReleaseType.Mix
|
||||
6 -> ReleaseType.Mixtape
|
||||
7 -> ReleaseType.Demo
|
||||
else -> throw IOException("Invalid snapshot release type")
|
||||
}
|
||||
}
|
||||
|
||||
private fun readRefinement(input: InputStream): ReleaseType.Refinement? {
|
||||
return when (readNullableString(input)) {
|
||||
null -> null
|
||||
"LIVE" -> ReleaseType.Refinement.LIVE
|
||||
"REMIX" -> ReleaseType.Refinement.REMIX
|
||||
else -> throw IOException("Invalid snapshot release refinement")
|
||||
}
|
||||
}
|
||||
|
||||
fun writeReplayGainAdjustment(output: OutputStream, value: ReplayGainAdjustment) {
|
||||
writeNullableFloat(output, value.track)
|
||||
writeNullableFloat(output, value.album)
|
||||
}
|
||||
|
||||
fun readReplayGainAdjustment(input: InputStream): ReplayGainAdjustment {
|
||||
val track = readNullableFloat(input)
|
||||
val album = readNullableFloat(input)
|
||||
return ReplayGainAdjustment(track, album)
|
||||
}
|
||||
|
||||
fun writeMusicBrainzId(output: OutputStream, value: UUID?) {
|
||||
val dataOutput = DataOutputStream(output)
|
||||
dataOutput.writeByte(if (value == null) 0 else 1)
|
||||
if (value != null) {
|
||||
writeLong(dataOutput, value.mostSignificantBits)
|
||||
writeLong(dataOutput, value.leastSignificantBits)
|
||||
}
|
||||
dataOutput.flush()
|
||||
}
|
||||
|
||||
fun readMusicBrainzId(input: InputStream): UUID? {
|
||||
val marker = DataInputStream(input).readUnsignedByte()
|
||||
return when (marker) {
|
||||
0 -> null
|
||||
1 -> {
|
||||
val mostSignificantBits = readLong(input)
|
||||
val leastSignificantBits = readLong(input)
|
||||
UUID(mostSignificantBits, leastSignificantBits)
|
||||
}
|
||||
else -> throw IOException("Invalid snapshot MusicBrainz ID marker: $marker")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,16 @@ import java.io.ByteArrayOutputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.EOFException
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
import org.oxycblt.musikr.Music
|
||||
import org.oxycblt.musikr.tag.Date
|
||||
import org.oxycblt.musikr.tag.Disc
|
||||
import org.oxycblt.musikr.tag.ReleaseType
|
||||
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||
|
||||
class LibrarySnapshotCodecTest {
|
||||
@Test
|
||||
@@ -249,4 +255,104 @@ class LibrarySnapshotCodecTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun readBoolean_invalidMarker_throwsIOException() {
|
||||
assertThrows(IOException::class.java) {
|
||||
LibrarySnapshotCodec.readBoolean(ByteArrayInputStream(byteArrayOf(2)))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun uid_roundTrip_preservesCanonicalRepresentation() {
|
||||
val uid =
|
||||
requireNotNull(
|
||||
Music.UID.fromString("uas00112233-4455-6677-8899-aabbccddeeff")
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeUid(output, uid)
|
||||
val decoded = LibrarySnapshotCodec.readUid(ByteArrayInputStream(output.toByteArray()))
|
||||
|
||||
assertEquals(uid, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun date_roundTrip_preservesFullPrecision() {
|
||||
val date = requireNotNull(Date.from("2016-08-16T00:01:02Z"))
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeDate(output, date)
|
||||
val decoded = LibrarySnapshotCodec.readDate(ByteArrayInputStream(output.toByteArray()))
|
||||
|
||||
assertEquals(date, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun disc_roundTrip_preservesNumberAndSubtitle() {
|
||||
val disc = Disc(2, "Bonus 界")
|
||||
val output = ByteArrayOutputStream()
|
||||
|
||||
LibrarySnapshotCodec.writeDisc(output, disc)
|
||||
val decoded = LibrarySnapshotCodec.readDisc(ByteArrayInputStream(output.toByteArray()))
|
||||
|
||||
assertEquals(disc.number, decoded.number)
|
||||
assertEquals(disc.name, decoded.name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun releaseType_roundTrip_preservesTypeAndRefinement() {
|
||||
val values =
|
||||
listOf<ReleaseType>(
|
||||
ReleaseType.Album(null),
|
||||
ReleaseType.Album(ReleaseType.Refinement.LIVE),
|
||||
ReleaseType.EP(ReleaseType.Refinement.REMIX),
|
||||
ReleaseType.Single(null),
|
||||
ReleaseType.Compilation(ReleaseType.Refinement.LIVE),
|
||||
ReleaseType.Soundtrack,
|
||||
ReleaseType.Mix,
|
||||
ReleaseType.Mixtape,
|
||||
ReleaseType.Demo,
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
values.forEach { LibrarySnapshotCodec.writeReleaseType(output, it) }
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
val decoded = values.map { LibrarySnapshotCodec.readReleaseType(input) }
|
||||
|
||||
assertEquals(values, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun replayGain_roundTrip_preservesIndependentTrackAndAlbumValues() {
|
||||
val values =
|
||||
listOf(
|
||||
ReplayGainAdjustment(track = -6.5f, album = null),
|
||||
ReplayGainAdjustment(track = null, album = -9.0f),
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
values.forEach { LibrarySnapshotCodec.writeReplayGainAdjustment(output, it) }
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
val decoded = values.map { LibrarySnapshotCodec.readReplayGainAdjustment(input) }
|
||||
|
||||
assertEquals(values, decoded)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun musicBrainzId_roundTrip_preservesNullableUuids() {
|
||||
val values =
|
||||
listOf<UUID?>(
|
||||
null,
|
||||
UUID.fromString("00112233-4455-6677-8899-aabbccddeeff"),
|
||||
UUID.fromString("ffeeddcc-bbaa-9988-7766-554433221100"),
|
||||
)
|
||||
val output = ByteArrayOutputStream()
|
||||
values.forEach { LibrarySnapshotCodec.writeMusicBrainzId(output, it) }
|
||||
val input = ByteArrayInputStream(output.toByteArray())
|
||||
|
||||
val decoded = values.map { LibrarySnapshotCodec.readMusicBrainzId(input) }
|
||||
|
||||
assertEquals(values, decoded)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user