说明
io.netty.buffer.ByteBuf的函数isDirect()可以判断该ByteBuf底层是否被NIO direct buffer支撑。如果结果返回true,表示底层被NIO direct buffer支撑。文章来源:https://www.toymoban.com/news/detail-645993.html
示例
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) {
// 创建一个ByteBuf
ByteBuf buf = Unpooled.buffer();
// 查看buf的具体类名
System.out.println("buf.getClass().getName(): " + buf.getClass().getName());
// 判断是否direct
System.out.println("buf.isDirect: " + buf.isDirect());
System.out.println();
// 另外一种分配ByteBuf的方法
ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer();
System.out.println("buf2.getClass().getName(): " + buf2.getClass().getName());
System.out.println("buf2.isDirect: " + buf2.isDirect());
}
}
运行输出:文章来源地址https://www.toymoban.com/news/detail-645993.html
buf.getClass().getName(): io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf
buf.isDirect: false
buf2.getClass().getName(): io.netty.buffer.PooledUnsafeDirectByteBuf
buf2.isDirect: true
到了这里,关于Netty:判断ByteBuf底层是否被NIO direct buffer支撑的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!