org.voovan.tools.serialize.ProtoStuffSerialize Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voovan-framework Show documentation
Show all versions of voovan-framework Show documentation
Voovan is a java framwork and it not depends on any third-party framework.
package org.voovan.tools.serialize;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import org.voovan.tools.TByte;
import org.voovan.tools.collection.ObjectThreadPool;
import org.voovan.tools.reflect.TReflect;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* ProtoStuff 的序列化实现
*
* @author: helyho
* ignite-test Framework.
* WebSite: https://github.com/helyho/ignite-test
* Licence: Apache v2 License
*/
public class ProtoStuffSerialize implements Serialize {
ObjectThreadPool objectThreadPool = new ObjectThreadPool(128);
Map SCHEMAS = new ConcurrentHashMap();
public Schema getSchema(Class clazz) {
Schema schema = SCHEMAS.get(clazz);
if(schema == null) {
schema = RuntimeSchema.getSchema(clazz);
}
return schema;
}
@Override
public byte[] serialize(Object obj) {
byte[] buf = null;
buf = TByte.toBytes(obj);
if(buf==null) {
Schema schema = getSchema(obj.getClass());
LinkedBuffer buffer =objectThreadPool .get(()->LinkedBuffer.allocate(512));
try {
buf = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
} finally {
buffer.clear();
}
}
byte[] type = (TSerialize.getSimpleNameByClass(obj.getClass())+"\0").getBytes();
buf = TByte.byteArrayConcat(type, type.length, buf, buf.length);
return buf;
}
@Override
public T unserialize(byte[] bytes) {
byte[] type = new byte[512];
int index = 0;
for(index=0;index
© 2015 - 2024 Weber Informatics LLC | Privacy Policy