当前位置:网站首页>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步骤
边栏推荐
- Dameng 8 database export and import
- 银行数据采集,数据补录与指标管理3大问题如何解决?
- 国学*周易*梅花易数 代码实现效果展示 - 梅花心易
- [Paper Notes] MapReduce: Simplified Data Processing on Large Clusters
- Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
- Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
- UE4 第一人称角色模板 添加蹲伏功能
- 概率论的学习和整理8: 几何分布和超几何分布
- 炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit
- Hard power or soft power, which is more important to testers?
猜你喜欢

Spark基础【介绍、入门WordCount案例】

Hard power or soft power, which is more important to testers?

Paparazzi: Surface Editing by way of Multi-View Image Processing

Mysql's redo log detailed explanation

Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value

Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
![[CISCN2019 South China Division]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)
[CISCN2019 South China Division]Web11
![[Software testing] unittest framework for automated testing](/img/80/caedd5cf6dd61c9d75475866613cac.png)
[Software testing] unittest framework for automated testing
![[极客大挑战 2019]FinalSQL](/img/e4/0c8225ef7c5e7e5bdbaac2ef6fc867.png)
[极客大挑战 2019]FinalSQL

Walter talked little knowledge | "remote passthrough" that something
随机推荐
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
Paparazzi: Surface Editing by way of Multi-View Image Processing
YYGH-13-客服中心
Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
Open-Falcon of operation and maintenance monitoring system
Ffmpeg - sources analysis
达梦8数据库导出导入
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
Walter talked little knowledge | "remote passthrough" that something
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
Spark基础【介绍、入门WordCount案例】
Shell script: for loop and the while loop
Solana NFT开发指南
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
36-Jenkins-Job迁移
【树莓派】树莓派调光
mutillidae下载及安装
将故事写成我们
[SWPU2019]Web1