|
| 1 | +package io.github.dunwu.javalib.io; |
| 2 | + |
| 3 | +import com.esotericsoftware.kryo.Kryo; |
| 4 | +import com.esotericsoftware.kryo.io.Input; |
| 5 | +import com.esotericsoftware.kryo.io.Output; |
| 6 | +import com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy; |
| 7 | +import org.objenesis.strategy.StdInstantiatorStrategy; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.Base64; |
| 13 | + |
| 14 | +/** |
| 15 | + * <a href="https://github.com/EsotericSoftware/kryo">Kyro</a> 序列化/反序列化示例 |
| 16 | + * |
| 17 | + * @author <a href="mailto:forbreak@163.com">Zhang Peng</a> |
| 18 | + * @author <a href="https://www.cnblogs.com/hntyzgn/p/7122709.html">Kryo 使用指南</a> |
| 19 | + * @since 2019-11-26 |
| 20 | + */ |
| 21 | +public class KryoDemo { |
| 22 | + |
| 23 | + // 每个线程的 Kryo 实例 |
| 24 | + private static final ThreadLocal<Kryo> kryoLocal = ThreadLocal.withInitial(() -> { |
| 25 | + Kryo kryo = new Kryo(); |
| 26 | + |
| 27 | + /** |
| 28 | + * 不要轻易改变这里的配置!更改之后,序列化的格式就会发生变化, |
| 29 | + * 上线的同时就必须清除 Redis 里的所有缓存, |
| 30 | + * 否则那些缓存再回来反序列化的时候,就会报错 |
| 31 | + */ |
| 32 | + //支持对象循环引用(否则会栈溢出) |
| 33 | + kryo.setReferences(true); //默认值就是 true,添加此行的目的是为了提醒维护者,不要改变这个配置 |
| 34 | + |
| 35 | + //不强制要求注册类(注册行为无法保证多个 JVM 内同一个类的注册编号相同;而且业务系统中大量的 Class 也难以一一注册) |
| 36 | + kryo.setRegistrationRequired(false); //默认值就是 false,添加此行的目的是为了提醒维护者,不要改变这个配置 |
| 37 | + |
| 38 | + //Fix the NPE bug when deserializing Collections. |
| 39 | + ((DefaultInstantiatorStrategy) kryo.getInstantiatorStrategy()) |
| 40 | + .setFallbackInstantiatorStrategy(new StdInstantiatorStrategy()); |
| 41 | + |
| 42 | + return kryo; |
| 43 | + }); |
| 44 | + |
| 45 | + /** |
| 46 | + * 获得当前线程的 Kryo 实例 |
| 47 | + * |
| 48 | + * @return 当前线程的 Kryo 实例 |
| 49 | + */ |
| 50 | + public static Kryo getInstance() { |
| 51 | + return kryoLocal.get(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 将对象序列化为 byte 数组 |
| 56 | + * |
| 57 | + * @param obj 任意对象 |
| 58 | + * @param <T> 对象的类型 |
| 59 | + * @return 序列化后的 byte 数组 |
| 60 | + */ |
| 61 | + public static <T> byte[] writeToBytes(T obj) { |
| 62 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 63 | + Output output = new Output(byteArrayOutputStream); |
| 64 | + |
| 65 | + Kryo kryo = getInstance(); |
| 66 | + kryo.writeObject(output, obj); |
| 67 | + output.flush(); |
| 68 | + |
| 69 | + return byteArrayOutputStream.toByteArray(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 将对象序列化为 byte 数组后,再使用 Base64 编码 |
| 74 | + * |
| 75 | + * @param obj 任意对象 |
| 76 | + * @param <T> 对象的类型 |
| 77 | + * @return 序列化后的字符串 |
| 78 | + */ |
| 79 | + public static <T> String writeToString(T obj) { |
| 80 | + byte[] bytes = writeToBytes(obj); |
| 81 | + return new String(Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * 将 byte 数组反序列化为原对象 |
| 86 | + * |
| 87 | + * @param bytes {@link #writeToBytes} 方法序列化后的 byte 数组 |
| 88 | + * @param clazz 原对象的类型 |
| 89 | + * @param <T> 原对象的类型 |
| 90 | + * @return 原对象 |
| 91 | + */ |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + public static <T> T readFromBytes(byte[] bytes, Class<T> clazz) { |
| 94 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); |
| 95 | + Input input = new Input(byteArrayInputStream); |
| 96 | + |
| 97 | + Kryo kryo = getInstance(); |
| 98 | + return (T) kryo.readObject(input, clazz); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 将字符串反序列化为原对象,先使用 Base64 解码 |
| 103 | + * |
| 104 | + * @param str {@link #writeToString} 方法序列化后的字符串 |
| 105 | + * @param clazz 原对象的类型 |
| 106 | + * @param <T> 原对象的类型 |
| 107 | + * @return 原对象 |
| 108 | + */ |
| 109 | + public static <T> T readFromString(String str, Class<T> clazz) { |
| 110 | + byte[] bytes = str.getBytes(StandardCharsets.UTF_8); |
| 111 | + return readFromBytes(Base64.getDecoder().decode(bytes), clazz); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments