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

com.jn.agileway.codec.serialization.protostuff.ProtostuffCodec Maven / Gradle / Ivy

package com.jn.agileway.codec.serialization.protostuff;

import com.jn.agileway.codec.AbstractCodec;
import com.jn.agileway.codec.CodecException;
import com.jn.langx.util.reflect.Reflects;

public class ProtostuffCodec extends AbstractCodec {

    public ProtostuffCodec() {

    }

    public ProtostuffCodec(Class targetType) {
        setTargetType(targetType);
    }

    @Override
    public byte[] encode(T obj) throws CodecException {
        try {
            return Protostuffs.serialize(obj);
        } catch (Throwable ex) {
            throw new CodecException(ex.getMessage(), ex);
        }
    }

    @Override
    public T decode(byte[] bytes) throws CodecException {
        return decode(bytes, (Class) getTargetType());
    }

    @Override
    public T decode(byte[] bytes, Class targetType) throws CodecException {
        try {
            return Protostuffs.deserialize(bytes, targetType);
        } catch (Throwable ex) {
            throw new CodecException(ex.getMessage(), ex);
        }
    }

    @Override
    public boolean canSerialize(Class type) {
        return type != null && type != Object.class && Reflects.isSubClassOrEquals(type, getTargetType());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy