use of io.protostuff.Schema in project jim-framework by jiangmin168168.
the class ProtoStuffSerializeUtil method serialize.
public static <T> byte[] serialize(T obj) {
if (obj == null) {
throw new RpcException("ProtoStuffSerialize.serialize: obj is null");
}
if (obj instanceof List) {
return serializeList((List) obj);
}
Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
LinkedBuffer buffer = LinkedBuffer.allocate();
try {
return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
buffer.clear();
}
}
use of io.protostuff.Schema in project scalecube by scalecube.
the class MessageSchema method writeTo.
@Override
public void writeTo(Output output, Message message) throws IOException {
// Write headers
if (!message.headers().isEmpty()) {
for (Map.Entry<String, String> headerEntry : message.headers().entrySet()) {
if (headerEntry.getKey() != null && headerEntry.getValue() != null) {
output.writeString(HEADER_KEYS_FIELD_NUMBER, headerEntry.getKey(), true);
output.writeString(HEADER_VALUES_FIELD_NUMBER, headerEntry.getValue(), true);
}
}
}
// Write data
Object originalData = message.data();
if (originalData != null) {
if (originalData instanceof byte[]) {
// Write data byte array as is
output.writeByteArray(DATA_FIELD_NUMBER, (byte[]) originalData, false);
} else {
// Write data class as an additional header
Class<?> dataClass = originalData.getClass();
output.writeString(HEADER_KEYS_FIELD_NUMBER, Message.HEADER_DATA_TYPE, true);
output.writeString(HEADER_VALUES_FIELD_NUMBER, dataClass.getName(), true);
// Write data as serialized byte array
Schema dataSchema = RuntimeSchema.getSchema(dataClass);
try (RecyclableLinkedBuffer rlb = recyclableLinkedBuffer.get()) {
byte[] array = ProtostuffIOUtil.toByteArray(originalData, dataSchema, rlb.buffer());
output.writeByteArray(DATA_FIELD_NUMBER, array, false);
}
}
}
// Write sender
Address sender = message.sender();
if (sender != null) {
output.writeString(SENDER_HOST_FIELD_NUMBER, sender.host(), false);
output.writeInt32(SENDER_PORT_FIELD_NUMBER, sender.port(), false);
}
}
use of io.protostuff.Schema in project BRFS by zhangnianli.
the class ProtoStuffUtils method deserializeThrowable.
/**
* 通过字节数组解析对象
*
* @param bytes
* @param cls
* @return
* @throws Exception
*/
public static <T> T deserializeThrowable(byte[] bytes, Class<T> cls) throws Exception {
T obj = newInstance(cls);
@SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
ProtostuffIOUtil.mergeDelimitedFrom(new ByteArrayInputStream(bytes), obj, schema);
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 = newInstance(cls);
@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();
}
Aggregations