All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kernl.data.source.serialize.SerializableSerializer.kt Maven / Gradle / Ivy

Go to download

Kernl: A Kotlin Symbol Processing (KSP) library for automatic repository generation.

There is a newer version: 0.0.1-beta6
Show newest version
package io.github.mattshoe.shoebox.kernl.data.source.serialize

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable

class SerializableSerializer : Serializer {
    override fun serialize(data: T): ByteArray {
        val byteArrayOutputStream = ByteArrayOutputStream()
        ObjectOutputStream(byteArrayOutputStream).use { it.writeObject(data) }
        return byteArrayOutputStream.toByteArray()
    }

    @Suppress("UNCHECKED_CAST")
    override fun deserialize(data: ByteArray): T {
        val byteArrayInputStream = ByteArrayInputStream(data)
        return ObjectInputStream(byteArrayInputStream).use { it.readObject() as T }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy