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 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 dubbo by alibaba.
the class ProtostuffObjectOutput method writeObject.
@SuppressWarnings("unchecked")
@Override
public void writeObject(Object obj) throws IOException {
byte[] bytes;
byte[] classNameBytes;
try {
if (obj == null || WrapperUtils.needWrapper(obj)) {
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
Wrapper wrapper = new Wrapper(obj);
bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer);
classNameBytes = Wrapper.class.getName().getBytes();
} else {
Schema schema = RuntimeSchema.getSchema(obj.getClass());
bytes = GraphIOUtil.toByteArray(obj, schema, buffer);
classNameBytes = obj.getClass().getName().getBytes();
}
} finally {
buffer.clear();
}
dos.writeInt(classNameBytes.length);
dos.writeInt(bytes.length);
dos.write(classNameBytes);
dos.write(bytes);
}
use of io.protostuff.Schema in project dubbo by alibaba.
the class ProtostuffObjectInput method readObject.
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public Object readObject() throws IOException, ClassNotFoundException {
int classNameLength = dis.readInt();
int bytesLength = dis.readInt();
if (classNameLength < 0 || bytesLength < 0) {
throw new IOException();
}
byte[] classNameBytes = new byte[classNameLength];
dis.readFully(classNameBytes, 0, classNameLength);
byte[] bytes = new byte[bytesLength];
dis.readFully(bytes, 0, bytesLength);
String className = new String(classNameBytes);
Class clazz = Class.forName(className);
Object result;
if (WrapperUtils.needWrapper(clazz)) {
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
Wrapper wrapper = schema.newMessage();
GraphIOUtil.mergeFrom(bytes, wrapper, schema);
result = wrapper.getData();
} else {
Schema schema = RuntimeSchema.getSchema(clazz);
result = schema.newMessage();
GraphIOUtil.mergeFrom(bytes, result, schema);
}
return result;
}
use of io.protostuff.Schema in project scalecube by scalecube.
the class MessageSchema method mergeFrom.
@Override
public void mergeFrom(Input input, Message message) throws IOException {
// Read input data
boolean iterate = true;
List<String> headerKeys = new ArrayList<>();
List<String> headerValues = new ArrayList<>();
String senderHost = null;
int senderPort = 0;
byte[] dataBytes = null;
while (iterate) {
int number = input.readFieldNumber(this);
switch(number) {
case 0:
iterate = false;
break;
case HEADER_KEYS_FIELD_NUMBER:
headerKeys.add(input.readString());
break;
case HEADER_VALUES_FIELD_NUMBER:
headerValues.add(input.readString());
break;
case DATA_FIELD_NUMBER:
dataBytes = input.readByteArray();
break;
case SENDER_HOST_FIELD_NUMBER:
senderHost = input.readString();
break;
case SENDER_PORT_FIELD_NUMBER:
senderPort = input.readInt32();
break;
default:
input.handleUnknownField(number, this);
break;
}
}
// Deserialize headers
Map<String, String> headers = new HashMap<>(headerKeys.size());
if (!headerKeys.isEmpty()) {
ListIterator<String> headerValuesIterator = headerValues.listIterator();
for (String key : headerKeys) {
String value = headerValuesIterator.next();
headers.put(key, value);
}
}
// Deserialize data
Object data = null;
if (dataBytes != null) {
String dataType = headers.get(Message.HEADER_DATA_TYPE);
if (dataType == null) {
data = dataBytes;
} else {
Optional<Class> optionalDataClass = classCache.computeIfAbsent(dataType, this::classForName);
if (optionalDataClass.isPresent()) {
headers.remove(Message.HEADER_DATA_TYPE);
Class<?> dataClass = optionalDataClass.get();
Schema dataSchema = RuntimeSchema.getSchema(dataClass);
data = dataSchema.newMessage();
try {
ProtostuffIOUtil.mergeFrom(dataBytes, data, dataSchema);
} catch (Throwable e) {
LOGGER.error("Failed to deserialize : {}", message);
throw e;
}
} else {
data = dataBytes;
}
}
}
// Deserialize sender
Address sender = senderHost != null ? Address.create(senderHost, senderPort) : null;
// Set message
message.setHeaders(headers);
message.setData(data);
message.setSender(sender);
}
Aggregations