java bytebuffer分包收集,Java ByteBuffer rewind()用法及代码示例
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()用法及代码示例相关推荐
- java中week of year_Java WeekFields weekOfYear()用法及代码示例
WeekFields类的weekOfYear()方法用于返回一个字段,以基于此WeekFields访问一年中的星期.例: 如果一年的第一天是星期一,则第一周从第一天开始,没有零周 如果一年的第二天是星 ...
- java indexof int_Java Stack indexOf(Object, int)用法及代码示例
Java.util.Stack.indexOf(Object element,int index)方法用于在此Stack中首次出现指定元素的索引,从索引开始向前搜索,如果找不到该元素,则返回-1.更正 ...
- java scanner怎么用_Java Scanner delimiter()用法及代码示例
java.util.Scanner类的delimiter()方法返回此Scanner当前用于匹配定界符的Pattern. 用法: public Pattern delimiter() 返回值:该函数返 ...
- java reader类 实例_Java Reader ready()用法及代码示例
Java中Reader类的ready()方法用于检查此Reader是否已准备好被读取.它返回一个布尔值,该值指示阅读器是否准备就绪. 用法: public void ready() 参数:此方法不接受 ...
- java 二维高斯_Java Random nextGaussian()用法及代码示例
随机类的nextGaussian()方法返回下一个伪随机数,即与随机数生成器序列的平均值为0.0,标准差为1.0的高斯(正态)分布双精度值. 用法: public double nextGaussia ...
- minimum在java中的意思_Java Calendar getMinimum()用法及代码示例
Calendar类中的getMinimum(int calndr_field)方法用于返回此Calendar实例的给定日历字段(int calndr_field)的最小值. 用法: public ab ...
- java printwriter实例_Java PrintWriter print(String)用法及代码示例
Java中的PrintWriter类的print(String)方法用于在流上打印指定的String值.该字符串值用作参数. 用法: public void print(String StringVa ...
- java pattern详解_Java Pattern pattern()用法及代码示例
Java中Pattern类的pattern()方法用于获取正则表达式,将其编译以创建此模式.我们使用正则表达式创建模式,并且使用此方法来获取相同的源表达式. 用法: public String pat ...
- java使用abs函数_Java Math abs()用法及代码示例
java.lang.Math.abs()返回给定参数的绝对值. 如果参数不为负,则返回参数. 如果参数为负,则返回参数的取反. 用法: public static DataType abs(DataT ...
最新文章
- CSS实现跨浏览器兼容性的盒阴影效果
- 天籁obd接口针脚定义_OBD协议介绍
- WPF 带CheckBox、图标的TreeView
- 【 Grey Hack 】万金油脚本:从路由器获取Password
- seata 如何开启tcc事物_分布式事务Seata-TCC源码分析
- 万众期待的PowerBI Report Server与PowerBI Premium
- WSL Arch Linux 已在 Microsoft Store 上可用
- oracle disk missing,oracle asm disk被格式化为ntfs文件系统
- cisco路由器基本实验之六 CHAP and RIP (Boson NetSim)
- Servlet CDI Example Analysis
- 电大计算机网考选择题多少分,[2017年电大]电大计算机网考选择题题库精选汇总.doc...
- mysql 查看修改连接数据库_mysql查看最大连接数和修改mysql数据库最大连接数方法...
- SQL替换字段中一部分字符串
- Python+Django+vue的旅游信息网站系统项目源码介绍
- C语言---内存操作及基础知识
- python死循环_Python for死循环
- 韩寒:给李彦宏先生的一封信
- 利用 Python 特性在 Jinja2 模板中执行任意代码
- 【车载】轮速-AK协议
- python中imag用法_花了一晚上时间,终于把Python的基本用法归纳好了!