Netty学习12-缓冲区【2】ByteBuf

来源:互联网 发布:第一次在淘宝购物流程 编辑:程序博客网 时间:2024/06/10 09:04
1 概述

《Netty学习11-缓冲区之JDK ByteBuffer》一文中介绍了JDK的缓冲区ByteBuffer的用法和不足。为了弥补这些不足,Netty提供了自己的缓冲区实现ByteBuf(netty3.X是ChannelBuffer)。



2 实现策略

JDK的ByteBuffer已经提供了基础的能力,Netty的ByteBuf的实现可以有两种策略。

[1] 参考JDK ByteBuffer的实现,增加额外功能,解决原ByteBuffer的问题。
[2] 句话JDK ByteBuffer,通过Facade模式对其进行包装。


3 代码

import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;public class NettyByteBuf {public static void main(String[] args) {// 实例初始化ByteBuf buffer = Unpooled.buffer(20);System.out.println(String.format("#####初始化 capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// 写数据String msg = "xy";buffer.writeBytes(msg.getBytes());System.out.println(String.format("#####写数据 capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// 读数据(只读取一个字节)// byte[] vArray = new Byte[ buffer.writerIndex()];byte[] vArray = new byte[1];buffer.readBytes(vArray);System.out.println("result is " + new String(vArray));System.out.println(String.format("#####读数据 capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// discardReadBytesbuffer.discardReadBytes();System.out.println(String.format("#####discard capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// clearbuffer.clear();System.out.println(String.format("#####clear capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));}}
#####初始化 capacity:20 readerIndex:0 writeIndex:0 readeableBytes:0 writableBytes:20
#####写数据 capacity:20 readerIndex:0 writeIndex:2 readeableBytes:2 writableBytes:18
result is x
#####读数据 capacity:20 readerIndex:1 writeIndex:2 readeableBytes:1 writableBytes:18
#####discard capacity:20 readerIndex:0 writeIndex:1 readeableBytes:1 writableBytes:19
#####clear capacity:20 readerIndex:0 writeIndex:0 readeableBytes:0 writableBytes:20



4 图解

上述过程的图解说明,图片摘自李林峰老师《Netty权威指南》一书。


  

  

 



5 markReaderIndex和resetReaderIndex

import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;public class NettyByteBuf2 {public static void main(String[] args) {// 实例初始化ByteBuf buffer = Unpooled.buffer(20);System.out.println(String.format("#####初始化 capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// 写数据String msg = "xy";buffer.writeBytes(msg.getBytes());System.out.println(String.format("#####写数据 capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// 读数据(只读取一个字节)byte[] vArray = new byte[1];// 标记读索引位置buffer.markReaderIndex();buffer.readBytes(vArray);System.out.println("result is " + new String(vArray));System.out.println(String.format("#####读数据 capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));// 还原读索引位置buffer.resetReaderIndex();System.out.println(String.format("#####resetReaderIndex capacity:%s readerIndex:%s writeIndex:%s readeableBytes:%s writableBytes:%s",buffer.capacity(), buffer.readerIndex(), buffer.writerIndex(),buffer.readableBytes(),buffer.writableBytes()));}}

#####初始化 capacity:20 readerIndex:0 writeIndex:0 readeableBytes:0 writableBytes:20
#####写数据 capacity:20 readerIndex:0 writeIndex:2 readeableBytes:2 writableBytes:18
result is x
#####读数据 capacity:20 readerIndex:1 writeIndex:2 readeableBytes:1 writableBytes:18
#####resetReaderIndex capacity:20 readerIndex:0 writeIndex:2 readeableBytes:2 writableBytes:18


1 0
原创粉丝点击