All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scray.common.serialization.pool.KryoJavaPoolSerialization Maven / Gradle / Ivy

The newest version!
package scray.common.serialization.pool;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import com.esotericsoftware.kryo.Serializer;
import com.twitter.chill.KryoPool;
import com.twitter.chill.SerDeState;

public class KryoJavaPoolSerialization {
	public Object fromStream(InputStream stream) {
		SerDeState serde = chill.borrow();
		try {
			serde.setInput(stream);
			return serde.readClassAndObject();
		} finally {
			chill.release(serde);
		}
	}

	public  T fromStream(InputStream stream,
			Class cls) {
		SerDeState serde = chill.borrow();
		try {
			serde.setInput(stream);
			return serde.readObject(cls);
		} finally {
			chill.release(serde);
		}
	}

	private KryoJavaPoolSerialization() {
	}

	private static class KryoJavaPoolSerializationHolder {
		private static KryoJavaPoolSerialization instance = new KryoJavaPoolSerialization();
	}

	public static KryoJavaPoolSerialization getInstance() {
		return KryoJavaPoolSerializationHolder.instance;
	}

	private int POOL_SIZE = 100;
	private ScrayJavaKryoInstantiator instantiator = new ScrayJavaKryoInstantiator();
	private List> serializers = new LinkedList>();

	public KryoPool chill = KryoPool.withByteArrayOutputStream(POOL_SIZE,
			instantiator);

	public List> getSerializers() {
		return serializers;
	}

	public  void register(Class cls, Serializer serializer, int number) {
		serializers.add(new SerializerEntry(cls, serializer, number));
	}

	class SerializerEntry {
		Class cls;
		Serializer ser;
		int num;

		public SerializerEntry(Class cls, Serializer ser, int num) {
			this.cls = cls;
			this.ser = ser;
			this.num = num;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy