java kryo序列化与反序列化

https://blog.csdn.net/lan12334321234/article/details/84907492

 

 问题:

https://blog.csdn.net/baidu_38404101/article/details/79103216

实用

 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.JavaSerializer;

public class KyroTest {

public static void main(String[] args) {
List list = new ArrayList();
Person p ;
for(int i=0;i<=100000;i++){
p = new Person();
p.setId(i);
list.add(p);
}
long begin = System.currentTimeMillis();
String listStr = serializationList(list,Person.class);
System.out.println(listStr.length());
System.out.println(System.currentTimeMillis()-begin);
begin = System.currentTimeMillis();
deserializationList(listStr,Person.class);
System.out.println(System.currentTimeMillis()-begin);
}
@SuppressWarnings("all")
private static String serializationList(List obj, Class clazz) {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.setRegistrationRequired(true);

CollectionSerializer serializer = new CollectionSerializer();
serializer.setElementClass(clazz, new JavaSerializer());
serializer.setElementsCanBeNull(false);

kryo.register(clazz, new JavaSerializer());
kryo.register(ArrayList.class, serializer);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeObject(output, obj);
output.flush();
output.close();

byte[] b = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return new String(new Base64().encode(b));
}

@SuppressWarnings("all")
private static List deserializationList(String obj, Class clazz) {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.setRegistrationRequired(true);

CollectionSerializer serializer = new CollectionSerializer();
serializer.setElementClass(clazz, new JavaSerializer());
serializer.setElementsCanBeNull(false);

kryo.register(clazz, new JavaSerializer());
kryo.register(ArrayList.class, serializer);

ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(obj));
Input input = new Input(bais);
return (List) kryo.readObject(input, ArrayList.class, serializer);
}

/**
* 序列化对象
*
* @param obj
* @return
*/
@SuppressWarnings("all")
private static String serializationObject(T obj) {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.register(obj.getClass(), new JavaSerializer());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeClassAndObject(output, obj);
output.flush();
output.close();
byte[] b = baos.toByteArray();
try {
baos.flush();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}

return new String(new Base64().encode(b));
}

/**
* 反序列化
*
* @param obj
* @param clazz
* @return
*/
@SuppressWarnings("all")
private T deserializationObject(String obj, Class clazz) {
Kryo kryo = new Kryo();
kryo.setReferences(false);
kryo.register(clazz, new JavaSerializer());

ByteArrayInputStream bais = new ByteArrayInputStream(new Base64().decode(obj));
Input input = new Input(bais);
return (T) kryo.readClassAndObject(input);
}
}
class Person implements Serializable{

private static final long serialVersionUID = 1L;
private int id ;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}
---------------------
作者:艾伦蓝
来源:CSDN
原文:https://blog.csdn.net/lan12334321234/article/details/84907492
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/zhangbojiangfeng/p/11270739.html

你可能感兴趣的:(java kryo序列化与反序列化)