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

ru.inforion.lab403.common.extensions.serializable.kt Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package ru.inforion.lab403.common.extensions

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

/**
 * Created by Alexei Gladkikh on 10/09/16.
 */
fun Serializable.serialize(): ByteArray {
    val baos = ByteArrayOutputStream()
    val oos = ObjectOutputStream(baos)
    oos.writeUnshared(this)
    return baos.toByteArray()
}

fun deserialize(data: ByteArray): Serializable {
    return try {
        val ois = ObjectInputStream(data.inputStream())
        ois.readUnshared() as Serializable
    } catch (e: Exception) {
        println(data.hexlify())
        throw e
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy