Search in sources :

Example 11 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 12 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 13 with Schema

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

Example 14 with Schema

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

Example 15 with Schema

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);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) Schema(io.protostuff.Schema) ArrayList(java.util.ArrayList)

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