com.kould.utils.ProtostuffUtils Maven / Gradle / Ivy
package com.kould.utils;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
public class ProtostuffUtils {
private static final Schema schema = RuntimeSchema.getSchema(ObjectWrapper.class);
public static byte[] serialize(Object t) {
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
byte[] bytes;
try {
bytes = ProtostuffIOUtil.toByteArray(new ObjectWrapper(t), schema, buffer);
} finally {
buffer.clear();
}
return bytes;
}
public static Object deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
try {
ObjectWrapper objectWrapper = new ObjectWrapper();
ProtostuffIOUtil.mergeFrom(bytes, objectWrapper, schema);
return objectWrapper.getObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static class ObjectWrapper {
private Object object;
ObjectWrapper() {
}
ObjectWrapper(Object object) {
this.object = object;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
}