当前位置:网站首页>bytebuffer use demo
bytebuffer use demo
2022-08-05 03:59:00 【java continuous practice】
pom文件
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
</dependencies>
package cn.itcast.netty.c1;
import lombok.extern.slf4j.Slf4j;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/** * 类名称:TestByteBuffer * @author: https://javaweixin6.blog.csdn.net/ * 创建时间:2022-07-31 13:42 */
@Slf4j
public class TestByteBuffer {
public static void main(String[] args) {
try (FileChannel channel = new FileInputStream("data.txt").getChannel()){
// 准备缓冲区 缓冲10个字节
ByteBuffer buffer = ByteBuffer.allocate(10);
while (true) {
// 从channel 读取数据, 向buffer写入
int len = channel.read(buffer);
log.debug("读取到的字节数{}", len);
if (len == -1) {
break;
}
// 切换到buffer的读模式 .
buffer.flip();
// It is judged whether there is any remaining unread data
while (buffer.hasRemaining()) {
// 一次读一个字节.
byte b = buffer.get();
// 打印buffer的内容
// System.out.println((char)b);
log.debug("实际字节{}", (char)b);
}
// 切换为写模式
buffer.clear();
}
} catch (IOException e) {
}
}
}
创建一个txt文件, 放在项目最外层, 内容如下, 有13个字节.
控制台打印如下:
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 读取到的字节数10
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节1
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节2
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节3
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节4
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节5
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节6
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节7
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节8
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节9
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节0
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 读取到的字节数3
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节a
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节b
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 实际字节c
14:30:30 [DEBUG] [main] c.i.n.c.TestByteBuffer - 读取到的字节数-1
see through the console, The cache is read for the first time10个字节, 1到0. 挨个把1到0打印了出来.
第二次读取了3个字节, a到c, printed out one by one.
总结步骤:
- 向buffer 写入数据: channel.read(buffer)
- 调用flip() 切换到读模式
- 从buffer 读取数据, 调用buffer.get()
- 调用clear获取compact() 切换到写模式
- 重复 1-4步骤
边栏推荐
- Use CH341A to program external Flash (W25Q16JV)
- ffmpeg 枚举decoders, encoders 分析
- 银行数据采集,数据补录与指标管理3大问题如何解决?
- 第一次性能测试实践,有“亿”点点紧张
- ffmpeg enumeration decoders, encoders analysis
- Summary of common methods of arrays
- bytebuffer put flip compact clear 方法演示
- [Software testing] unittest framework for automated testing
- 队列题目:最近的请求次数
- UE4 第一人称角色模板 添加生命值和调试伤害
猜你喜欢
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
Web3.0 Dapps——通往未来金融世界的道路
public static
List asList(T... a) What is the prototype? [BSidesCF 2019]Kookie
UE4 通过重叠事件开启门
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
Growth-based checkerboard corner detection method
UE4 第一人称角色模板 添加冲刺(加速)功能
[MRCTF2020]Ezpop(详解)
Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
随机推荐
markdown如何换行——md文件
iMedicalLIS监听程序(2)
Spark基础【介绍、入门WordCount案例】
Fifteen. Actual combat - MySQL database building table character set and collation
UE4 第一人称角色模板 添加冲刺(加速)功能
Slapped in the face: there are so many testers in a certain department of byte
Haproxy搭建Web群集
Open-Falcon of operation and maintenance monitoring system
阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
事件解析树Drain3使用方法和解释
多御安全浏览器新版下载 | 功能优秀性能出众
Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
How to find all fields with empty data in sql
Initial solution of the structure
Kubernetes 网络入门
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
从企业的视角来看,数据中台到底意味着什么?
[SWPU2019]Web1
The most comprehensive exam questions for software testing engineers in 2022
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容