Skip to content

Commit

Permalink
Fix new new TL serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pron committed Feb 23, 2016
1 parent 269d73a commit 0057c28
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
* @author pron
*/
public class ThreadLocalSerializer extends Serializer<ThreadLocal<?>> {

public static boolean PRINT_WARNINGS_ON_UNSERIALIZABLE_THREAD_LOCAL = false;

static final class DEFAULT {}

static final class DEFAULT implements Serializable {
}

public ThreadLocalSerializer() {
setImmutable(true);
}
Expand All @@ -40,8 +42,11 @@ public void write(Kryo kryo, Output output, ThreadLocal<?> tl) {
try {
kryo.writeClassAndObject(output, val);
} catch (RuntimeException e) {
if (PRINT_WARNINGS_ON_UNSERIALIZABLE_THREAD_LOCAL)
System.err.println("WARNING: Cannot serialize ThreadLocal (" + tl + " = " + val + "), it will be restored as null.");

output.setPosition(pos);
kryo.writeObjectOrNull(output, null, DEFAULT.class);
kryo.writeObject(output, new DEFAULT());
}
}

Expand All @@ -50,9 +55,9 @@ public ThreadLocal<?> read(Kryo kryo, Input input, Class<ThreadLocal<?>> type) {
final boolean inheritable = input.readBoolean();
final ThreadLocal tl = inheritable ? new InheritableThreadLocal() : new ThreadLocal();

final Class<?> clazz = kryo.readClass(input).getType();
if (!clazz.equals(DEFAULT.class))
tl.set(kryo.readObject(input, clazz));
final Object val = kryo.readClassAndObject(input);
if (!(val instanceof DEFAULT))
tl.set(val);
return tl;
}

Expand Down

0 comments on commit 0057c28

Please sign in to comment.