当前位置:网站首页>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步骤
边栏推荐
- The most comprehensive exam questions for software testing engineers in 2022
- bytebuffer internal structure
- [BJDCTF2020]EasySearch
- How to find all fields with empty data in sql
- 【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
- Event parse tree Drain3 usage and explanation
- Some conventional routines of program development (1)
- Shell script: for loop and the while loop
- Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
- Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
猜你喜欢

UE4 第一人称角色模板 添加蹲伏功能

Event parse tree Drain3 usage and explanation

冰蝎V4.0攻击来袭,安全狗产品可全面检测

Acid (ACID) Base (BASE) Principles for Database Design

Defect detection (image processing part)

markdown如何换行——md文件

不看后悔,appium自动化环境完美搭建

【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】

Kubernetes 网络入门

多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
随机推荐
bytebuffer put flip compact clear 方法演示
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
UE4 通过重叠事件开启门
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
Redis key basic commands
rpc-remote procedure call demo
DEJA_VU3D - Cesium功能集 之 057-百度地图纠偏
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
不看后悔,appium自动化环境完美搭建
ffmpeg -sources分析
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
36-Jenkins-Job迁移
大佬们,我注意到mysql cdc connector有参数scan.incremental.sna
Queue Topic: Recent Requests
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
You may use special comments to disable some warnings. Three ways to report errors
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
How do newcomers get started and learn software testing?
Growth-based checkerboard corner detection method
多御安全浏览器新版下载 | 功能优秀性能出众