作者简介:大家好,我是码炫码哥,前中兴通讯、美团架构师,现任某互联网公司CTO,兼职码炫课堂主讲源码系列专题
代表作:《jdk源码&多线程&高并发》,《深入tomcat源码解析》,《深入netty源码解析》,《深入dubbo源码解析》,《深入springboot源码解析》,《深入spring源码解析》,《深入redis源码解析》等
联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬。码炫课堂的个人空间-码炫课堂个人主页-哔哩哔哩视频
前言
本篇将通过nio读取一个文本文件来演示bytebuffer的基本使用
一、准备
- 数据准备
创建data.txt文件,增加如下内容:
1234567890abcd
- 创建Maven项目
- 安装lomback插件
二、ByteBuffer 使用分析
- 向 buffer 写入数据,例如调用 channel.read(buffer)
- 调用 flip() 切换至 读模式
- 从 buffer 读取数据,例如调用 buffer.get()
- 调用 clear() 或 compact() 切换至 写模式
- 重复 1~4 步骤
三、代码实现
- 引入pom依赖
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.39.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
- 代码实现
import lombok.extern.slf4j.Slf4j;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* Created by mx
* Date 2022/5/24
* Description ByteBuffer Demo
*/
@Slf4j
public class TestByteBuffer {
public static void main(String[] args){
/**
* FileChannel
* 方式1:输入输出流
* 方式2:RandomAccessFile
*/
try (FileChannel channel = new FileInputStream("datas/data.txt").getChannel()){
//准备缓冲区
ByteBuffer buffer = ByteBuffer.allocate(10);
while (true){
//1.从 channel 读取数据,向buffer写入
int len = channel.read(buffer);
log.debug("读取到的字节数:{}",len);
if(len == -1){ //判断内容是否读取完
break;
}
//打印buffer中的内容
buffer.flip(); //2.切换至读模式
while (buffer.hasRemaining()){ //是否还有剩余未读数据
byte b = buffer.get(); //3.读取数据内容
log.debug("实际字节:{}",(char) b);
}
buffer.clear(); //4.切换到写模式
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 读取到的字节数:10
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:1
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:2
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:3
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:4
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:5
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:6
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:7
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:8
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:9
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:0
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 读取到的字节数:4
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:a
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:b
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:c
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 实际字节:d
10:16:41 [DEBUG] [main] c.l.n.b.TestByteBuffer - 读取到的字节数:-1
整体目录结构