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

com.godmao.mqbroker.handler.SingleMessageEncoderHandler Maven / Gradle / Ivy

There is a newer version: 0.2.7.RELEASE
Show newest version
package com.godmao.mqbroker.handler;

import com.godmao.mqbroker.message.SingleMessage;
import com.godmao.netty.handler.AbstractEncoderHandler;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandler;

import java.nio.charset.StandardCharsets;

@ChannelHandler.Sharable
public class SingleMessageEncoderHandler extends AbstractEncoderHandler {

    public static final SingleMessageEncoderHandler INSTANCE = new SingleMessageEncoderHandler();


    public ByteBuf encode(SingleMessage.Response msg) {
        final String topic = msg.getTopic();
        final byte[] topic_bytes = topic.getBytes(StandardCharsets.UTF_8);
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(5 + topic_bytes.length + msg.getData().length);
        byteBuf.writeByte(msg.getCmd());     // 1
        byteBuf.writeInt(topic_bytes.length);// 4
        byteBuf.writeBytes(topic_bytes);
        byteBuf.writeBytes(msg.getData());
        return byteBuf;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy