序列化工具-Kryo-序列化和反序列化用法

一、环境依赖:

<dependency>
  <groupId>com.esotericsoftware</groupId>
  <artifactId>kryo</artifactId>
  <version>4.0.1</version>
</dependency>
<dependency>
  <groupId>com.esotericsoftware</groupId>
  <artifactId>kryo-shaded</artifactId>
  <version>4.0.1</version>
</dependency>

二、序列化&反序列化:

public interface ISerializer {
    <T> T deserialize(byte[] data);
    <T> byte[] serialize(T obj);
}
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.jason.seria.ISerializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class KryoSerializer implements ISerializer {
    Kryo stream=new Kryo();
    //序列化
    @Override
    public <T> byte[] serialize(T obj) {
// //支持对象循环引用(否则会栈溢出)
// stream.setReferences(true); //默认值就是 true,添加此行的目的是为了提醒维护者,不要改变这个配置
//
// //不强制要求注册类(注册行为无法保证多个 JVM 内同一个类的注册编号相同;而且业务系统中大量的 Class 也难以一一注册)
// stream.setRegistrationRequired(false); //默认值就是 false,添加此行的目的是为了提醒维护者,不要改变这个配置
 kryo.register(UnmodifiableCollectionsSerializer.class);
// UnmodifiableCollectionsSerializer.registerSerializers(stream);
//
// //Fix the NPE bug when deserializing Collections.
// ((Kryo.DefaultInstantiatorStrategy) stream.getInstantiatorStrategy())
// .setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
        byte[] bytes=null;
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        Output output = new Output(byteArrayOutputStream);
        stream.writeClassAndObject(output,obj);
        output.close();
        output.flush();
        return byteArrayOutputStream.toByteArray();
    }
    //反序列化
    @Override
    public <T> T deserialize(byte[] data) {
        ByteArrayInputStream byteArrayInputStream = new
                ByteArrayInputStream(data);
        Input input = new Input(byteArrayInputStream);
        T t=null;
        try {
            t =(T)stream.readClassAndObject(input);
            input.close();
            byteArrayInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return t;
        }
    }
}
public class TestMain {
    public static void main(String[] args) {
        Student stu=new Student();
        stu.setName("jason");
        stu.setAge(18);
        ISerializer serializer=new KryoSerializer();
        long t1 = System.nanoTime();
        byte[] serialize = serializer.serialize(stu);
        System.out.println("序列化耗时="+(System.nanoTime()-t1));
        System.out.println("hessian 序列化长度 result:"+serialize.length);
        System.out.println(""+new String(serialize));
        long t3 = System.nanoTime();
        Student stu2=(Student)serializer.deserialize(serialize);
        System.out.println("反序列化耗时="+(System.nanoTime()-t3));
    }
}

猜你喜欢

转载自blog.csdn.net/jason_jiahongfei/article/details/112645432