作者:扩散平衡 | 来源:互联网 | 2023-09-23 10:04
Step01:定义mail类:packagecom.java.serializable;importjava.io.Serializable;importjava.util.D
Step01:定义mail类:
package com.java.serializable;import java.io.Serializable;
import java.util.Date;public class Mail implements Serializable{private static final long serialVersionUID = 6599166688654530165L;private Integer id;private String title;private String content;private Date createdTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Date getCreatedTime() {return createdTime;}public void setCreatedTime(Date createdTime) {this.createdTime = createdTime;}@Overridepublic String toString() {return "Mail [id=" + id + ", title=" + title + ", content=" + content + ", createdTime=" + createdTime + "]";}}
Step02:添加依赖
com.esotericsoftwarekryo5.0.0-RC4
Step03:编写测试类
package com.java.serializable;
import java.util.Date;import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
public class TestSerializable06 {public static void main(String[] args) {Mail m=new Mail();m.setId(100);m.setTitle("test");m.setContent("this is test content");m.setCreatedTime(new Date());//基于Kryo框架将对象序列化Kryo kryo=new Kryo();//将默认类的自动注册功能关闭(默认会将类全名序列化)kryo.setRegistrationRequired(false);//kryo.register(Mail.class);//kryo.register(Date.class);ByteArrayOutputStream bos=//内置可扩容数组new ByteArrayOutputStream();Output output=new Output(bos);kryo.writeObject(output, m);output.close();System.out.println("序列化ok");//基于Kryo框架将对象反序列化byte[] data=bos.toByteArray();Input input=new Input(data);Mail m2=kryo.readObject(input,Mail.class);input.close();System.out.println(m2);}
}
结果:
序列化ok
Mail [id=100, title=test, content=this is test content, createdTime=Mon Nov 11 14:15:35 CST 2019]
说明:可将如上序列化方法进行封装,写到序列化工具类中,例如
package com.java.serializable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
//基于Kryo框架实现对象序列化和反序列化.
/*** kryo 对象是一个线程不安全对象,不允许多线程共享.* 可以每个线程有一份。那如何保证每个线程此类的实例* 只有一份呢?可以借助ThreadLocal实现。* * ThreadLocal 提供一种线程绑定机制,可以基于* 此对象将某个对象绑定当前线程中,也可以从当前* 线程获取某个对象.* 1)set() 绑定* 2)get() 获取*/
public class KryoSerializationUtil {//static Kryo kryo = new Kryo();//线程共享private static final ThreadLocal kryos = new ThreadLocal() {//获取当前线程中Kryo对象时,假如线程中没有此对象//此时会调用initialValue创建对象并通过set方法绑定当前线程protected Kryo initialValue() {Kryo kryo = new Kryo();kryo.setRegistrationRequired(false);// Configure the Kryo instance.return kryo;};};//序列化public static byte[] serializable(T t) throws IOException{//1.构建kryo对象//Kryo k = new Kryo();//k.setRegistrationRequired(false);//2.构建字节数组输出流(内置可扩容数组)ByteArrayOutputStream bos=new ByteArrayOutputStream();//3.构建处理流output对象Output output=new Output(bos);//4.将对象序列化kryos.get().writeObject(output, t);output.flush();byte[] array=bos.toByteArray();output.close();return array;}//反序列化public staticT deserialization(byte[] array,Class cls) {//1.构建kryo对象//Kryo k=new Kryo();//k.setRegistrationRequired(false);//2.构建input对象(负责读字节数据)Input input=new Input(array);//3.反序列化数据T t=(T)kryos.get().readObject(input,cls);input.close();return t;}
}
编写测试类:
package com.java.serializable;
import java.io.IOException;
import java.util.Date;
public class TestSerializable07 {public static void main(String[] args)throws IOException {Mail m=new Mail();m.setId(100);m.setTitle("test");m.setContent("this is test content");m.setCreatedTime(new Date());//基于Kryo框架将对象序列化byte[] array=KryoSerializationUtil.serializable(m);System.out.println("序列化OK,array.length="+array.length);//基于Kryo框架将对象反序列化Mail m2=KryoSerializationUtil.deserialization(array,Mail.class);System.out.println(m2);}
}
运行结果:
序列化OK,array.length=49
Mail [id=100, title=test, content=this is test content, createdTime=Mon Nov 11 18:04:03 CST 2019]
知识补充:
ThreadLocal原理及详解参考文章:https://www.cnblogs.com/dolphin0520/p/3920407.html