![JAR search and dependency download from the Maven repository](/logo.png)
com.godmao.mqserver.handler.TopicSubscribeCancelMessageEncoderHandler Maven / Gradle / Ivy
package com.godmao.mqserver.handler;
import com.godmao.mqserver.message.TopicSubscribeCancelMessage;
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;
import java.util.Set;
@ChannelHandler.Sharable
public class TopicSubscribeCancelMessageEncoderHandler extends AbstractEncoderHandler {
public static final TopicSubscribeCancelMessageEncoderHandler INSTANCE = new TopicSubscribeCancelMessageEncoderHandler();
public ByteBuf encode(TopicSubscribeCancelMessage msg) {
final Set topics = msg.getTopics();
// 预估缓冲区大小
int estimatedSize = 5; // cmd(1 byte) + isAll(1 byte) + topics.size(4 bytes)
final byte[][] _topics_bytes = new byte[topics.size()][];
int index = 0;
for (String topic : topics) {
final byte[] topic_bytes = topic.getBytes(StandardCharsets.UTF_8);
_topics_bytes[index++] = topic_bytes;
estimatedSize += 4 + topic_bytes.length;
}
final ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(estimatedSize);
byteBuf.writeByte(msg.getCmd());// 1
byteBuf.writeInt(topics.size());// 4
//4
for (final byte[] topic_bytes : _topics_bytes) {
byteBuf.writeInt(topic_bytes.length);
byteBuf.writeBytes(topic_bytes);
}
return byteBuf;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy