字节操作
ByteBuf buffer = Unpooled.buffer(10);
ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!", Charset.forName("utf-8"));
int len = byteBuf.readableBytes();
//可读的字节数12
System.out.println("len=" + len);
//使用for取出各个字节
for (int i = 0;
i < len;
i++) {
System.out.println((char) byteBuf.getByte(i));
// 强行转为字符串,否则会直接打印ASCII码
}//按照某个范围读取
System.out.println(byteBuf.getCharSequence(0, 4, Charset.forName("utf-8")));
System.out.println(byteBuf.getCharSequence(4, 6, Charset.forName("utf-8")));
ByteBuf byteBuf1 = Unpooled.copiedBuffer(byteBuf);
ByteBuf转字节数组
byte[] bytes = ByteBufUtil.getBytes(byteBuf1);
字节读取
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
byte[] newBytes = new byte[bytes.length];
while (inputStream.read(newBytes)!=-1){
inputStream.close();
System.out.println(new String(newBytes));
}ByteArrayInputStream inputStream1 = new ByteArrayInputStream(bytes,2,5);
byte[] newBytes1 = new byte[bytes.length];
while (inputStream1.read(newBytes1)!=-1){
inputStream1.close();
System.out.println(new String(newBytes1));
}
推荐阅读
- java|使用虹软SDK实现离线人脸识别(局域网)
- java|前端小游戏飞机大战源码完整版
- 程序员|java二叉树中序遍历非递归,值得一看
- 尝鲜(阿里前端低代码引擎LowCodeEngine开源啦!)
- 大数据|Docker员工自述(我们为什么“输”给了K8s())
- kubernetes|K8s 系列(五) - 浅谈 CSI
- 操作系统|后台开发应该读的书
- 数据可视化|3【源码】数据可视化(基于 Echarts +Java SpringBoot 实现的动态实时大屏范例-物联网)
- Java|Java1.6下载地址