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

org.bdware.doipv3.V3MessageTool Maven / Gradle / Ivy

The newest version!
package org.bdware.doipv3;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bdware.doip.codec.MessageEnvelopeCodec;
import org.bdware.doip.codec.NaiveEnvelopeToDoMessage;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.MessageEnvelope;
import org.bdware.doip.codec.exception.MessageCodecException;
import org.bdware.doip.codec.v3.headers.EnvelopeWrapper;

import java.util.ArrayList;
import java.util.List;

public class V3MessageTool {
    public static EnvelopeWrapper wrapDoipMessage(DoipMessage message) throws MessageCodecException {
        List envelopes = new ArrayList<>();
        NaiveEnvelopeToDoMessage.messageToEnvelopes(message, envelopes);
        MessageEnvelope envelopeResponse = (MessageEnvelope) envelopes.get(0);
        ByteBuf buf = Unpooled.directBuffer();
        MessageEnvelopeCodec.envelopeToBytes(envelopeResponse, buf);
        EnvelopeWrapper wrapper = new EnvelopeWrapper(buf);
        return wrapper;
    }

    public static DoipMessage extract(EnvelopeWrapper wrapper) throws MessageCodecException {
        MessageEnvelope envelope = new MessageEnvelope();
        MessageEnvelopeCodec.byteBufToEnvelope(wrapper.messageEnvelopeByteBuf, envelope);
        DoipMessage result = NaiveEnvelopeToDoMessage.byteBufToMessage(envelope.content, envelope.requestId);
        return result;
    }
}