java 使用kyro进行高性能序列化对象和集合



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;
}

}


你可能感兴趣的:(java,高性能)