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

com.zhuang.netty.handler.print.PrintHexHandler Maven / Gradle / Ivy

package com.zhuang.netty.handler.print;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PrintHexHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf buf = (ByteBuf) msg;
        int i = buf.readableBytes();
        byte[] bytes = new byte[i];
        buf.readBytes(bytes);
        log.info(toHex(bytes));
        buf.resetReaderIndex();
        ctx.fireChannelRead(msg);
    }

    public static String toHex(byte[] bytes) {
        StringBuilder hex = new StringBuilder();
        for (byte b : bytes) {
            hex.append(String.format("%02x", b));
        }
        return hex.toString().toUpperCase();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy