Kryo框架使用方法代码示例
作者:OK_boom 时间:2021-05-30 15:46:05
Kryo框架的source已移至https://github.com/EsotericSoftware/kryo ,进入此页面,然后点击右边的Download Zip按钮,就能下载到最新版本的Kryo框架。
导入Eclipse时,记得JDK/JRE选用 JDK1.7版本,因为Kryo会引用到unsafe()对象的一些方法JDK1.7才兼容。。
先来一个String类的序列化跟还原,是不是很简单?
</pre><pre name="code" class="java"> private static void testString () {
Kryo kryo=new Kryo();
String w_str1="简体中文,繁體中文,English";
//把w_str1对象序列化
Output output=new Output(1024);
kryo.writeObject(output, w_str1);
output.flush();
output.close();
byte[] w_ret= output.toBytes(); //获得byte数据,这些数据可用作储存、网络传输等...
//还原
Input input=new Input(w_ret);
input.close();
String w_str2=kryo.readObject(input, String.class);
System.out.println(w_str2);
}
再来一个HashMap类的序列化跟还原,因为Kryo自带了很多java基本类的Serializer,所以尽管不知道Serializer,Kryo也自动匹配:
public static void testHashMap() throws NoSuchAlgorithmException{
Kryo kryo=new Kryo();
HashMap h=new HashMap();
h.put("k1", "v1");
h.put("k2", "v2");
Output output=new Output(1, 1024);
kryo.writeObject(output, h);
output.close();
byte[] data=output.toBytes();
Input i=new Input(data);
i.close();
HashMap h2= (HashMap)kryo.readObject(i, HashMap.class);
System.out.println(h2.get("k2"));
}
那么,我自定义的Bean又应该如何处理呢?下面给出例子:
1、先定义Bean TestBean:
public static class TestBean implements Serializable{
private int[] intArray;
private HashMap<String,String> hashMapVal;
private String strVal;
public int[] getIntArray () {
return intArray;
}
public void setIntArray (int[] intArray) {
this.intArray = intArray;
}
public HashMap<String, String> getHashMapVal () {
return hashMapVal;
}
public void setHashMapVal (HashMap<String, String> hashMapVal) {
this.hashMapVal = hashMapVal;
}
public String getStrVal () {
return strVal;
}
public void setStrVal (String strVal) {
this.strVal = strVal;
}
}
2、因为这是自定义的Bean,Kryo在序列化前先要对TestBean进行注册:kryo.register(TestBean.class,new BeanSerializer(kryo, TestBean.class)); ,具体例子如下:
public static void testBean() throws NoSuchAlgorithmException{
Kryo kryo=new Kryo();
kryo.register(TestBean.class,new BeanSerializer(kryo, TestBean.class));
TestBean tb1=new TestBean();
tb1.setStrVal("test1");
tb1.setHashMapVal(new HashMap<String,String>());
tb1.getHashMapVal().put("k1", "v1");
tb1.getHashMapVal().put("k2", "v2");
int[] ints=new int[3];
ints[0]=1;
ints[1]=2;
ints[2]=3;
tb1.setIntArray(ints);
Output output=new Output(1, 1024);
kryo.writeObject(output, tb1);
output.close();
byte[] data=output.toBytes();
Input i=new Input(data);
i.close();
TestBean tb2= (TestBean)kryo.readObject(i, TestBean.class);
System.out.println(tb2.strVal);
System.out.println(tb2.hashMapVal.get("k1"));
System.out.println(tb2.intArray[2]);
}
总结
是不是非常简单?关于Kryo框架使用方法代码示例的介绍就到这里,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家的。
来源:http://blog.csdn.net/rocklee/article/details/26451739
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于jQuery获取table数据发送到后端
Spring Boot集成MyBatis的方法
![](https://img.aspxhome.com/file/2023/6/64486_0s.png)
java开发之MD5加密算法的实现
java并发编程专题(六)----浅析(JUC)Semaphore
Java 设计模式中的策略模式详情
![](https://img.aspxhome.com/file/2023/1/57731_0s.png)
java 使用idea将工程打成jar并创建成exe文件类型执行的方法详解
![](https://img.aspxhome.com/file/2023/2/60702_0s.png)
Java 在PDF中添加骑缝章示例解析
![](https://img.aspxhome.com/file/2023/5/59985_0s.png)
SpringMVC Restful api接口实现的代码
![](https://img.aspxhome.com/file/2023/8/60528_0s.jpg)
Android 应用签名的两种方法
springboot 自定义权限标签(tld),在freemarker引用操作
![](https://img.aspxhome.com/file/2023/9/59979_0s.jpg)
javaweb Servlet开发总结(一)
![](https://img.aspxhome.com/file/2023/0/65610_0s.jpg)
实例讲解JAVA设计模式之备忘录模式
一篇带你解析入门LongAdder源码
![](https://img.aspxhome.com/file/2023/1/60401_0s.png)
Springbean的几种注入方式都了解吗
Java开发学习 Eclipse项目有红感叹号解决之道
spring中bean的生命周期详解
java实现一个简单的网络爬虫代码示例
Java SpringMVC数据响应超详细讲解
![](https://img.aspxhome.com/file/2023/1/63161_0s.png)