当前位置:网站首页>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步骤
边栏推荐
- 2022 Hangzhou Electric Multi-School 1st Game
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- The test salary is so high?20K just graduated
- MySql的索引学习和使用;(本人觉得足够详细)
- ffmpeg 像素格式基础知识
- Open-Falcon of operation and maintenance monitoring system
- ffmpeg 枚举decoders, encoders 分析
- On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
- DEJA_VU3D - Cesium功能集 之 056-智图Arcgis地图纠偏
- 多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
猜你喜欢
UE4 通过重叠事件开启门
Kubernetes 网络入门
Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
Defect detection (image processing part)
public static
List asList(T... a) What is the prototype? [极客大挑战 2019]FinalSQL
[GYCTF2020]EasyThinking
MySql index learning and use; (I think it is detailed enough)
How do newcomers get started and learn software testing?
JeeSite新建报表
随机推荐
数据库设计的酸(ACID)碱(BASE)原则
结构体初解
将故事写成我们
第一次性能测试实践,有“亿”点点紧张
Slapped in the face: there are so many testers in a certain department of byte
36-Jenkins-Job Migration
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters
The most comprehensive exam questions for software testing engineers in 2022
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?
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
Why is the pca component not associated
This year's Qixi Festival, "love vegetables" are more loving than gifts
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
[SWPU2019]Web1
Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
七夕节代码表白
905. 区间选点
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..