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

net.dongliu.prettypb.runtime.ProtoBufEncoder Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.runtime;

import net.dongliu.prettypb.runtime.code.ProtoFieldInfo;
import net.dongliu.prettypb.runtime.code.ProtoInfo;
import net.dongliu.prettypb.runtime.code.ProtoBufWriter;
import net.dongliu.prettypb.runtime.exception.IllegalBeanException;
import net.dongliu.prettypb.runtime.exception.IllegalDataException;
import net.dongliu.prettypb.runtime.exception.MissRequiredFieldException;
import net.dongliu.prettypb.runtime.exception.ProtoSerializeException;
import net.dongliu.prettypb.runtime.include.Extendable;
import net.dongliu.prettypb.runtime.include.ExtensionField;
import net.dongliu.prettypb.runtime.utils.BeanParser;
import net.dongliu.prettypb.runtime.utils.ExtensionFieldUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

/**
 * proto encoder
 *
 * @author Dong Liu
 */
public class ProtoBufEncoder {
    ProtoBufWriter protoBufWriter;

    private ProtoBufEncoder(OutputStream os) {
        this.protoBufWriter = new ProtoBufWriter(os);
    }

    /**
     * serialize proto value into bytes
     */
    public static  byte[] toBytes(T value, Class clazz) {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream(256)) {
            toStream(value, clazz, os);
            return os.toByteArray();
        } catch (IOException e) {
            //should not happen
            throw new RuntimeException("", e);
        }
    }


    /**
     * serialize protobuf value into outputStream
     */
    public static  void toStream(T value, Class clazz, OutputStream os)
            throws IOException {
        if (value == null) {
            throw new NullPointerException("Value cannot be null.");
        }
        ProtoBufEncoder protoBufEncoder = new ProtoBufEncoder(os);
        protoBufEncoder.writeMessage(value, clazz);
    }

    private  void writeMessage(T value, Class clazz) throws IOException {
        ProtoInfo protoInfo;
        try {
            protoInfo = BeanParser.getProtoInfo(clazz);
        } catch (IllegalBeanException e) {
            throw new ProtoSerializeException("Value serialize failed:" + clazz.getName(), e);
        }

        // serialize fields
        for (ProtoFieldInfo protoFieldInfo : protoInfo.getProtoFieldInfos()) {
            try {
                protoFieldInfo.serializeField(value, protoBufWriter);
            } catch (IllegalBeanException | IllegalAccessException | MissRequiredFieldException
                    | IllegalDataException | RuntimeException e) {
                throw new ProtoSerializeException("Serialize failed for bean:" + clazz.getName()
                        + ", field:" + protoFieldInfo.getName(), e);
            }
        }

        // serialize extension fields
        if (value instanceof Extendable) {
            Extendable extendable = (Extendable) value;
            Map extensions = extendable.getAllExtensions();

            for (Map.Entry entry : extensions.entrySet()) {
                ExtensionField extensionField = entry.getKey();
                Object v = entry.getValue();
                // just ignore null values
                if (v == null) {
                    continue;
                }
                ExtensionFieldUtils.writeField(extensionField, v, protoBufWriter);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy