java.nio.ByteBuffer类的rewind()方法用于倒带此缓冲区。位置设置为零,标记被丢弃。假设已正确设置了限制,请在序列channel-write或get操作之前调用此方法。调用此方法既不会更改,也不会丢弃标记的值。

用法:

public ByteBuffer rewind()

返回值:此方法返回此缓冲区。

下面是说明rewind()方法的示例:

范例1:

// Java program to demonstrate

// rewind() method

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// defining and allocating ByteBuffer

// using allocate() method

ByteBuffer byteBuffer = ByteBuffer.allocate(4);

// put byte value in byteBuffer

// using put() method

byteBuffer.put((byte)20);

byteBuffer.put((byte)'a');

// print the byte buffer

System.out.println("Buffer before operation:"

+ Arrays.toString(byteBuffer.array())

+ "\nPosition:" + byteBuffer.position()

+ "\nLimit:" + byteBuffer.limit());

// rewind the Buffer

// using rewind() method

byteBuffer.rewind();

// print the bytebuffer

System.out.println("\nBuffer after operation:"

+ Arrays.toString(byteBuffer.array())

+ "\nPosition:" + byteBuffer.position()

+ "\nLimit:" + byteBuffer.limit());

}

}

输出:

Buffer before operation:[20, 97, 0, 0]

Position:2

Limit:4

Buffer after operation:[20, 97, 0, 0]

Position:0

Limit:4

范例2:

// Java program to demonstrate

// rewind() method

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// defining and allocating ByteBuffer

// using allocate() method

ByteBuffer byteBuffer = ByteBuffer.allocate(5);

// put byte value in byteBuffer

// using put() method

byteBuffer.put((byte)20);

byteBuffer.put((byte)30);

byteBuffer.put((byte)40);

// mark will be going to discarded by rewind()

byteBuffer.mark();

// print the buffer

System.out.println("Buffer before operation:"

+ Arrays.toString(byteBuffer.array())

+ "\nPosition:" + byteBuffer.position()

+ "\nLimit:" + byteBuffer.limit());

// Rewind the Buffer

// using rewind() method

byteBuffer.rewind();

// print the buffer

System.out.println("\nBuffer after operation:"

+ Arrays.toString(byteBuffer.array())

+ "\nPosition:" + byteBuffer.position()

+ "\nLimit:" + byteBuffer.limit());

}

}

输出:

Buffer before operation:[20, 30, 40, 0, 0]

Position:3

Limit:5

Buffer after operation:[20, 30, 40, 0, 0]

Position:0

Limit:5

java bytebuffer分包收集,Java ByteBuffer rewind()用法及代码示例相关推荐

  1. java中week of year_Java WeekFields weekOfYear()用法及代码示例

    WeekFields类的weekOfYear()方法用于返回一个字段,以基于此WeekFields访问一年中的星期.例: 如果一年的第一天是星期一,则第一周从第一天开始,没有零周 如果一年的第二天是星 ...

  2. java indexof int_Java Stack indexOf(Object, int)用法及代码示例

    Java.util.Stack.indexOf(Object element,int index)方法用于在此Stack中首次出现指定元素的索引,从索引开始向前搜索,如果找不到该元素,则返回-1.更正 ...

  3. java scanner怎么用_Java Scanner delimiter()用法及代码示例

    java.util.Scanner类的delimiter()方法返回此Scanner当前用于匹配定界符的Pattern. 用法: public Pattern delimiter() 返回值:该函数返 ...

  4. java reader类 实例_Java Reader ready()用法及代码示例

    Java中Reader类的ready()方法用于检查此Reader是否已准备好被读取.它返回一个布尔值,该值指示阅读器是否准备就绪. 用法: public void ready() 参数:此方法不接受 ...

  5. java 二维高斯_Java Random nextGaussian()用法及代码示例

    随机类的nextGaussian()方法返回下一个伪随机数,即与随机数生成器序列的平均值为0.0,标准差为1.0的高斯(正态)分布双精度值. 用法: public double nextGaussia ...

  6. minimum在java中的意思_Java Calendar getMinimum()用法及代码示例

    Calendar类中的getMinimum(int calndr_field)方法用于返回此Calendar实例的给定日历字段(int calndr_field)的最小值. 用法: public ab ...

  7. java printwriter实例_Java PrintWriter print(String)用法及代码示例

    Java中的PrintWriter类的print(String)方法用于在流上打印指定的String值.该字符串值用作参数. 用法: public void print(String StringVa ...

  8. java pattern详解_Java Pattern pattern()用法及代码示例

    Java中Pattern类的pattern()方法用于获取正则表达式,将其编译以创建此模式.我们使用正则表达式创建模式,并且使用此方法来获取相同的源表达式. 用法: public String pat ...

  9. java使用abs函数_Java Math abs()用法及代码示例

    java.lang.Math.abs()返回给定参数的绝对值. 如果参数不为负,则返回参数. 如果参数为负,则返回参数的取反. 用法: public static DataType abs(DataT ...

最新文章

  1. CSS实现跨浏览器兼容性的盒阴影效果
  2. 天籁obd接口针脚定义_OBD协议介绍
  3. WPF 带CheckBox、图标的TreeView
  4. 【 Grey Hack 】万金油脚本:从路由器获取Password
  5. seata 如何开启tcc事物_分布式事务Seata-TCC源码分析
  6. 万众期待的PowerBI Report Server与PowerBI Premium
  7. WSL Arch Linux 已在 Microsoft Store 上可用
  8. oracle disk missing,oracle asm disk被格式化为ntfs文件系统
  9. cisco路由器基本实验之六 CHAP and RIP (Boson NetSim)
  10. Servlet CDI Example Analysis
  11. 电大计算机网考选择题多少分,[2017年电大]电大计算机网考选择题题库精选汇总.doc...
  12. mysql 查看修改连接数据库_mysql查看最大连接数和修改mysql数据库最大连接数方法...
  13. SQL替换字段中一部分字符串
  14. Python+Django+vue的旅游信息网站系统项目源码介绍
  15. C语言---内存操作及基础知识
  16. python死循环_Python for死循环
  17. 韩寒:给李彦宏先生的一封信
  18. 利用 Python 特性在 Jinja2 模板中执行任意代码
  19. 【车载】轮速-AK协议
  20. python中imag用法_花了一晚上时间,终于把Python的基本用法归纳好了!

热门文章

  1. 锐捷网络:引领地铁移动互联网快捷交付2.0时代到来
  2. 六种水果健康食用方法
  3. 怎么用clementine做关联规则分析
  4. 【随感】8月工作总结:这个月我去干啥了?
  5. 可摇尾巴,可撸,一只无脸机器猫为什么这么火?
  6. MATLAB | πDay快乐,pi居然有这么多炫酷好玩的可视化
  7. 大陆ARS-408XX毫米波雷达
  8. Root系统后使用RE管理器删除系统自带程序的方法
  9. HTML网页设计自动适应屏幕宽度
  10. 母鸡下蛋 - UPCOJ 3636 - 尺取