使用Kryo序列化任意Java对象(获取IllegalAccessError)

时间:2015-01-01 15:15:44

标签: java serialization deserialization kryo

动机:

为了帮助进行远程调试(Java),能够请求远程服务器通过任意对象发送到本地计算机进行检查非常有用。但是,这意味着远程服务器必须能够序列化在运行时事先不知道的任意java对象。

所以我四处询问并偶然发现Kryo serialization library。从Kryo's documentation开始,一个主要特征是它在序列化任意java对象时非常强大。对象不必实现Serializable,也不需要无反序列化的构造函数,我甚至不需要在序列化之前了解对象的结构。完美!

问题:

因此,为了测试Kryo,我试图查看是否可以序列化然后反序列化PrintWriter对象(即任意对象):

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.*;

public class SerializationTest {

    private static final String ioFileName = "someIO.bin";

    public static void main(String[] args) {

        // Create a PrintWriter object that I will later attempt to serialize
        PrintWriter outObj = null;
        try {
            outObj = new PrintWriter("textfile.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // Change the PrintWriter's state as a test for later to see if state is restored after serialization and deserialization
        outObj.println("Hello");   // "Hello" held in PrintWriter's buffer

        Kryo kryo = new Kryo();    // Initialize Kryo serialization
        writeObj(kryo, outObj);    // Save PrintWriter object to file with "Hello" still in its buffer

        // Read the previously saved Printwriter object (still with "Hello" in its buffer)
        PrintWriter inObj = (PrintWriter) readObj(kryo);

        inObj.close();    // commit "Hello" to disk (using deserialized object)
        outObj.close();   // commit "Hello" to disk (using original object)

        System.out.println(inObj);
    }

    public static Object readObj(Kryo kryo) {
        Object obj = null;
        try {
            Input input = new Input(new FileInputStream(ioFileName));
            obj = kryo.readClassAndObject(input);   // ERROR HERE!!
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static void writeObj(Kryo kryo, Object obj) {
        try {
            Output output = new Output(new FileOutputStream(ioFileName));
            kryo.writeClassAndObject(output, obj);
            output.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

序列化工作正常,但在反序列化后,对第39行的kryo.readClassAndObject(input)的调用给出了以下IllegalAccessError

Exception in thread "main" java.lang.IllegalAccessError: tried to access class sun.nio.cs.UTF_8 from class sun.nio.cs.UTF_8ConstructorAccess
    at sun.nio.cs.UTF_8ConstructorAccess.newInstance(Unknown Source)
    at com.esotericsoftware.kryo.Kryo$DefaultInstantiatorStrategy$1.newInstance(Kryo.java:1234)
    at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1086)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:547)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:523)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:704)
    at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:528)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:704)
    at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:528)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:704)
    at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:528)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:704)
    at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:106)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:528)
    at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:786)
    at SerializationTest.readObj(SerializationTest.java:39)
    at SerializationTest.main(SerializationTest.java:27)

我曾希望我可以序列化和反序列化PrintWriter对象outObj,并且对象的状态将保持完好,所以我仍然可以使用反序列化的对象来编写{{1这将保存在缓冲区中。

有谁知道发生了什么以及如何纠正这个错误?

2 个答案:

答案 0 :(得分:2)

我认为,您希望kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());避免构造函数调用。 更多信息here

但是,如果我可能会问,为什么世界上你想要序列化PrintWriter?这肯定是在惹麻烦。 Kryo不是"银弹",而它的默认派生程序可以与大多数类一起使用,这是实用的(即使这样,总是存在需要编写自定义的极端情况插件),你当然不能指望它能够处理你能想到的每一个奇特的东西(并且序列化由内部jvm特定代码支持的类,如sun.*绝对有资格作为异国情调。)

答案 1 :(得分:0)

这个错误在Kryo中很常见。问题是UTF_8类不公开,因此Kryo失败。添加下面的自定义序列化程序帮助我解决它。因为许多人都在努力解决这个问题,所以最好随Kryo一起发布以下类型的序列化器。

Custom serializer for kryo for UTF-8 and other charsets

通过这种方式,您告诉Kryo,对于所有已注册的Charset类,请调用我的自定义序列化程序。我刚刚发出字符串名称。