Created
December 31, 2012 04:08
-
-
Save timperrett/4417294 to your computer and use it in GitHub Desktop.
Kryo2 scala example
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
| package demo | |
| class User(name: String, age: Int) | |
| object Example { | |
| import com.esotericsoftware.kryo.{Kryo,io}, io.{Output,Input} | |
| val kryo = new Kryo | |
| kryo.register(classOf[User]) | |
| val user = new User("Tim", 26) | |
| lazy val foo = { | |
| using(new Output(1024)){ output => | |
| kryo.writeObject(output, user) | |
| output.toBytes | |
| } | |
| } | |
| lazy val bar = { | |
| using(new Input(foo)){ input => | |
| kryo.readObject(input, classOf[User]) | |
| } | |
| } | |
| def using[A <: { def close(): Unit }, B](r: A)(f: A => B): B = | |
| try f(r) | |
| finally r.close() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment