netty(9)ByteBuf的操作

ByteBuf的说明

http://www.cnblogs.com/wade-luffy/p/6196481.html#_label0_0

https://segmentfault.com/a/1190000013523875

https://blog.csdn.net/dfdsggdgg/article/details/51533287

https://blog.csdn.net/u010853261/article/details/53690780

http://netty.io/wiki/reference-counted-objects.html

1.读取ByteBuf之后的转换

在channelRead(ChannelHandlerContext ctx,Object msg)函数中,

a.转换成String

ByteBuf in=(ByteBuf)msg;

String conv=in.toString(CharsetUtil.US_ASCII);

说明:ByteBuf 中toString的作用:

Decodes this buffer's readable bytes into a string with the specified
* character set name. US_ASCII,UTF_8等值

 等效于buf.toString(buf.readerIndex(), buf.readableBytes(), charsetName)

 b转成byte[]

ByteBuf buf = ...
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);

如果不想改变readerIndex的值
ByteBuf buf = ...
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);

,如果要最小化拷贝,可以使用backing array

ByteBuf buf = ...
byte[] bytes;
int offset;
int length = buf.readableBytes();

if (buf.hasArray()) {
    bytes = buf.array();
    offset = buf.arrayOffset();
} else {
    bytes = new byte[length];
    buf.getBytes(buf.readerIndex(), bytes);
    offset = 0;
}

backing array

2.封装其他数据,转换成ByteBuf后发送

byte[] responseByteArray = "你好".getBytes("UTF-8");
ByteBuf out = ctx.alloc().buffer(responseByteArray.length);

3.自己写代码测试ByteBuf各种生成,性能函数

测试ByteBuf的代码

public void testByteBuf(ByteBuf input) {
        System.out.println("the input ByteBuf info: readerIndex is: "+input.readerIndex()+"  write index is: "
                + input.writerIndex()+" 
capacity is:"+input.capacity()
                + " maxCapacity is: "+input.maxCapacity()+" isDirect: "+input.isDirect()
                        + ""
                        + "");
        System.out.println(ByteBufUtil.hexDump(input));
    }

测试Unpooled.copiedBuffer系列函数

public void makeByteBuf(ByteBuf input) {
        //input is an inbound msg
        Charset utf8=Charset.forName("UTF-8");
        //Unpooled copiedBuffer from String and byte array
        ByteBuf upcb1=Unpooled.copiedBuffer("the",utf8);
        byte [] ba=new byte[24];
        ba[0]=(byte)'b';ba[1]=(byte)'i';ba[22]=(byte)'a';ba[23]=(byte)'d';
        ByteBuf upcb2=Unpooled.copiedBuffer(ba);
        ByteBuf upcb3=Unpooled.copiedBuffer(input,upcb1);
        testByteBuf(upcb3);
        /*copiedBuffer返回的对象都是Heap中的,不是direct.
    WriteIndex和Capacity都会变成原来对象字节的长度,不可再写入了。
  可以使用ByteBuf中的readerIndex(int)和writerIndex(int)来重新设置
这2个index
*/ }

 ByteBufAllocator生成Bytebuf

//在channelRead(ChannelHandlerContext ctx, Object msg)函数中
//调用makeByteBuf,传入ctx,这里生成directBuffer,
//将客户端写入的56789,的前三个写入buf对象中
public void makeByteBuf(ChannelHandlerContext ctx,ByteBuf input) {
            ByteBufAllocator a1=ctx.alloc();
            ByteBuf buf=a1.directBuffer(2048);
            buf.writeBytes(input, 0,3);
            testByteBuf(buf);
    }
/*测试数据结果
the input ByteBuf info: readerIndex is: 0  write index is: 3 
capacity is:2048 maxCapacity is: 2147483647 isDirect: true hasArray: false
353637
567
Client said:56789

*/
/*
获取ByteBufAllocator对象有两种方法
1.使用channel.alloc();
2.使用ctx.alloc();
ByteBufAllocator分为PooledByteAllocator和UnpooledByteBufAllocator
使用ChannelConfig来设置
*/
原文地址:https://www.cnblogs.com/legion/p/8717466.html