zoukankan      html  css  js  c++  java
  • java kryo序列化与反序列化

    https://blog.csdn.net/lan12334321234/article/details/84907492

     问题:

    https://blog.csdn.net/baidu_38404101/article/details/79103216

    实用

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.commons.codec.binary.Base64;

    import com.esotericsoftware.kryo.Kryo;
    import com.esotericsoftware.kryo.io.Input;
    import com.esotericsoftware.kryo.io.Output;
    import com.esotericsoftware.kryo.serializers.CollectionSerializer;
    import com.esotericsoftware.kryo.serializers.JavaSerializer;

    public class KyroTest {

    public static void main(String[] args) {
    List<Person> list = new ArrayList<Person>();
    Person p ;
    for(int i=0;i<=100000;i++){
    p = new Person();
    p.setId(i);
    list.add(p);
    }
    long begin = System.currentTimeMillis();
    String listStr = serializationList(list,Person.class);
    System.out.println(listStr.length());
    System.out.println(System.currentTimeMillis()-begin);
    begin = System.currentTimeMillis();
    deserializationList(listStr,Person.class);
    System.out.println(System.currentTimeMillis()-begin);
    }
    @SuppressWarnings("all")
    private static <T extends Serializable> String serializationList(List<T> obj, Class<T> clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);

    CollectionSerializer serializer = new CollectionSerializer();
    serializer.setElementClass(clazz, new JavaSerializer());
    serializer.setElementsCanBeNull(false);

    kryo.register(clazz, new JavaSerializer());
    kryo.register(ArrayList.class, serializer);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeObject(output, obj);
    output.flush();
    output.close();

    byte[] b = baos.toByteArray();
    try {
    baos.flush();
    baos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return new String(new Base64().encode(b));
    }

    @SuppressWarnings("all")
    private static <T extends Serializable> List<T> deserializationList(String obj, Class<T> clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.setRegistrationRequired(true);

    CollectionSerializer serializer = new CollectionSerializer();
    serializer.setElementClass(clazz, new JavaSerializer());
    serializer.setElementsCanBeNull(false);

    kryo.register(clazz, new JavaSerializer());
    kryo.register(ArrayList.class, serializer);

    ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(obj));
    Input input = new Input(bais);
    return (List<T>) kryo.readObject(input, ArrayList.class, serializer);
    }

    /**
    * 序列化对象
    *
    * @param obj
    * @return
    */
    @SuppressWarnings("all")
    private static <T extends Serializable> String serializationObject(T obj) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(obj.getClass(), new JavaSerializer());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeClassAndObject(output, obj);
    output.flush();
    output.close();
    byte[] b = baos.toByteArray();
    try {
    baos.flush();
    baos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    return new String(new Base64().encode(b));
    }

    /**
    * 反序列化
    *
    * @param obj
    * @param clazz
    * @return
    */
    @SuppressWarnings("all")
    private <T extends Serializable> T deserializationObject(String obj, Class<T> clazz) {
    Kryo kryo = new Kryo();
    kryo.setReferences(false);
    kryo.register(clazz, new JavaSerializer());

    ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(obj));
    Input input = new Input(bais);
    return (T) kryo.readClassAndObject(input);
    }
    }
    class Person implements Serializable{

    private static final long serialVersionUID = 1L;
    private int id ;

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    }
    ---------------------
    作者:艾伦蓝
    来源:CSDN
    原文:https://blog.csdn.net/lan12334321234/article/details/84907492
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    NOI 2020 游记
    BJOI2020 游记
    行列式
    CSP-S 2019 游记
    类欧几里得算法
    有关二次离线和 Yuno loves sqrt technology II
    NOI2019 游记
    CTS 2019 Pearl
    BJOI2019 游记
    2017 山东二轮集训 Day7 国王
  • 原文地址:https://www.cnblogs.com/zhangbojiangfeng/p/11270739.html
Copyright © 2011-2022 走看看