《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()));
// discardReadBytes
buffer.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()));
// clear
buffer.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