Created
March 21, 2018 18:18
-
-
Save BrianLondon/dc9541bbe915b4ecf80f9da43be2f9b5 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io._ | |
import com.esotericsoftware.kryo.Kryo | |
import com.esotericsoftware.kryo.io._ | |
sealed trait Gen[T] { | |
def reader: GenReader[T] | |
} | |
sealed trait GenReader[T] { | |
def toGen: T | |
} | |
case class Foo(n: Int, s: String) extends Gen[Foo] { | |
def reader: FooReader = { | |
val f = new FooReader() | |
f.n = n | |
f.s = s | |
f | |
} | |
} | |
case class Bar(x: Double) extends Gen[Bar] { | |
def reader: BarReader = { | |
val f = new BarReader() | |
f.x = x | |
f | |
} | |
} | |
class FooReader() extends GenReader[Foo] { | |
var n: Int = 0; | |
var s: String = "" | |
def toGen = Foo(n, s) | |
} | |
class BarReader() extends GenReader[Bar] { | |
var x: Double = 0.0 | |
def toGen = Bar(x) | |
} | |
val foos = Array( | |
Foo(1, "one"), | |
Foo(2, "two"), | |
Foo(3, "three"), | |
Foo(100, "One Hundred"), | |
Foo(1984, "One thousand nine hundred eighty four"), | |
Foo(6,"six") | |
) | |
val kryo = new Kryo() | |
kryo.register(classOf[FooReader]) | |
val output = new Output( new FileOutputStream("foo_list.bin")) | |
kryo.writeObject(output, foos.map(_.reader)) | |
output.close() | |
/* | |
> hexdump -C foo_list.bin | |
00000000 01 07 0c 01 02 01 6f 6e e5 0c 01 04 01 74 77 ef |......on.....tw.| | |
00000010 0c 01 06 01 74 68 72 65 e5 0c 01 c8 01 01 4f 6e |....thre......On| | |
00000020 65 20 48 75 6e 64 72 65 e4 0c 01 80 1f 01 4f 6e |e Hundre......On| | |
00000030 65 20 74 68 6f 75 73 61 6e 64 20 6e 69 6e 65 20 |e thousand nine | | |
00000040 68 75 6e 64 72 65 64 20 65 69 67 68 74 79 20 66 |hundred eighty f| | |
00000050 6f 75 f2 0c 01 0c 01 73 69 f8 |ou.....si.| | |
*/ | |
val input = new Input(new FileInputStream("foo_list.bin")) | |
val qq = kryo.readObject(input, classOf[Array[FooReader]]).map(_.toGen) | |
input.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment