关于缓冲器ByteBuffer的三个方法:flip(),clear(),remind()。
一、flip():反转此缓冲区,将限制设置为当前位置,然后将位置设置为 0 !
之前的写操作会不断更新当前位置,当写操作完成之后,需调用此方法,将限制位置设置为当前位置,将当前位置设置为0,这样下一个读操作会从0开始,直到限制位置。
/**
* Flips this buffer. The limit is set to the current position and then
* the position is set to zero. If the mark is defined then it is
* discarded.
*
* <p> This method is often used in conjunction with the {@link
* java.nio.ByteBuffer#compact compact} method when transferring data from
* one place to another. </p>
*
* @return This buffer
*/
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
public byte get() {
return hb[ix(nextGetIndex())];
}
final int nextGetIndex() { // package-private
if (position >= limit)
throw new BufferUnderflowException();
return position++;
}
二、remain():与flip不同的是,不会修改限制位置。
比如初始化时:
ByteBuffer buffer=ByteBuffer.allocate(1024);
那么做读操作的时候就会读到第(1024-1)个索引,而flip却不一定能读到(1024-1)个索引,这取决于他的写操作的数据长度。
/**
* Rewinds this buffer. The position is set to zero and the mark is
* discarded.
*
* <p> Invoke this method before a sequence of channel-write or <i>get</i>
* operations, assuming that the limit has already been set
* appropriately. For example:
*
* <blockquote><pre>
* out.write(buf); // Write remaining data
* buf.rewind(); // Rewind buffer
* buf.get(array); // Copy data into array</pre></blockquote>
*
* @return This buffer
*/
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
三、clear():“清除”此缓冲区,将位置设置为 0,将限制设置为容量!此方法不能实际清除缓冲区中的数据,但从名称来看它似乎能够这样做,这样命名是因为它多数情况下确实是在清除数据时使用。因为调用该方法后,我们一般都会调用FileChannel.read(buff)或者buff.put()来把新的数据放到buff中,此时原来的内容就会被新的内容所覆盖!也不是全部覆盖,而是覆盖掉新数据所包含的字节数!所以看起来好象就是原来的内容被删除一样!
/**
* Clears this buffer. The position is set to zero, the limit is set to
* the capacity, and the mark is discarded.
*
* <p> Invoke this method before using a sequence of channel-read or
* <i>put</i> operations to fill this buffer. For example:
*
* <blockquote><pre>
* buf.clear(); // Prepare buffer for reading
* in.read(buf); // Read data</pre></blockquote>
*
* <p> This method does not actually erase the data in the buffer, but it
* is named as if it did because it will most often be used in situations
* in which that might as well be the case. </p>
*
* @return This buffer
*/
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
四、ByteBuffer类中提供position(),remaining(),hasRemaining(),limit()等方法来验证以上三点。
五、上述代码中提到的:
and the mark is discarded.
参照mark(),reset()方法。
分享到:
相关推荐
本文实例讲述了Android在JNI中使用ByteBuffer的方法。分享给大家供大家参考。具体如下: 一、ByteBuffer 定义 在NIO中,数据的读写操作始终是与缓冲区相关联的(读取时信道(SocketChannel)将数据读入缓冲区,写入时...
仿安卓ByteBuffer 完美组包、拆包
使用nio byteBuffer 实现按行读取文件(大文件) 在window/linux/macOS上均测试通过 对于中文乱码也已处理成功 完整注释,可随需求更改 有问题请邮件:mly610865580@126.com
Android
protobuf+long+bytebuffer,利用protobuf.js实现编解码 所需的三个js库
主要解决从流中获取数据,缓存,拆解,可用于TCP粘包问题
【IT十八掌徐培成】Java基础第26天-05.ByteBuffer-mark-pos-limit-cap-flip.zip
易语言汇编版ByteBuffer源码。主要用于各种网络协议的组包 具体用法可以点上面的网址 功能和jAVA的一样。@10371178。Tags:易语言汇编版ByteBuffer源码。
ios-byteBuffer [![CI状态]( Lee / ios-byteBuffer.svg?style = flat)]( Lee / ios-byteBuffer ) 用法 #分配 ByteBuffer *buffer = [ByteBuffer initWithOrder: ByteOrderLittleEndian]; #输入数据 - ( ...
java api之ByteBuffer基础、应用场景、实战讲解 文档中有丰富的例子代码实现
易语言汇编版ByteBuffer源码主要用于各种网络协议的组包 具体用法可以点上面的网址 功能和jAVA的一样
dena-bytebuffer
目录中文件数:25个 ├─(1) 第1章_01_nio三大组件-channel-buffer.mp4 ├─(2) 第1章_02_nio三大组件-服务器设计-多线程版.mp4 ├─(3) 第1章_03_nio三大组件-服务器设计-线程池版.mp4 ├─(4) 第1章_04_nio三大组件...
jdk api-ServerSocketChannel、Selector、ByteBuffer结合实现网络报文间的通讯
由于对于本程序 116个字节以上的行才有意义,所以 在next实现方法中,有对 116 长度的判断,否则返回 null 修正了之前版本中的问题: 修正后的方法 private int readByte() throws IOException{ fbb.rewind(); ...
NULL 博文链接:https://chinaestone.iteye.com/blog/468138
NULL 博文链接:https://zheng12tian.iteye.com/blog/1094811