io.netty.buffer.ByteBuf是一个抽象类,我们看看它最终的实现类。实现类有多个,具体用的是哪个实现类,跟分配ByteBuf的方式有关。
作为举例,分别用Unpooled和ByteBufAllocator.DEFAUL来分配一个ByteBuf。
package com.thb;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
public class Test {
public static void main(String[] args) {
// 用Unpooled创建一个ByteBuf
ByteBuf buf = Unpooled.buffer();
System.out.println(buf.getClass().getName());
// 用ByteBufAllocator.DEFAULT创建一个ByteBuf
ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer();
System.out.println(buf2.getClass().getName());
}
}
运行输出:
io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf
io.netty.buffer.PooledUnsafeDirectByteBuf