Files
fast-messenger/app/src/main/java/com/meloda/fast/util/Utils.kt
T
2021-02-21 00:31:02 +03:00

51 lines
1.3 KiB
Kotlin

package com.meloda.fast.util
import android.content.Context
import android.content.res.Configuration
import com.meloda.fast.R
import com.meloda.fast.common.AppGlobal
import com.meloda.fast.extensions.ArrayExtensions.isNullOrEmpty
import com.meloda.fast.io.BytesOutputStream
import java.io.ByteArrayInputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
object Utils {
fun getLocalizedThrowable(context: Context, t: Throwable): String {
return context.getString(R.string.error, t.message.toString())
}
fun serialize(source: Any?): ByteArray? {
try {
val bos = BytesOutputStream()
val out = ObjectOutputStream(bos)
out.writeObject(source)
out.close()
return bos.byteArray
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
fun deserialize(source: ByteArray?): Any? {
if (source.isNullOrEmpty()) {
return null
}
try {
val bis = ByteArrayInputStream(source)
val `in` = ObjectInputStream(bis)
val o = `in`.readObject()
`in`.close()
return o
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
}