Skip to content

Commit

Permalink
Override ObjectInputStream ClassLoader
Browse files Browse the repository at this point in the history
Due to a known Java issue ObjectInputStream might use a different ClassLoader
This can cause ClassNotFoundException
  • Loading branch information
aviemzur committed Dec 18, 2016
1 parent f3700c4 commit 19a6b5e
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/com/esotericsoftware/kryo/serializers/JavaSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

package com.esotericsoftware.kryo.serializers;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
Expand Down Expand Up @@ -57,12 +60,37 @@ public Object read (Kryo kryo, Input input, Class type) {
ObjectMap graphContext = kryo.getGraphContext();
ObjectInputStream objectStream = (ObjectInputStream)graphContext.get(this);
if (objectStream == null) {
objectStream = new ObjectInputStream(input);
objectStream = new ObjectInputStreamWithKryoClassLoader(input, kryo);
graphContext.put(this, objectStream);
}
return objectStream.readObject();
} catch (Exception ex) {
throw new KryoException("Error during Java deserialization.", ex);
}
}

/**
* ${@link ObjectInputStream} uses the last user-defined ${@link ClassLoader} which may not be the correct one.
* This is a known Java issue and is often solved by using a specific class loader.
* See:
* https://github.com/apache/spark/blob/v1.6.3/streaming/src/main/scala/org/apache/spark/streaming/Checkpoint.scala#L154
* https://issues.apache.org/jira/browse/GROOVY-1627
*/
private static class ObjectInputStreamWithKryoClassLoader extends ObjectInputStream {
private final ClassLoader loader;

ObjectInputStreamWithKryoClassLoader(InputStream in, Kryo kryo) throws IOException {
super(in);
this.loader = kryo.getClassLoader();
}

@Override
protected Class<?> resolveClass(ObjectStreamClass desc) {
try {
return Class.forName(desc.getName(), false, loader);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class not found: " + desc.getName(), e);
}
}
}
}

0 comments on commit 19a6b5e

Please sign in to comment.