Skip to content

Instantly share code, notes, and snippets.

@JoshRosen
Last active August 29, 2015 14:22
Show Gist options
  • Save JoshRosen/14ba69ef53af53ef2839 to your computer and use it in GitHub Desktop.
Save JoshRosen/14ba69ef53af53ef2839 to your computer and use it in GitHub Desktop.
scalaVersion := "2.10.4"
libraryDependencies += "com.esotericsoftware.kryo" % "kryo" % sys.env.getOrElse("KRYO_VERSION", "2.21")
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.serializers.JavaSerializer
import com.esotericsoftware.kryo.io.{Input, Output}
class JavaSerializable extends Serializable
object KryoTest {
def main(args: Array[String]): Unit = {
val kryo = new Kryo()
kryo.register(classOf[JavaSerializable], new JavaSerializer())
var output = new Output(100, 100)
val objectToWrite = new JavaSerializable
kryo.writeClassAndObject(output, objectToWrite)
val firstWriteBytes = output.toBytes
println("First size is " + firstWriteBytes.length)
kryo.readClassAndObject(new Input(firstWriteBytes))
println("Successfully deserialized first copy")
//output = new Output(100, 100)
output.clear()
kryo.reset()
kryo.writeClassAndObject(output, objectToWrite)
val secondWriteBytes = output.toBytes
println("Second size is " + secondWriteBytes.length)
kryo.readClassAndObject(new Input(secondWriteBytes))
println("Successfully deserialized second copy")
}
}
@JoshRosen
Copy link
Author

When I run this in sbt:

> run
[info] Compiling 1 Scala source to /Users/joshrosen/Documents/kryo-output-bug/target/scala-2.10/classes...
[info] Running KryoTest
First size is 39
Successfully deserialized first copy
Second size is 36
[error] (run-main-5) com.esotericsoftware.kryo.KryoException: Error during Java deserialization.
com.esotericsoftware.kryo.KryoException: Error during Java deserialization.
    at com.esotericsoftware.kryo.serializers.JavaSerializer.read(JavaSerializer.java:42)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:729)
    at KryoTest$.main(KryoTest.scala:29)
    at KryoTest.main(KryoTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
Caused by: java.io.StreamCorruptedException: invalid stream header: 79737200
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at com.esotericsoftware.kryo.serializers.JavaSerializer.read(JavaSerializer.java:40)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:729)
    at KryoTest$.main(KryoTest.scala:29)
    at KryoTest.main(KryoTest.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

The second deserialization crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment