Skip to content

Instantly share code, notes, and snippets.

@eleddie
Created June 26, 2017 05:37
Show Gist options
  • Save eleddie/1dc5bad20f583f87360391e90b60e71f to your computer and use it in GitHub Desktop.
Save eleddie/1dc5bad20f583f87360391e90b60e71f to your computer and use it in GitHub Desktop.
ObjectSerializer written in Kotlin
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import kotlin.experimental.and
object ObjectSerializer {
@Throws(IOException::class)
fun serialize(obj: Serializable): String {
try {
val serialObj = ByteArrayOutputStream()
val objStream = ObjectOutputStream(serialObj)
objStream.writeObject(obj)
objStream.close()
return encodeBytes(serialObj.toByteArray())
} catch (e: Exception) {
throw RuntimeException(e)
}
}
@Throws(IOException::class)
fun deserialize(str: String): Any? {
if (str.isEmpty()) return null
try {
val serialObj = ByteArrayInputStream(decodeBytes(str))
val objStream = ObjectInputStream(serialObj)
return objStream.readObject()
} catch (e: Exception) {
throw RuntimeException(e)
}
}
private fun encodeBytes(bytes: ByteArray): String {
val strBuf = StringBuffer()
for (i in bytes.indices) {
strBuf.append(((bytes[i].toInt() shr 4 and 0xF) + 'a'.toInt()).toChar())
strBuf.append(((bytes[i] and 0xF) + 'a'.toInt()).toChar())
}
return strBuf.toString()
}
private fun decodeBytes(str: String): ByteArray {
val bytes = ByteArray(str.length / 2)
for (i in 0..str.length-1 step 2) {
var c = str[i]
bytes[i / 2] = (c - 'a' shl 4).toByte()
c = str[i + 1]
bytes[i / 2] = (bytes[i / 2] + (c - 'a').toByte()).toByte()
}
return bytes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment