Skip to content

Instantly share code, notes, and snippets.

@fayimora
Created January 30, 2018 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fayimora/119ab972cdbc045ba97db8154a7c13a5 to your computer and use it in GitHub Desktop.
Save fayimora/119ab972cdbc045ba97db8154a7c13a5 to your computer and use it in GitHub Desktop.
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}
object Serialization extends App {
def serialise(value: Any): Array[Byte] = {
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(stream)
oos.writeObject(value)
oos.close
stream.toByteArray
}
def deserialise(bytes: Array[Byte]): Any = {
val ois = new ObjectInputStream(new ByteArrayInputStream(bytes))
val value = ois.readObject
ois.close
value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment