Created
November 22, 2019 20:11
-
-
Save BurakDizlek/e574b1545dd69a4e6e8cd274e1152bb9 to your computer and use it in GitHub Desktop.
Kotlin class deep copy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class User(var name:String,var list:List<User>) : Serializable | |
fun <T:Serializable?>T.deepCopy(): T? { | |
if (this == null) return null | |
val baos = ByteArrayOutputStream() | |
val oos = ObjectOutputStream(baos) | |
oos.writeObject(this) | |
oos.close() | |
val bais = ByteArrayInputStream(baos.toByteArray()) | |
val ois = ObjectInputStream(bais) | |
@Suppress("unchecked_cast") | |
return ois.readObject() as T | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment