发布于 2014/06/23 20:58
字数 991
阅读 4230
收藏 0
点赞 0
评论 0

Netty s buffer API has two interfaces: 

  • ByteBuf 

  • ByteBufHolder

ByteBuf - The byte data container

Whenever(每当)  you  need  to  interact(相互作用)  with  a  remote  peer  such  as  a  database,  the  communication needs to be done in bytes. For this and other reasons an efficient(高效), convenient, and easy-to-use data structure  is required, and Netty's ByteBuf implementation  meets these requirements and more, making it an ideal data container, optimized(最佳的) for holding and interacting with bytes. ByteBufis a data container that allows you to add/get bytes from it in an efficient way. To make it easier to operate, it uses two indices: one for reading and one for writing. This allows you to read data out of them in a sequential way and "jump" back to read it again. All you need to do is adjust(调整) the reader index and start the read operation again.

How it works

After  something  is  written  to  the ByteBuf, its writerIndex is  increased  by  the  amount  of bytes written. After you start to read bytes, its readerIndexis increased. You can read bytes until  the writerIndex and readerIndex are  at  the  same position.  The  ByteBuf then becomes  unreadable,  so  the  next  read  request  triggers(触发) an IndexOutOfBoundsException similar to what you ve seen when trying to read beyond the capacity of an array. Calling any of the buffer s methods beginning with "read" or "write" automatically(自动的) advances the  reader  and  writer  indexes  or  you. There  are  also  relative  operations  to set  and   get bytes. These don t move the indexes but operate on the relative(相对的) index that was given. A ByteBuf may  have  a maximum  capacity(容量)  to  set  an  upper  limit(上限)  to  the  maximum  data  it can hold, trying to move the writer index beyond this capacity will result in an exception. The default limit is Integer.MAX_VALUE. 

Figure 5.2 shows how a ByteBufis laid out. 


As  Figure  5.1  shows,  a ByteBuf is  similar  to  a  byte  array,  the  most  notable(显著的)  difference  being the  addition  of  the read  and  write  indices  which  can  be used  to control  access  to  the  buffer's data. You ll  learn  more  about  the  operations  that can  be  performed  on  a ByteBuf in  a  later section. For now, keep this in mind and let s review the different types of ByteBufthat you ll most likely use. 

Different types of ByteBuf

There are three different types of ByteBuf you ll encounter when using Netty (there are more, but these are used internally). You may end up implementing your own, but this is out of scope here. Let s look at the provided types that you are most likely interested in. 


The  most  used type is the ByteBuf that stores  its data  in the heap space(堆内存) of the JVM.  This  is done  by  storing  it  in a  backing  array.  This  type  is  fast  to  allocate  and  also  de-allocate  when you 're  not  using  a  pool.  It  also  offers  a  way  to directly  access  the  backing  array,  which  may make it easier to interact with legacy(遗留的) code . 


Another ByteBuf implementation  is  the   direct  one.Direct  means  that  it  allocates  the memory  directly,  which  is  outside  the heap . You won' t  see  its memory  usage  in  your  heap space. You must take this into account when calculating the maximum amount of memory your application will use and how to limit it, as the max heap size won' t be enough. Direct buffers on the  other side  are  optimal(最佳的,最优的) when  it s  time  to transfer  data  over  a  socket. In  fact,  if  you  use a nondirect  buffer,  the  JVM will  make  a copy  of  your  buffer  to  a  direct  buffer internally(实质上)  before sending it over the socket. The  down  side  of  direct buffers is  that they' re  more expensive  to  allocate(分配)  and  de-allocate compared to heap buffers. This is one of the reasons why Netty supports pooling, which makes this problem disappear. Another possible down side can be that you' re no longer able to access the data  via the  backing  array,  so  you' ll  need  to  make  a copy  of  the  data  if  it  needs  to  work with legacy code that requires this. The following listing shows how you can get the data in an array and call your method even without the ability to access the backing array directly. 


The  last ByteBuf implementation  you  may  be  confronted(面对的)  with  is  the  CompositeByteBuf. This  does  exactly what its  name  says;  it  allows  you  to  compose different ByteBuf instances and provides a view over them. The good thing is you can also add and remove them on-the-fly(在传输过程中), so it s kind of like a List. If you ve ever worked with the JDK's ByteBuffer you' ve most likely missed such a feature there. As the CompositeByteBufis just a view over others, the hasArray() method  will  return false  because  it may  contain  several  ByteBuf instances  of both direct and nondirect types. For  example,  a  message  could  be  composed  of  two parts:  header and  body.  In  a modularized application, the two parts could be produced by different modules and assembled later when  the  message  is  sent  out.  Also,  you may  use  the  same  body  all  the  time  and  just change the header. So it would make sense here to not allocate a new buffer every time. This  would  be  a  perfect fit  for  a CompositeByteBuf as  no  memory  copy  will be  needed and the same API could be used as with non-composite buffers. 

Figure  5.2  shows  how  a CompositeByteBuf would  be  used  to  compose(组成)  the  header  and body.


© 著作权归作者所有

共有 人打赏支持
粉丝 222
博文 581
码字总数 411013
作品 0

在JAVA NIO相关的组件中,ByteBuffer是除了Selector、Channel之外的另一个很重要的组件,它是直接和Channel打交道的缓冲区,通常场景或是从ByteBuffer写入Channel,或是从Channel读入Buffer;...

Float_Luuu ⋅ 2016/03/13 ⋅ 0



阿阮 ⋅ 2014/12/02 ⋅ 10

对于 Netty ByteBuf 的零拷贝(Zero Copy) 的理解

根据 Wiki 对 Zero-copy 的定义: "Zero-copy" describes computer operations in which the CPU does not perform the task of copying data from one memory area to another. This is fre......

永顺 ⋅ 2017/11/29 ⋅ 0

Netty ByteBuf详解

本文主要介绍Netty5 ByteBuf原理及具体使用。 基本结构:与NIO ByteBuffer类似,使用ByteBuffer往往需要在读写之间通过flip切换。ByteBuf里维护两个index,一个readerIndex,一个writerIndex;r...

robin-yao ⋅ 2015/04/28 ⋅ 0


NettyByteBuf基本用法二 上一篇http://my.oschina.net/xinxingegeya/blog/283227 ByteBuf's byte operations #1 Segment(段) that holds bytes that can be discarded(废弃的) as they ......

秋风醉了 ⋅ 2014/06/29 ⋅ 0

Netty Buffer

Netty高性能很大原因在于Netty Buffer的设计,Netty Buffer由Nio Buffer而来,并对Nio Buffer做了改进。首先,Netty Buffer的“零拷贝”特性使得Netty Buffer的性能十分优越;还有Netty内存池...

AaronSheng ⋅ 2016/10/15 ⋅ 0

Netty in Action ——— The codec framework

本文是Netty文集中“Netty in action”系列的文章。主要是对Norman Maurer and Marvin Allen Wolfthal 的 《Netty in action》一书简要翻译,同时对重要点加上一些自己补充和扩展。 本章含盖...

tomas家的小拨浪鼓 ⋅ 2017/12/15 ⋅ 0

Netty 4.0 源码分析(四):ByteBuf

Netty是基于流的消息传递机制。Netty框架中,所有消息的传输都依赖于ByteBuf接口,ByteBuf是Netty NIO框架中的缓冲区。ByteBuf接口可以理解为一般的Byte数组,不过Netty对Byte进行了封装,增...

GreenDay ⋅ 2014/09/13 ⋅ 0


有过痛苦的经历,特别能写出深刻的文章 —— 凯尔文. 肖 直接内存是IO框架的绝配,但直接内存的分配销毁不易,所以使用内存池能大幅提高性能。但,要重新培养被Java的自动垃圾回收惯坏了的惰...

Mr_Young ⋅ 2015/08/24 ⋅ 0


本章介绍 ByteBuf ByteBufHolder ByteBufAllocator 使用这些接口分配缓冲和执行操作 每当你需要传输数据时,它必须包含一个缓冲区。Java NIO API自带的缓冲区类是相当有限的,没有经过优化,...

李矮矮 ⋅ 2016/09/23 ⋅ 0





IDEA PermGen space内存溢出

解决方案: File -> Settings -> Build, Execution, Deployment / Build Tools / Maven / Runner下,找到VM Options选项,默认是空的,改为如下内容(或更大值)...

快乐的小火柴 ⋅ 16分钟前 ⋅ 0


什么是跨域? 跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的。 广义的跨域: 1.) 资源跳转: A链接、重定向、表单提交2.) 资源嵌入: <link>、<script>、<im...

临江仙卜算子 ⋅ 17分钟前 ⋅ 0


service命令用来控制系统服务的实用工具,例如启动、停止、重启和关闭系统服务,以及当前状态。当然也可以直接操作,例如/etc/init.d/mysqld restart等。 语法 service (选项)(参数) 选项...

Jpchina ⋅ 22分钟前 ⋅ 0

MySQL 联合索引的命中规则

为什么要用联合索引? 对于查询语句“SELECT T.* FROM T WHERE T.c1=1 AND T.c3=2”涉及到两列,这个时候我们一般采用一个联合索引(c1, c3);而不用两个单列索引,这是因为一条查询语句往往应...

hensemlee ⋅ 30分钟前 ⋅ 0

Spring 自动组件扫描

通常情况下都是在XML配置文件中手动声明Bean和组件的。不过Spring也可以自动扫描组件实例化Bean,这样就可以避免在XML文件中繁琐的Bean声明。 手动声明Bean: 这里不再啰嗦,就是简单地在XML...

霍淇滨 ⋅ 34分钟前 ⋅ 0



Jason_typ ⋅ 36分钟前 ⋅ 0


SpringBoot多数据源切换,先上配置文件: 1.pom: <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20......

JackyRiver ⋅ 38分钟前 ⋅ 0


版本:Boost 1.66.0 Windows库编译 官网指南:直接执行bootstrap.bat处理文件即可,可以我却遇到一堆的问题。 环境:Windows 10 + Visual Studio 2017 Boost编译出来库命名 boost库生成文件命...

水海云 ⋅ 42分钟前 ⋅ 0


如果jar文件是以外部依赖的形式导入的。Eclipse将web项目发布到Tomcat时,是不会自动发布这些依赖的。 可以通过Eclipse在项目上右击 - Propertics - Deployment Assembly,添加“Java Build ...

ArlenXu ⋅ 43分钟前 ⋅ 0

iview tree组件层级过多时可左右滚动

使用vue+iview的tree组件,iview官网iview的tree树形控件 问题描述:tree层级过多时左右不可滚动 问题解决:修改overflow属性值 .el-tree-node>.el-tree-node_children { overflow: vi...

YXMBetter ⋅ 44分钟前 ⋅ 0