关于java:MyByteUtils

49次阅读

共计 1157 个字符,预计需要花费 3 分钟才能阅读完成。


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.nio.charset.StandardCharsets;

import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;

public class MyByteUtils {public static void log(ByteBuf buffer) {int length = buffer.readableBytes();
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder buf = new StringBuilder(rows * 80 * 2)
                .append("read index:").append(buffer.readerIndex())
                .append("write index:").append(buffer.writerIndex())
                .append("capacity:").append(buffer.capacity())
                .append(NEWLINE);
        appendPrettyHexDump(buf, buffer);
        System.out.println(buf.toString());
    }

    public static ByteBuf createByteBuf(String userName,String passWord) {String format = String.format("%s\u0000%s", userName, passWord);

        ByteBuf byteBuf1 = ByteBufAllocator.DEFAULT.buffer(16 + format.length());

        byteBuf1.writeBoolean(true);

        byteBuf1.writeInt(5);

        byteBuf1.writeBytes("PLAIN".getBytes(StandardCharsets.UTF_8));

        byteBuf1.writeByte(5);

        byteBuf1.writeInt(format.length() + 1);

        byteBuf1.writeBytes("\u0000".getBytes(StandardCharsets.UTF_8));

        byteBuf1.writeBytes(format.getBytes(StandardCharsets.UTF_8));

        MyByteUtils.log(byteBuf1);

        return byteBuf1;
    }
}

正文完
 0