当前位置:网站首页>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步骤
边栏推荐
- cross domain solution
- Use CH341A to program external Flash (W25Q16JV)
- Redis1: Introduction to Redis, basic features of Redis, relational database, non-relational database, database development stage
- ffmpeg enumeration decoders, encoders analysis
- Slapped in the face: there are so many testers in a certain department of byte
- 2022.8.4-----leetcode.1403
- Open-Falcon of operation and maintenance monitoring system
- 阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
- 多御安全浏览器新版下载 | 功能优秀性能出众
- leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
猜你喜欢

Initial solution of the structure

UE4 第一人称角色模板 添加生命值和调试伤害

炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit

Kubernetes 网络入门

YYGH-13-Customer Service Center

Based on holding YOLOv5 custom implementation of FacePose YOLO structure interpretation, YOLO data format conversion, YOLO process modification"

Haproxy搭建Web群集

用Unity发布APP到Hololens2无坑教程

Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals

markdown如何换行——md文件
随机推荐
[SWPU2019]Web1
[Software testing] unittest framework for automated testing
Dameng 8 database export and import
Fifteen. Actual combat - MySQL database building table character set and collation
2022软件测试工程师最全面试题
多御安全浏览器 V10.8.3.1 版正式发布,优化多项内容
What is the difference between SAP ERP and ORACLE ERP?
新人如何入门和学习软件测试?
MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
ffmpeg 像素格式基础知识
public static <T> List<T> asList(T... a) 原型是怎么回事?
How to discover a valuable GameFi?
Use CH341A to program external Flash (W25Q16JV)
数组常用方法总结
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
Call Alibaba Cloud oss and sms services
High Item 02 Information System Project Management Fundamentals
The most effective seven performance testing techniques of software testing techniques