当前位置:网站首页>bytebuffer 使用demo
bytebuffer 使用demo
2022-08-05 03:53:00 【java持续实践】
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();
// 判断是否还有剩余未读取数据
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
通过控制台看到, 第一次读取到了缓存的10个字节, 1到0. 挨个把1到0打印了出来.
第二次读取了3个字节, a到c, 挨个打印了出来.
总结步骤:
- 向buffer 写入数据: channel.read(buffer)
- 调用flip() 切换到读模式
- 从buffer 读取数据, 调用buffer.get()
- 调用clear获取compact() 切换到写模式
- 重复 1-4步骤
边栏推荐
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- 【Mysql进阶优化篇02】索引失效的10种情况及原理
- rpc-remote procedure call demo
- Web3.0 Dapps - the road to the future financial world
- XMjs cross-domain problem solving
- [Solved] Unity Coroutine coroutine is not executed effectively
- Ffmpeg - sources analysis
- Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
- The test salary is so high?20K just graduated
- How do newcomers get started and learn software testing?
猜你喜欢
How to wrap markdown - md file
21 Days Learning Challenge (2) Use of Graphical Device Trees
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
10 years of testing experience, worthless in the face of the biological age of 35
Dive into how it works together by simulating Vite
shell脚本:for循环与while循环
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
随机推荐
How to wrap markdown - md file
今年七夕,「情蔬」比礼物更有爱
Dive into how it works together by simulating Vite
调用阿里云oss和sms服务
public static
List asList(T... a) What is the prototype? The most effective seven performance testing techniques of software testing techniques
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
Kubernetes 网络入门
Slapped in the face: there are so many testers in a certain department of byte
Solana NFT开发指南
惨遭打脸:字节某部门竟有这么多测试员
rpc-remote procedure call demo
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
What is the difference between SAP ERP and ORACLE ERP?
DNS被劫持如何处理?
36-Jenkins-Job迁移
DEJA_VU3D - Cesium功能集 之 059-腾讯地图纠偏
Mathematics - Properties of Summation Symbols
[Software testing] unittest framework for automated testing