Skip to content

Instantly share code, notes, and snippets.

@BurakDizlek
Created November 22, 2019 20:11
Show Gist options
  • Save BurakDizlek/e574b1545dd69a4e6e8cd274e1152bb9 to your computer and use it in GitHub Desktop.
Save BurakDizlek/e574b1545dd69a4e6e8cd274e1152bb9 to your computer and use it in GitHub Desktop.
Kotlin class deep copy.
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