2

kryoを使用して Java コレクションをシリアライズおよびデシリアライズしようとしています。

シリアル化メソッドは次のようになります。

public <T> byte[] serialize(List<T> objectsToSerialize) {
CollectionSerializer cs = new CollectionSerializer();
Output output = new Output();
cs.write(kryo, output, objectsToSerialize);
return output.toBytes();
}

今度は逆シリアル化の方法を書きたいのですが、うまくいきません。基本的に CollectionSerializer には読み取りメソッドがありますが、その使用方法がわかりません (また、私が知る限り、ドキュメントはかなり貧弱です)。

何か案は?

4

1 に答える 1

1

a をファイルにシリアル化するだけの場合はCollection、標準の Kryo コマンド ( などkryo.writeClassAndObject) で十分に処理できます。たとえば、次のプログラムは、文字列のコレクションをファイル「testfile」に渡し、再び戻します。

public class TestClass{


    public static void main(String[] args) throws FileNotFoundException{
        serialize();
        deSerialize();
    }

    public static void serialize() throws FileNotFoundException{
        Collection<String>collection=new ArrayList<>();

        collection.add("This is a serialized collection of strings");

        Kryo kryo = new Kryo();
        Output output = new Output(new FileOutputStream("testfile"));
        kryo.writeClassAndObject(output, collection);
        output.close();
    }

    public static void deSerialize() throws FileNotFoundException{
        Collection<String>collection;

        Kryo kryo = new Kryo();
        Input input = new Input(new FileInputStream("testfile"));
        collection=(Collection<String>)kryo.readClassAndObject(input);
        input.close();

        for(String string: collection){
            System.out.println(string);
        }

    }


}
于 2014-01-07T08:48:07.160 に答える