当前位置:网站首页>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步骤
边栏推荐
- YYGH-13-Customer Service Center
- How to wrap markdown - md file
- 七夕节代码表白
- Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
- There are several common event handling methods in Swing?How to listen for events?
- UE4 第一人称角色模板 添加蹲伏功能
- Event parse tree Drain3 usage and explanation
- 商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
- YYGH-13-客服中心
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
猜你喜欢
运维监控系统之Open-Falcon
UE4 第一人称角色模板 添加生命值和调试伤害
iMedicalLIS监听程序(2)
MySql index learning and use; (I think it is detailed enough)
markdown如何换行——md文件
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
There are several common event handling methods in Swing?How to listen for events?
Growth-based checkerboard corner detection method
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
随机推荐
The sword refers to Offer--find the repeated numbers in the array (three solutions)
数据库设计的酸(ACID)碱(BASE)原则
多御安全浏览器新版下载 | 功能优秀性能出众
Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
cross domain solution
Defect detection (image processing part)
ffmpeg 像素格式基础知识
Android实战开发-Kotlin教程(入门篇-登录功能实现 3.3)
Open-Falcon of operation and maintenance monitoring system
【背包九讲——01背包问题】
【测量学】速成汇总——摘录高数帮
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
36-Jenkins-Job迁移
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
Fifteen. Actual combat - MySQL database building table character set and collation
After the large pixel panorama is completed, what are the promotion methods?
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
UE4 opens door via interaction (keyboard key)
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto