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

com.yhzdys.myosotis.serialize.ProtoStuffSerializer Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package com.yhzdys.myosotis.serialize;

import com.yhzdys.myosotis.entity.MyosotisConfig;
import com.yhzdys.myosotis.entity.MyosotisEvent;
import com.yhzdys.myosotis.entity.PollingData;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;

public final class ProtoStuffSerializer implements Serializer {

    private final Schema polling_schema;
    private final Schema event_schema;
    private final Schema config_schema;

    public ProtoStuffSerializer() {
        this.polling_schema = RuntimeSchema.getSchema(PollingData.class);
        this.event_schema = RuntimeSchema.getSchema(MyosotisEvent.class);
        this.config_schema = RuntimeSchema.getSchema(MyosotisConfig.class);
    }

    @Override
    public byte[] serializePollingData(List list) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ProtobufIOUtil.writeListTo(outputStream, list, polling_schema, LinkedBuffer.allocate());
        return outputStream.toByteArray();
    }

    @Override
    public List deserializePollingData(byte[] data) throws Exception {
        return ProtobufIOUtil.parseListFrom(new ByteArrayInputStream(data), polling_schema);
    }

    @Override
    public byte[] serializeEvents(List list) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ProtobufIOUtil.writeListTo(outputStream, list, event_schema, LinkedBuffer.allocate());
        return outputStream.toByteArray();
    }

    @Override
    public List deserializeEvents(byte[] data) throws Exception {
        return ProtobufIOUtil.parseListFrom(new ByteArrayInputStream(data), event_schema);
    }

    @Override
    public byte[] serializeConfigs(List list) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ProtobufIOUtil.writeListTo(outputStream, list, config_schema, LinkedBuffer.allocate());
        return outputStream.toByteArray();
    }

    @Override
    public List deserializeConfigs(byte[] data) throws Exception {
        return ProtobufIOUtil.parseListFrom(new ByteArrayInputStream(data), config_schema);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy