Search in sources :

Example 1 with Schema

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) IOException(java.io.IOException)

Example 2 with Schema

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();
}
Also used : Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with Schema

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) IOException(java.io.IOException)

Example 4 with Schema

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) IOException(java.io.IOException)

Example 5 with Schema

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;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) RpcException(com.jim.framework.rpc.exception.RpcException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RpcException(com.jim.framework.rpc.exception.RpcException) IOException(java.io.IOException)

Aggregations

Schema (io.protostuff.Schema)15 RuntimeSchema (io.protostuff.runtime.RuntimeSchema)15 IOException (java.io.IOException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 RpcException (com.jim.framework.rpc.exception.RpcException)2 LinkedBuffer (io.protostuff.LinkedBuffer)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1