冷冻 serialization/deserialization

Kryo serialization/deserialization

我正在尝试使用 kryo 序列化和反序列化为二进制文件。我想我有序列化工作,但我似乎无法反序列化。下面是我正在处理的代码,但最终我想存储一个 byte[] 并稍后再次读取它。文档只展示了如何处理文件。

        Kryo kryo = new Kryo();
        kryo.register(ArrayList.class);
        kryo.register(Modifier.class);

        ByteArrayOutputStream b = new ByteArrayOutputStream();

        Modifier modifier = new Modifier();
        modifier.type = "Yo swa!";
        modifier.amount = 10;

        Output output = new Output(b);
        kryo.writeClassAndObject(output, modifier);

        System.out.println("Kryo size: " + output.toBytes().length);

        Input input = new Input(output.getBuffer());

        //Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 0
        Modifier r = kryo.readObject(input, Modifier.class);
        //Same error:
        Modifier r = kryo.readObject(new Input(new ByteArrayInputStream(output.toBytes())), Modifier.class);

        System.out.println(r.type);

你用 writeClassAndObject 方法序列化,用 readObject 反序列化,这不兼容。

您应该使用 writeClassAndObject => readClassAndObjectwriteObject => readObject.

以下对我有效(kryo 版本 2.21)。

writeObject =>readObject

package test;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class TestMain {

    public static void main(String[] args) {
        Kryo kryo = new Kryo();
        kryo.register(ArrayList.class);
        kryo.register(Modifier.class);

        ByteArrayOutputStream b = new ByteArrayOutputStream();

        Modifier modifier = new Modifier();
        modifier.type = "Yo swa!";
        modifier.amount = 10;

        Output output = new Output(b);
        kryo.writeObject(output, modifier);
        output.flush();
        output.close();

        System.out.println("Kryo size: " + output.toBytes().length);

        Input input = new Input(output.getBuffer());

        Modifier r = kryo.readObject(input, Modifier.class);

        System.out.println(r.type);
    }

    static class Modifier {
        String type;
        int amount;
    }
}

writeClassAndObject => readClassAndObject

package test;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class TestMain {

    public static void main(String[] args) {
        Kryo kryo = new Kryo();
        kryo.register(ArrayList.class);
        kryo.register(Modifier.class);

        ByteArrayOutputStream b = new ByteArrayOutputStream();

        Modifier modifier = new Modifier();
        modifier.type = "Yo swa!";
        modifier.amount = 10;

        Output output = new Output(b);
        kryo.writeClassAndObject(output, modifier);
        output.flush();
        output.close();

        System.out.println("Kryo size: " + output.toBytes().length);

        Input input = new Input(output.getBuffer());

        Modifier r = (Modifier) kryo.readClassAndObject(input);

        System.out.println(r.type);
    }

    static class Modifier {
        String type;
        int amount;
    }
}