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

org.redkalex.convert.protobuf.ProtobufObjectDecoder Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.redkalex.convert.protobuf;

import java.lang.reflect.Type;
import org.redkale.convert.*;
import org.redkale.util.Attribute;

/**
 *
 * @author zhangjx
 * @param  T
 */
public class ProtobufObjectDecoder extends ObjectDecoder {

    protected ProtobufObjectDecoder(Type type) {
        super(type);
    }

    @Override
    protected boolean hasNext(ProtobufReader in, boolean first) {
        return in.hasNext();
    }

    @Override
    protected void initForEachDeMember(ConvertFactory factory, DeMember member) {
        Attribute attr = member.getAttribute();
        setTag(member, ProtobufFactory.getTag(attr.field(), attr.genericType(), member.getPosition(), ((ProtobufFactory) factory).enumtostring));
    }

    @Override
    protected ProtobufReader objectReader(ProtobufReader in) {
        if (in.position() > in.initoffset) return new ProtobufReader(in.readByteArray());
        return in;
    }

    @Override
    protected Object readMemberValue(ProtobufReader in, DeMember member, boolean first) {
        Decodeable decoder = member.getDecoder();
        if (decoder instanceof ProtobufArrayDecoder) {
            return ((ProtobufArrayDecoder) decoder).convertFrom(in, member);
        } else if (decoder instanceof ProtobufCollectionDecoder) {
            return ((ProtobufCollectionDecoder) decoder).convertFrom(in, member);
        } else if (decoder instanceof ProtobufStreamDecoder) {
            return ((ProtobufStreamDecoder) decoder).convertFrom(in, member);
        } else if (decoder instanceof ProtobufMapDecoder) {
            return ((ProtobufMapDecoder) decoder).convertFrom(in, member);
        } else {
            return member.read(in);
        }
    }

    @Override
    protected void readMemberValue(ProtobufReader in, DeMember member, T result, boolean first) {
        Decodeable decoder = member.getDecoder();
        if (decoder instanceof ProtobufArrayDecoder) {
            member.getAttribute().set(result, ((ProtobufArrayDecoder) decoder).convertFrom(in, member));
        } else if (decoder instanceof ProtobufCollectionDecoder) {
            member.getAttribute().set(result, ((ProtobufCollectionDecoder) decoder).convertFrom(in, member));
        } else if (decoder instanceof ProtobufStreamDecoder) {
            member.getAttribute().set(result, ((ProtobufStreamDecoder) decoder).convertFrom(in, member));
        } else if (decoder instanceof ProtobufMapDecoder) {
            member.getAttribute().set(result, ((ProtobufMapDecoder) decoder).convertFrom(in, member));
        } else {
            member.read(in, result);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy