Netty:判断ByteBuf底层是否被NIO direct buffer支撑

说明

io.netty.buffer.ByteBuf的函数isDirect()可以判断该ByteBuf底层是否被NIO direct buffer支撑。如果结果返回true,表示底层被NIO direct buffer支撑。

示例

复制代码
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());
	}
}

运行输出:

复制代码
buf.getClass().getName(): io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf
buf.isDirect: false

buf2.getClass().getName(): io.netty.buffer.PooledUnsafeDirectByteBuf
buf2.isDirect: true
相关推荐
爆更小哇10 分钟前
Selenium自动化测试函数全解析(二)
java·selenium·测试工具·自动化
C雨后彩虹10 分钟前
计算误码率
java·数据结构·算法·华为·面试
fanruitian14 分钟前
Springboot项目父子工程
java·数据库·spring boot
小肖爱笑不爱笑18 分钟前
SpringBoot Web
java·http协议·分层解耦·web后端
柒.梧.21 分钟前
Spring核心知识全解析:从入门实战到进阶
java·后端·spring
乌日尼乐24 分钟前
【Java基础整理】Java字符串处理,String、StringBuffer、StringBuilder
java·后端
全栈独立开发者31 分钟前
点餐系统装上了“DeepSeek大脑”:基于 Spring AI + PgVector 的 RAG 落地指南
java·人工智能·spring
dmonstererer33 分钟前
【k8s设置污点/容忍】
java·容器·kubernetes
super_lzb38 分钟前
mybatis拦截器ParameterHandler详解
java·数据库·spring boot·spring·mybatis
程序之巅39 分钟前
VS code 远程python代码debug
android·java·python