玉器网站模版,网站建设和运营的课程,建设银行网站 无法访问,军事新闻俄乌最新消息java相关#xff1a;Kryo框架使用方法代码示例发布于 2021-1-21|复制链接摘记: Kryo框架的source已移至https://github.com/EsotericSoftware/kryo #xff0c;进入此页面#xff0c;然后点击右边的Download Zip按钮#xff0c;就能下载到最新版本的Kr ..Kryo框架的source已…java相关Kryo框架使用方法代码示例发布于 2021-1-21|复制链接摘记: Kryo框架的source已移至https://github.com/EsotericSoftware/kryo 进入此页面然后点击右边的Download Zip按钮就能下载到最新版本的Kr ..Kryo框架的source已移至https://github.com/EsotericSoftware/kryo 进入此页面然后点击右边的Download Zip按钮就能下载到最新版本的Kryo框架。 导入Eclipse时记得JDK/JRE选用 JDK1.7版本因为Kryo会引用到unsafe()对象的一些方法JDK1.7才兼容。。先来一个String类的序列化跟还原是不是很简单javaprivate static void testString () {Kryo kryonew Kryo();String w_str1简体中文繁體中文English;//把w_str1对象序列化Output outputnew Output(1024);kryo.writeObject(output, w_str1);output.flush();output.close();byte[] w_ret output.toBytes(); //获得byte数据,这些数据可用作储存、网络传输等...//还原Input inputnew Input(w_ret);input.close();String w_str2kryo.readObject(input, String.class);System.out.println(w_str2);}再来一个HashMap类的序列化跟还原因为Kryo自带了很多java基本类的Serializer所以尽管不知道SerializerKryo也自动匹配javapublic static void testHashMap() throws NoSuchAlgorithmException{Kryo kryonew Kryo();HashMap hnew HashMap();h.put(k1, v1);h.put(k2, v2);Output outputnew Output(1, 1024);kryo.writeObject(output, h);output.close();byte[] dataoutput.toBytes();Input inew Input(data);i.close();HashMap h2 (HashMap)kryo.readObject(i, HashMap.class);System.out.println(h2.get(k2));}那么我自定义的Bean又应该如何处理呢下面给出例子1、先定义Bean TestBeanjavapublic static class TestBean implements Serializable{private int[] intArray;private HashMap hashMapVal;private String strVal;public int[] getIntArray () {return intArray;}public void setIntArray (int[] intArray) {this.intArray intArray;}public HashMap getHashMapVal () {return hashMapVal;}public void setHashMapVal (HashMap hashMapVal) {this.hashMapVal hashMapVal;}public String getStrVal () {return strVal;}public void setStrVal (String strVal) {this.strVal strVal;}}2、因为这是自定义的BeanKryo在序列化前先要对TestBean进行注册kryo.register(TestBean.class,new BeanSerializer(kryo, TestBean.class)); 具体例子如下javapublic static void testBean() throws NoSuchAlgorithmException{Kryo kryonew Kryo();kryo.register(TestBean.class,new BeanSerializer(kryo, TestBean.class));TestBean tb1new TestBean();tb1.setStrVal(test1);tb1.setHashMapVal(new HashMap());tb1.getHashMapVal().put(k1, v1);tb1.getHashMapVal().put(k2, v2);int[] intsnew int[3];ints[0]1;ints[1]2;ints[2]3;tb1.setIntArray(ints);Output outputnew Output(1, 1024);kryo.writeObject(output, tb1);output.close();byte[] dataoutput.toBytes();javaInput inew 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]);}