use of io.protostuff.Schema in project BRFS by zhangnianli.
the class ProtoStuffUtils method deserialize.
/**
* 通过字节数组解析对象
*
* @param bytes
* @param cls
* @return
*/
public static <T> T deserialize(byte[] bytes, Class<T> cls) {
T obj = null;
try {
obj = cls.newInstance();
@SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
ProtostuffIOUtil.mergeDelimitedFrom(new ByteArrayInputStream(bytes), obj, schema);
} catch (Exception e) {
obj = null;
}
return obj;
}
use of io.protostuff.Schema in project BRFS by zhangnianli.
the class ProtoStuffUtils method serialize.
/**
* 序列化对象为字节数组
*
* @param obj
* @return
* @throws IOException
*/
public static <T> byte[] serialize(T obj) throws IOException {
@SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
ProtostuffIOUtil.writeDelimitedTo(byteArrayOutput, obj, schema, LinkedBuffer.allocate(256));
return byteArrayOutput.toByteArray();
}
use of io.protostuff.Schema in project BRFS by zhangnianli.
the class ProtoStuffUtils method deserialize.
/**
* 通过字节数组解析对象
*
* @param bytes
* @param cls
* @return
*/
public static <T> T deserialize(byte[] bytes, Class<T> cls) {
T obj = null;
try {
obj = cls.newInstance();
@SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
ProtostuffIOUtil.mergeDelimitedFrom(new ByteArrayInputStream(bytes), obj, schema);
} catch (Exception e) {
obj = null;
}
return obj;
}
use of io.protostuff.Schema in project BRFS by zhangnianli.
the class ProtoStuffUtils method deserialize.
/**
* 通过字节数组解析对象
*
* @param bytes
* @param cls
* @return
*/
public static <T> T deserialize(byte[] bytes, Class<T> cls) {
T obj = null;
try {
obj = cls.newInstance();
@SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
ProtostuffIOUtil.mergeDelimitedFrom(new ByteArrayInputStream(bytes), obj, schema);
} catch (Exception e) {
obj = null;
}
return obj;
}
use of io.protostuff.Schema in project jim-framework by jiangmin168168.
the class ProtoStuffSerializeUtil method serializeList.
private static <T> byte[] serializeList(List<T> objs) {
if (null == objs || objs.size() == 0) {
return null;
}
Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objs.get(0).getClass());
LinkedBuffer buffer = LinkedBuffer.allocate();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] protostuff = null;
try {
ProtostuffIOUtil.writeListTo(byteArrayOutputStream, objs, schema, buffer);
protostuff = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new RpcException(e);
} finally {
buffer.clear();
try {
byteArrayOutputStream.close();
} catch (IOException e) {
logger.info("ByteArrayOutputStream close error:", e);
}
}
return protostuff;
}
Aggregations