您的位置:首页 > 其它

5 ByteBuf

2016-11-24 19:37 183 查看
5 ByteBuf

5 ByteBuf

Java NIO的ByteBuffer复杂笨重,Netty提供了更强大的ByteBuf。

5.1 The ByteBuf API

Netty处理数据的API包括两部分,抽象类ByteBuf和接口ByteBufHolder。

5.2 Class ByteBuf——Netty’s data container

5.2.1 How it works



5.2.2 ByteBuf usage patterns

HEAP BUFFERS(堆缓存)

Listing 5.1 Backing array

public static void heapBuffer(ByteBuf heapBuf) {
if (heapBuf.hasArray()) {
byte[] array = heapBuf.array();
int offset = heapBuf.arrayOffset() + heapBuf.readerIndex();
int length = heapBuf.readableBytes();
handleArray(array, offset, length);
}
}


DIRECT BUFFERS(直接缓存)

Listing 5.2 Direct buffer data access

public static void directBuffer(ByteBuf directBuf) {
if (!directBuf.hasArray()) {
int length = directBuf.readableBytes();
byte[] array = new byte[length];
directBuf.getBytes(directBuf.readerIndex(), array);
handleArray(array, 0, length);
}
}


COMPOSITE BUFFERS(复合缓存)

CompositeByteBuf是ByteBuf的子类,它提供了一个虚拟表示的多个缓冲区作为一个单一的,合并后的缓冲区。

5.3 Byte-level operations

5.3.1 随机访问下标

public static void byteBufRelativeAccess(ByteBuf buffer) {
for (int i = 0; i < buffer.capacity(); i++) {
byte b = buffer.getByte(i);
System.out.println((char) b);
}

}


5.3.2 顺序访问下标



5.3.3 可以丢弃的字节

调用discardReadBytes()方法,丢弃已读字节和回收空间。

5.3.4 可以读的字节

/**
* Listing 5.7 读取所有可读的字节
*/
public static void readAllData(ByteBuf buffer) {
while (buffer.isReadable()) {
System.out.println(buffer.readByte());
}
}


5.3.5 可以写的字节

/**
* Listing 5.8 写可以写的字节
*/
public static void write(ByteBuf buffer) {
while (buffer.writableBytes() >= 4) {
buffer.writeInt(random.nextInt());
}
}


5.3.6 下标管理

5.3.7 查询操作

/**
* Listing 5.9 使用ByteBufProcessor查找\r
*/
public static void byteBufProcessor(ByteBuf buffer) {
int index = buffer.forEachByte(ByteBufProcessor.FIND_CR);
}


5.3.8 派生缓存(相当于数据库视图,同一个缓存,不同下标管理)

不同于复制缓存。

/**
* Listing 5.10 使用派生,切割缓存
*/
public static void byteBufSlice() {
Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);
//用给定的字符串创建ByteBuf
ByteBuf sliced = buf.slice(0, 14);
//创建一个下标从0到14的slice
System.out.println(sliced.toString(utf8));
buf.setByte(0, (byte) 'J');
assert buf.getByte(0) == sliced.getByte(0);//成功,因为数据分享
}


/**
* Listing 5.11 复制一个ByteBuf
*/
public static void byteBufCopy() {
Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);     //1
ByteBuf copy = buf.copy(0, 14);                                          //2
System.out.println(copy.toString(utf8));                                 //3
buf.setByte(0, (byte) 'J');                                              //4
assert buf.getByte(0) != copy.getByte(0);
}


5.3.9 读/写操作

两种类型:

*get()和set()方法不改变读写下标

*read()和write()

/**
* Listing 5.12
*/
public static void byteBufSetGet() {
Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);    //1
System.out.println((char)buf.getByte(0));                   //2

int readerIndex = buf.readerIndex();                        //3
int writerIndex = buf.writerIndex();

buf.setByte(0, (byte)'B');                          //4

System.out.println((char)buf.getByte(0));                   //5
assert readerIndex == buf.readerIndex();                    //6
assert writerIndex ==  buf.writerIndex();
}

/**
* Listing 5.13
*/
public static void byteBufWriteRead() {
Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf = Unpooled.copiedBuffer("Netty in Action rocks!", utf8);    //1
System.out.println((char)buf.readByte());                   //2

int readerIndex = buf.readerIndex();                        //3
int writerIndex = buf.writerIndex();                        //4

buf.writeByte((byte)'?');                           //5

assert readerIndex == buf.readerIndex();
assert writerIndex != buf.writerIndex();
}


5.3.10 更多操作

5.4 ByteBufHolder接口

5.5 实例化ByteBuf

学习管理ByteBuf的实例

5.5.1 接口ByteBufAllocator

有很多方法返回ByteBuf实例

buffer()



可以从Channel和ChannleHandlerContext获得ByteBufAllocator实例。

ByteBufAllocator allocator = channel.alloc();

ByteBufAllocator allocator = ctx.alloc();

有两个实现类PooledByteBufAllocator和UnpooledByteBufAllocator。

5.5.2 Unpooled辅助类

提供静态方法创建非池中的ByeBuf实例。

buffer();



5.5.3 ByteBufUtile类

提供静态方法操纵ByteBuf。

hexdump(),打印ByteBuf的十六进制表示。

5.7 总结

*使用读写下标来控制数据访问

*不同的内存使用方法——支持数组和直接缓存

*使用compositebytebuf多bytebufs聚合视图

*数据访问方法:搜索,切片,和复制

*读,写,得到,并设置API

*bytebufallocator池和引用计数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Netty ByteBuf