【ChannelPromise作用:可以设置success或failure 是为了通知ChannelFutureListener】
Netty的数据处理API通过两个组件暴露——abstract class ByteBuf和interface ByteBufHolder。

下面是一些ByteBuf API的优点:
  它可以被用户自定义的缓冲区类型扩展;
  通过内置的复合缓冲区类型实现了透明的零拷贝;
  容量可以按需增长(类似于JDK的StringBuilder);
  在读和写这两种模式之间切换不需要调用ByteBuffer的flip()方法;
  读和写使用了不同的索引;
  支持方法的链式调用;
  支持引用计数;
  支持池化。
  使用不同的读索引和写索引来控制数据访问;
  readerIndex达到和writerIndex

  使用内存的不同方式——基于字节数组和直接缓冲区;
  通过CompositeByteBuf生成多个ByteBuf的聚合视图;
  数据访问方法——搜索、切片以及复制;
  随机访问索引 【0到capacity() - 1】
  顺序访问索引
  可丢弃字节 【discardReadBytes() clear()改变index值】
  可读字节【readBytes(ByteBuf dest) 】
  可写字节【writeBytes(ByteBuf dest);】
  索引管理【markReaderIndex()、markWriterIndex()、resetWriterIndex()和resetReaderIndex( readerIndex(int)或者writerIndex(int) 】
  查找操作【buf.indexOf(),forEachByte(ByteBufProcessor.FIND_NUL), int nullIndex = buf.forEachByte(ByteBufProcessor.FIND_NUL);int rIndex = buf.forEachByte(ByteBufProcessor.FIND_CR);】
  派生缓冲区【 返回新的buf,都具有 readIndex writeIndex markIndex
  buf.duplicate();
  ByteBuf rep = buf.copy();//创建副本
  buf.slice();//操作buf分段
  buf.slice(0, 5);//操作buf分段】
  读、写、获取和设置API;
  读/写操作【get()和set()操作,从给定的索引开始,并且保持索引不变;read()和write()操作,从给定的索引开始,并且会根据已经访问过的字节数对索引进行调整】
  ByteBufAllocator池化
  ByteBufAllocator pool = new PooledByteBufAllocator();//提高性能减少碎片,高效分配算法
  ByteBufAllocator unpool = new UnpooledByteBufAllocator(true);//一直新建
  引用计数
  ByteBufAllocator allocator = ctx.channel().alloc();
  ByteBuf directBuf = allocator.directBuffer();
  if(directBuf.refCnt() == 1){//当引用技术为1时释放对象
  directBuf.release();
  }

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws InterruptedException {
logger.info("channelRead start");
ByteBuf buf = (ByteBuf) msg;
if (buf.hasArray()) {//检查buf是否支持一个数组
byte[] array = buf.array();
//第一个偏移量
int off = buf.arrayOffset() + buf.readerIndex();
//获取可读取字节
int len = buf.readableBytes();
byte[] buffer = new byte[len];
buf.getBytes(off, buffer);
CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
ByteBuf header = (ByteBuf) msg;
ByteBuf body = (ByteBuf) msg;
compositeByteBuf.addComponent(header);
compositeByteBuf.addComponent(body);
compositeByteBuf.removeComponent(0);
for (ByteBuf bufer : compositeByteBuf) {
System.out.println(bufer.toString());
}
int comLen = compositeByteBuf.readableBytes(); for (int i = 0; i < buf.capacity(); i++) {
System.out.println((char) buf.getByte(i));
//读完成后进行丢弃
buf.discardReadBytes();
//或者调用clear
buf.clear();
}
//标记索引
buf.readerIndex(2);
buf.writeByte(2);
//重置索引
buf.markReaderIndex();
buf.markWriterIndex();
buf.resetReaderIndex();
buf.resetWriterIndex();
//查找
buf.indexOf(0, 5, (byte) 0);
int nullIndex = buf.forEachByte(ByteBufProcessor.FIND_NUL);
int rIndex = buf.forEachByte(ByteBufProcessor.FIND_CR);
//派生缓冲区 返回新的buf,都具有 readIndex writeIndex markIndex
buf.duplicate();
ByteBuf rep = buf.copy();//创建副本
buf.slice();//操作buf分段
buf.slice(0, 5);//操作buf分段
//==========数据访问方法——搜索、切片以及复制;
Charset charset = Charset.forName("UTF-8");
ByteBuf buf1 = Unpooled.copiedBuffer("Netty in Action rocks!", charset); //← -- 创建一个用于保存给定字符串的字节的ByteBuf
ByteBuf sliced = buf1.slice(0, 15); //← -- 创建该ByteBuf 从索引0 开始到索引15结束的一个新切片
System.out.println(sliced.toString(charset)); // ← -- 将打印“Netty in Action”
buf1.setByte(0, (byte) 'J'); //← -- 更新索引0 处的字节
assert buf1.getByte(0) == sliced.getByte(0); //← -- 将会成功,因为数据是共享的,对其中一个所做的更改对另外一个也是可见的 Charset utf8 = Charset.forName("UTF-8");
ByteBuf buf2 = Unpooled.copiedBuffer("Netty in Action rocks!", utf8); // ← -- 创建ByteBuf 以保存所提供的字符串的字节
ByteBuf copy = buf2.copy(0, 15);// ← -- 创建该ByteBuf 从索引0 开始到索引15结束的分段的副本
System.out.println(copy.toString(utf8));//  ← -- 将打印“Netty in Action”
buf2.setByte(0, (byte) 'J');//  ← -- 更新索引0 处的字节
assert buf2.getByte(0) != copy.getByte(0);// ← -- 将会成功,因为数据不是共享的 Unpooled.unmodifiableBuffer(buf);
buf.order();
buf.readSlice(1);
//使用不同的读索引和写索引来控制数据访问;
//读写操作 get/set不改变索引位置 read/write改变索引(readIndex/writeIndex)位置 Charset u8 = Charset.forName("UTF-8");
ByteBuf getSetBuf = Unpooled.copiedBuffer("Netty in Action rocks!", u8); // 创建一个新的ByteBuf以保存给定字符串的字节
System.out.println((char) getSetBuf.getByte(0));// 打印第一个字符'N'
int readerIndex = getSetBuf.readerIndex(); // 存储当前的readerIndex 和writerIndex
int writerIndex = getSetBuf.writerIndex();
getSetBuf.setByte(0, (byte) 'B'); // 将索引0 处的字节更新为字符'B'
System.out.println((char) getSetBuf.getByte(0)); // 打印第一个字符,现在是'B' 
assert readerIndex == getSetBuf.readerIndex();// 将会成功,因为这些操作并不会修改相应的索引
assert writerIndex == getSetBuf.writerIndex(); ByteBuf readWriteBuf = Unpooled.copiedBuffer("Netty in Action rocks!", u8); // 创建一个新的ByteBuf以保存给定字符串的字节
System.out.println((char) readWriteBuf.readByte());// 打印第一个字符'N'
System.out.println((boolean) readWriteBuf.readBoolean());// 读取当前boolean值,并将readIndex+1
readWriteBuf.writeByte('F');// 将字符F追加到缓冲区中,并将writeIndex+1
int reIndex = readWriteBuf.readerIndex(); // 存储当前的readerIndex 和writerIndex
int wrIndex = readWriteBuf.writerIndex();
readWriteBuf.setByte(0, (byte) 'B'); // 将索引0 处的字节更新为字符'B'
System.out.println((char) readWriteBuf.getByte(0)); // 打印第一个字符,现在是'B' 
assert reIndex == readWriteBuf.readerIndex();// 将会成功,因为这些操作并不会修改相应的索引
assert wrIndex == readWriteBuf.writerIndex(); buf.isReadable();//至少有一个字符可读,返回true
buf.isWritable();//至少有一个字节可被写入,返回true
int readableByte = buf.readableBytes();//返回可被读取的字节数
int writableByte = buf.writableBytes();//返回可被写入的字节数
int capacity = buf.capacity();//返回可容纳的字节数
buf.maxCapacity();//返回可容纳的最大字节数
buf.hasArray();//如果buf由一个字节数组支撑,返回true
buf.array();//将buf转换为字节数组 //除了数据外,还有一些其他的属性,如http的状态码,cookie等
ByteBufHolder byteBufHolder = new DefaultLastHttpContent();
ByteBuf httpContent = byteBufHolder.content();//返回一个http格式的ByteBuf
ByteBufHolder copyBufHolder = byteBufHolder.copy();//深拷贝,不共享
ByteBufHolder duplicateBufHolder = byteBufHolder.duplicate();//浅拷贝,共享 //ByteBufAllocator ByteBuf分配
// buffer()基于堆或直接内存的buf
//ioBuffer() 返回一个iobuf
//heapBuffer 堆buf
//directBuffer 直接buf
//compositeBuffer compositeHeapBuffer compositeDirectBuffer 复合buf
ByteBufAllocator ctxAllocator = ctx.alloc();
ByteBufAllocator channelAllocator = ctx.channel().alloc();
ctxAllocator.buffer();
ctxAllocator.ioBuffer();
ctxAllocator.compositeBuffer();
ctxAllocator.heapBuffer();
ctxAllocator.directBuffer(); ByteBufAllocator pool = new PooledByteBufAllocator();//提高性能减少碎片,高效分配算法
ByteBufAllocator unpool = new UnpooledByteBufAllocator(true);//一直新建 ctx.writeAndFlush(new byte[10]);
ctx.writeAndFlush(Unpooled.copiedBuffer(new byte[10]));//writeAndFlush参数是Object,使用非池化技术转为buf提升效率
//工具类ByteBufUtil
ByteBufUtil.hexDump(buf);//可对buf进行转换
ByteBufUtil.hexDump(new byte[9999]);//可对字节进行转换
//引用计数:跟踪特定对象的引用计数
ByteBufAllocator allocator = ctx.channel().alloc();
ByteBuf directBuf = allocator.directBuffer();
if(directBuf.refCnt() == 1){//当引用技术为1时释放对象
directBuf.release();
} if (buf.readableBytes() <= 0) {
ReferenceCountUtil.safeRelease(msg);
return;
}
byte[] msgContent = new byte[buf.readableBytes()];
buf.readBytes(msgContent);
logger.info("recv from client:length: {},toHexString: {}\n", buf.readableBytes(), HexStringUtils.toHexString(buf.array()));
if (buf.getByte(0) == 0x7e && buf.getByte(buf.readableBytes() - 1) == 0x7e) {}
if(msgContent[0] == 0x7e && msgContent[msgContent.length-1]==0x7e){}
} } //通常如果集成ChannelInboundHandlerAdapter时,复写channelRead(),需要进行手动的消息释放 //消息消费完成后自动释放
private void release(Object msg) {
try {
ReferenceCountUtil.release(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
=================================================== //在SimpleChannelInboundHandler中的channelRead0()方法中自动添加了释放消息的方法
//SimpleChannelInboundHandler<I> extends ChannelInboundHandlerAdapter
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
boolean release = true;
try {
if (acceptInboundMessage(msg)) {
@SuppressWarnings("unchecked")
I imsg = (I) msg;
channelRead0(ctx, imsg);
} else {
release = false;
ctx.fireChannelRead(msg);
}
} finally {
if (autoRelease && release) {
ReferenceCountUtil.release(msg);
}
}
}

  

netty之ByteBuf详解的更多相关文章

  1. netty系列之:netty中的ByteBuf详解

    目录 简介 ByteBuf详解 创建一个Buff 随机访问Buff 序列读写 搜索 其他衍生buffer方法 和现有JDK类型的转换 总结 简介 netty中用于进行信息承载和交流的类叫做ByteBu ...

  2. BAT面试必问细节:关于Netty中的ByteBuf详解

    在Netty中,还有另外一个比较常见的对象ByteBuf,它其实等同于Java Nio中的ByteBuffer,但是ByteBuf对Nio中的ByteBuffer的功能做了很作增强,下面我们来简单了解 ...

  3. Netty学习摘记 —— ByteBuf详解

    本文参考 本篇文章是对<Netty In Action>一书第五章"ByteBuf"的学习摘记,主要内容为JDK 的ByteBuffer替代品ByteBuf的优越性 你 ...

  4. 1、Netty 实战入门详解

    一.Netty 简介 Netty 是基于 Java NIO 的异步事件驱动的网络应用框架,使用 Netty 可以快速开发网络应用,Netty 提供了高层次的抽象来简化 TCP 和 UDP 服务器的编程 ...

  5. Netty实战入门详解——让你彻底记住什么是Netty(看不懂你来找我)

    一.Netty 简介 Netty 是基于 Java NIO 的异步事件驱动的网络应用框架,使用 Netty 可以快速开发网络应用,Netty 提供了高层次的抽象来简化 TCP 和 UDP 服务器的编程 ...

  6. [转帖]技术扫盲:新一代基于UDP的低延时网络传输层协议——QUIC详解

    技术扫盲:新一代基于UDP的低延时网络传输层协议——QUIC详解    http://www.52im.net/thread-1309-1-1.html   本文来自腾讯资深研发工程师罗成的技术分享, ...

  7. Java网络编程和NIO详解9:基于NIO的网络编程框架Netty

    Java网络编程和NIO详解9:基于NIO的网络编程框架Netty 转自https://sylvanassun.github.io/2017/11/30/2017-11-30-netty_introd ...

  8. netty系列之:netty中的Channel详解

    目录 简介 Channel详解 异步IO和ChannelFuture Channel的层级结构 释放资源 事件处理 总结 简介 Channel是连接ByteBuf和Event的桥梁,netty中的Ch ...

  9. Netty 中文教程 Hello World !详解

    1.HelloServer 详解 HelloServer首先定义了一个静态终态的变量---服务端绑定端口7878.至于为什么是这个7878端口,纯粹是笔者个人喜好.大家可以按照自己的习惯选择端口.当然 ...

随机推荐

  1. 如何让git忽略指定的文件

    有些文件,我们修改后,并不需要git提交更改,可以在.gitignore里面设置过滤规则 在.gitignore文件里面输入 *.zip 表示所有zip文件忽略更改 /bin 表示忽略整个根目录的bi ...

  2. 《LeetCode-0004》 寻找两个有序数组的中位数-Median of Two Sorted Arrays

    题目给定两个大小为 m 和 n 的有序数组nums1和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  3. [API 开发管理] EOLINKER 升级为多产品架构, AMS V4.5 版本常见问题汇总

    自AMS4.5开始,eoLinker 全面升级为多产品架构,部分操作方式较以前有较大改变,本文针对改进部分做重点说明. 在说明之前,我们先通过以下的图文看看AMSV4.5更新了哪些内容: Q:我可以创 ...

  4. js 阻止冒泡事件和默认事件

    阻止事件冒泡 window.enent ? window.enent.cancelBubble = true : e.stopPropagation() function stopBubble(eve ...

  5. [C++] muParser 的简单使用方法

    关于 muParser 库 许多应用程序需要解析数学表达式.该库的主要目的是提供一种快速简便的方法. muParser是一个用C ++编写的可扩展的高性能数学表达式解析器库. 它的工作原理是将数学表达 ...

  6. Git:分支的创建、合并、管理和删除

    了解分支 如果想实现多人协作.划出Bug区.Feature区等功能,就需要分支功能.(确实很强大的地方) 每次commit时,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条 ...

  7. Dijkstra算法求最短路径

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h&g ...

  8. 暑假集训D18总结

    考试 本来考试时以为能AK的,结果全是因为手贱啊= = T1 瞎XX贪心 我竟然当成了数学 还拍了半天以为是对的 T2 组合数学 太简单 半个小时直接A T3 最长上升(非下降?)子序列 考试25,加 ...

  9. Grails里DOMAIN类的一对一,一对多,多对多关系总结及集成测试

    终于干完这一章节,收获很多啊. 和DJANGO有类似,也有不同. User.groovy: package com.grailsinaction class User { String loginId ...

  10. Q - Period II

    For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...