scray.common.serialization.pool.KryoJavaPoolSerialization Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scray-client-jdbc Show documentation
Show all versions of scray-client-jdbc Show documentation
scray java code, which can be used from java and scala
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;
}
}
}