Kryo序列化工具类

1.使用 Kryo 需要添加的 Maven 依赖

        <dependency>
            <groupId>com.esotericsoftware</groupId>
            <artifactId>kryo</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.esotericsoftware.kryo</groupId>
            <artifactId>kryo5</artifactId>
            <version>5.3.0</version>
        </dependency>

2.工具类KryoSerializerUtil.java

public class KryoSerializerUtil {
    /**
     * Kryo序列化
     * @param obj
     * @param filepath
     * @param <T>
     * @throws FileNotFoundException
     */
    public static <T> void serialize(T obj, String filepath) throws FileNotFoundException {
        Kryo kryo = new Kryo();
        //支持对象循环引用(否则会栈溢出)
        kryo.setReferences(true); //默认值就是 true,添加此行的目的是为了提醒维护者,不要改变这个配置
        //不强制要求注册类(注册行为无法保证多个 JVM 内同一个类的注册编号相同;而且业务系统中大量的 Class 也难以一一注册)
        kryo.setRegistrationRequired(false); //默认值就是 false,添加此行的目的是为了提醒维护者,不要改变这个配置
        try {
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filepath));
            Output output = new Output(bufferedOutputStream);
            kryo.writeObject(output, obj);
            output.close();
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * kryo反序列化
     * @param clazz
     * @param filepath
     * @param <T>
     * @return
     */
    public static <T> T deserialize(Class<T> clazz, String filepath) {
        try {
            Kryo kryo = new Kryo();
            //支持对象循环引用(否则会栈溢出)
            kryo.setReferences(true); //默认值就是 true,添加此行的目的是为了提醒维护者,不要改变这个配置
            //不强制要求注册类(注册行为无法保证多个 JVM 内同一个类的注册编号相同;而且业务系统中大量的 Class 也难以一一注册)
            kryo.setRegistrationRequired(false); //默认值就是 false,添加此行的目的是为了提醒维护者,不要改变这个配置

            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filepath));
            Input input = new Input(bufferedInputStream);
            T t = kryo.readObject(input, clazz);
            input.close();
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

3.实例演示

public class KryoSerializerUtilTest {
    public static void main(String[] args) throws FileNotFoundException {
        String filepath="D:\\file\\file.bin";
        Car car = new Car();
        car.setName("宝马7系");
        car.setBrand("宝马");
        car.setPrice(new BigDecimal(1000000.00));
        KryoSerializerUtil.serialize(car,filepath);

        Car deserialize = KryoSerializerUtil.deserialize(car.getClass(), filepath);
        System.out.println(deserialize.toString());

    }

}

@Data
@ToString
class Car implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private BigDecimal price;
    private String brand;
}

4.演示结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值