`

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<Person> list = new ArrayList<Person>();
		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 <T extends Serializable> String serializationList(List<T> obj, Class<T> 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 <T extends Serializable> List<T> deserializationList(String obj, Class<T> 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<T>) kryo.readObject(input, ArrayList.class, serializer);
	}

	/**
	 * 序列化对象
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("all")
	private static <T extends Serializable> 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 extends Serializable> T deserializationObject(String obj, Class<T> 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;
	}
	
}







分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics