All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.net.tcp.codec.flex.decode.DefaultMetaInfoParser Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp.codec.flex.decode;

import com.firefly.net.tcp.codec.flex.model.MetaInfo;
import com.firefly.net.tcp.codec.flex.model.Request;
import com.firefly.net.tcp.codec.flex.model.Response;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

/**
 * @author Pengtao Qiu
 */
public class DefaultMetaInfoParser implements MetaInfoParser {

    public static final Schema requestSchema = RuntimeSchema.getSchema(Request.class);
    public static final Schema responseSchema = RuntimeSchema.getSchema(Response.class);

    @SuppressWarnings("unchecked")
    @Override
    public  T parse(byte[] data, Class clazz) {
        if (clazz == Request.class) {
            Request req = requestSchema.newMessage();
            ProtostuffIOUtil.mergeFrom(data, req, requestSchema);
            return (T) req;
        } else if (clazz == Response.class) {
            Response resp = responseSchema.newMessage();
            ProtostuffIOUtil.mergeFrom(data, resp, responseSchema);
            return (T) resp;
        } else {
            throw new IllegalArgumentException("The meta info type must be Request or Response");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy