package com.youchain.Netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

public class MessageEncodeHandler extends MessageToByteEncoder<String> {
    // 数据分割符
    String delimiter;

    public MessageEncodeHandler(String delimiter) {
        this.delimiter = delimiter;
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, String message, ByteBuf out) throws Exception {
        out.writeBytes((message + delimiter).getBytes(CharsetUtil.UTF_8));
    }
}

