大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
+-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | | | (CONTENT) | | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity
ByteBuf heapBuffer = Unpooled.buffer();
System.out.println(heapBuffer);
UnpooledHeapByteBuf(ridx: 0, widx: 0, cap: 256)
ByteBuf heapBuffer = Unpooled.buffer(8);
System.out.println("初始化:"+heapBuffer);
heapBuffer.writeBytes("测试测试测试");
System.out.println("写入测试测试测试:"+heapBuffer);
结果:
初始化:UnpooledHeapByteBuf(ridx: 0, widx: 0, cap: 8)
写入测试测试测试:UnpooledHeapByteBuf(ridx: 0, widx: 18, cap: 64)
cap初始化8,增加到64
//1、创建缓冲区
ByteBuf heapBuffer = Unpooled.buffer(8);
//2、写入缓冲区内容
heapBuffer.writeBytes("测试测试测试".getBytes());
//3、创建字节数组
byte[] b = new byte[heapBuffer.readableBytes()];
System.out.println(b[11]);
//4、复制内容到字节数组b
heapBuffer.readBytes(b);
System.out.println(b[11]);
//5、字节数组转字符串
String str = new String(b);
System.out.println(str);
结果:
0
-107
测试测试测试
ByteBuf转ByteBuffer
ByteBuffer bb = heapBuffer.nioBuffer();
ByteBuf的主要类继承关系图
ByteBuf heapBuffer = Unpooled.buffer();
System.out.println(heapBuffer);
ByteBuf directBuffer = Unpooled.directBuffer();
System.out.println(directBuffer);
ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(new byte[128]);
System.out.println(wrappedBuffer);
ByteBuf copiedBuffer = Unpooled.copiedBuffer(new byte[128]);
System.out.println(copiedBuffer);
结果:
UnpooledHeapByteBuf(ridx: 0, widx: 0, cap: 256)
SimpleLeakAwareByteBuf(UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 256))
UnpooledHeapByteBuf(ridx: 0, widx: 128, cap: 128/128)
UnpooledHeapByteBuf(ridx: 0, widx: 128, cap: 128/128)
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/191804.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...