Search in sources :

Example 6 with Schema

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

Example 7 with Schema

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);
    }
}
Also used : RuntimeSchema(io.protostuff.runtime.RuntimeSchema) Schema(io.protostuff.Schema) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with Schema

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

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

Example 10 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)

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