package scray.common.serialization.pool;

import java.util.LinkedList;
import java.util.List;

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

public class KryoJavaPoolSerialization {

	private KryoJavaPoolSerialization() {
	}

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

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

	private int POOL_SIZE = 10;
	private ScrayJavaKryoInstantiator instantiator = new ScrayJavaKryoInstantiator();
	private List<SerializerEntry<?>> serializers = new LinkedList<SerializerEntry<?>>();

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

	public List<SerializerEntry<?>> getSerializers() {
		return serializers;
	}

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

	class SerializerEntry<T> {
		Class<T> cls;
		Serializer<T> ser;
		int num;

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

}

@@ -35,16 +35,44 @@
 .pool;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -195,16 +195,53 @@
 ryoPool;
+%0Aimport com.twitter.chill.SerDeState;
 %0A%0Apublic
@@ -274,16 +274,436 @@
 zation %7B
+%0A%09public Object fromStream(InputStream stream) %7B%0A%09%09SerDeState serde = chill.borrow();%0A%09%09try %7B%0A%09%09%09serde.setInput(stream);%0A%09%09%09return serde.readClassAndObject();%0A%09%09%7D finally %7B%0A%09%09%09chill.release(serde);%0A%09%09%7D%0A%09%7D%0A%0A%09public %3CT%3E T fromStream(InputStream stream,%0A%09%09%09Class%3CT%3E cls) %7B%0A%09%09SerDeState serde = chill.borrow();%0A%09%09try %7B%0A%09%09%09serde.setInput(stream);%0A%09%09%09return serde.readObject(cls);%0A%09%09%7D finally %7B%0A%09%09%09chill.release(serde);%0A%09%09%7D%0A%09%7D
 %0A%0A%09priva

extend kryo pool-based serializer for streams