当前位置:网站首页>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步骤
边栏推荐
- Shell script: for loop and the while loop
- ffmpeg -sources分析
- Redis1: Introduction to Redis, basic features of Redis, relational database, non-relational database, database development stage
- Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
- [TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
- How to solve the three major problems of bank data collection, data supplementary recording and index management?
- Redis key basic commands
- Open-Falcon of operation and maintenance monitoring system
- Static method to get configuration file data
- 新人如何入门和学习软件测试?
猜你喜欢
不看后悔,appium自动化环境完美搭建
Mysql's redo log detailed explanation
Mysql的undo log详解
10 years of testing experience, worthless in the face of the biological age of 35
How to discover a valuable GameFi?
Some conventional routines of program development (1)
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
[MRCTF2020]PYWebsite
[Software testing] unittest framework for automated testing
随机推荐
shell脚本:for循环与while循环
How to solve the three major problems of bank data collection, data supplementary recording and index management?
36-Jenkins-Job迁移
程序开发的一些常规套路(一)
Shell script: for loop and the while loop
UE4 为子弹蓝图添加声音和粒子效果
There are several common event handling methods in Swing?How to listen for events?
UE4 通过与其它Actor互动开门
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
UE4 opens door via interaction (keyboard key)
Growth-based checkerboard corner detection method
ffmpeg -sources分析
Spark Basics [Introduction, Getting Started with WordCount Cases]
MySql的索引学习和使用;(本人觉得足够详细)
2022.8.4-----leetcode.1403
达梦8数据库导出导入
[SWPU2019]Web1
【Mysql进阶优化篇02】索引失效的10种情况及原理
bytebuffer 内部结构