当前位置:网站首页>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步骤
边栏推荐
- iMedicalLIS监听程序(2)
- Qixi Festival code confession
- iMedicalLIS listener (2)
- Android实战开发-Kotlin教程(入门篇-登录功能实现 3.3)
- [TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
- leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
- 结构体初解
- 【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
- Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
- Solana NFT开发指南
猜你喜欢
bytebuffer internal structure
[CISCN2019 South China Division]Web11
Use Unity to publish APP to Hololens2 without pit tutorial
【测量学】速成汇总——摘录高数帮
Haproxy搭建Web群集
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
Acid (ACID) Base (BASE) Principles for Database Design
Hard power or soft power, which is more important to testers?
Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
随机推荐
[CISCN2019 South China Division]Web11
How to solve the three major problems of bank data collection, data supplementary recording and index management?
Web3.0 Dapps - the road to the future financial world
10 years of testing experience, worthless in the face of the biological age of 35
ffmpeg 枚举decoders, encoders 分析
Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
There are several common event handling methods in Swing?How to listen for events?
35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
Dameng 8 database export and import
You may use special comments to disable some warnings. 报错解决的三种方式
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
ffmpeg -sources分析
Mysql的undo log详解
This year's Qixi Festival, "love vegetables" are more loving than gifts
日志导致线程Block的这些坑,你不得不防
今年七夕,「情蔬」比礼物更有爱
阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
[Software testing] unittest framework for automated testing
程序开发的一些常规套路(一)