package com.chen.nio;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.nio.IntBuffer;
import java.util.stream.IntStream;
/**
* Buffer 测试:Buffer类中比较重要的几个属性如下
* <ul>
* <li>position-游标,读和写默认都是从0开始,最大值为 limit-1</li>
* <li>limit-最大上限,写模式下默认是 capacity 的值,读模式下为缓冲区的数据量大小</li>
* <li>capacity-容量,并不是字节数的大小,指的是写入的数据对象的数量</li>
* </ul>
*
* @author chenzihan
* @date 2022/02/23
*/
@Slf4j
public class BufferTest {
/**
* 创建 Buffer 必须使用静态方法 allocate,创建完成后默认是写状态,具体的 api 可以参见具体的 Buffer 子类
*/
@Test
public void allocate() {
IntBuffer intBuffer = IntBuffer.allocate(10);
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
}
/**
* 在 buffer 中放数据
*/
@Test
public void put() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(1, 5)
.forEach(intBuffer::put);
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
}
/**
* 翻转:从写模式转换为读模式。
*/
@Test
public void flip() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(1, 5)
.forEach(intBuffer::put);
intBuffer.flip();
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
}
/**
* 从缓冲区中读取数据,默认从 index 为 0 开始读取,可以自己指定 index 进行读取
*/
@Test
public void get() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(1, 5)
.forEach(intBuffer::put);
intBuffer.flip();
IntStream.rangeClosed(1, 2)
.forEach(i -> {
// 指定 index 读取不会改变 position 的值
int result = intBuffer.get(i);
log.info("result = {}", result);
log.info("position = {}", intBuffer.position());
});
IntStream.rangeClosed(1, 4)
.forEach(i -> {
// 不指定 index 的值,会改变 position 的值
int result = intBuffer.get();
log.info("result = {}", result);
log.info("position = {}", intBuffer.position());
});
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
}
/**
* 倒带:读取完以后,调用 rewind 方法可以重置 position 和 mark 的状态,从头开始读取
*/
@Test
public void rewind() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(1, 5)
.forEach(intBuffer::put);
intBuffer.flip();
IntStream.rangeClosed(1, 2)
.forEach(i -> intBuffer.get());
intBuffer.rewind();
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
}
/**
* mark & reset,这两个方法一般配套使用,mark 记录 position 某一刻的状态,后续改变后,可以调用 reset 方法还原到 mark 时的状态
*/
@Test
public void reset() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(1, 5)
.forEach(intBuffer::put);
intBuffer.flip();
IntStream.rangeClosed(0, 4)
.forEach(i -> {
if (i == 2) {
intBuffer.mark();
}
int result = intBuffer.get();
log.info("result = {}", result);
});
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
intBuffer.reset();
log.info("position = {}", intBuffer.position());
log.info("limit = {}", intBuffer.limit());
log.info("capacity = {}", intBuffer.capacity());
}
/**
* 清空缓冲区:会将 buffer 重置到初始的写状态,即 position = 0;limit = capacity;mark = -1;
* 但是 clear 不会清空 buffer 的数组,所以通过 get(index) 还是可以获取固定位置下的元素
*/
@Test
public void clear() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(1, 5)
.forEach(intBuffer::put);
intBuffer.flip();
intBuffer.clear();
intBuffer.compact();
IntStream.rangeClosed(0, 4)
.forEach(i -> {
int result = intBuffer.get(i);
log.info("result = {}", result);
});
}
/**
* 压缩 buffer,具体规则参见 IntBuffer.compact 的注释
*/
@Test
public void compact() {
IntBuffer intBuffer = IntBuffer.allocate(10);
IntStream.rangeClosed(0, 4)
.forEach(intBuffer::put);
intBuffer.flip();
intBuffer.compact();
IntStream.rangeClosed(0, 9)
.forEach(i -> {
int result = intBuffer.get(i);
log.info("result = {}", result);
});
}
}
java 新手练习小项目
需积分: 0 192 浏览量
更新于2023-09-20
收藏 6KB ZIP 举报
Java是一种广泛使用的面向对象的编程语言,以其跨平台、高性能和丰富的类库而著名。对于初学者来说,Java提供了一个良好的学习曲线,可以快速入门并掌握基础。本项目旨在为Java新手提供一个实践平台,通过实际的小项目来巩固理论知识。
在"java8-main"这个压缩包中,我们可以推断这包含了一个基于Java 8的主程序或项目。Java 8是Java历史上的一个重要版本,引入了许多新特性,如lambda表达式、函数式接口、Stream API以及日期和时间API的改进等。
1. **Lambda表达式**:这是Java 8最显著的改变之一,它简化了处理匿名函数的方式。在项目中,你可能会看到使用lambda来定义简短的、无状态的代码块,比如在集合操作中,它可以使得代码更加简洁、易读。
2. **函数式接口**:为了支持lambda,Java 8引入了函数式接口,这些接口只有一个抽象方法。`Runnable`、`Comparator`和`Function`等都是预定义的函数式接口。在项目中,你会学习如何利用这些接口实现回调或者作为参数传递。
3. **Stream API**:Stream API提供了处理集合的新方式,可以进行数据过滤、映射、聚合等操作。在"java8-main"项目中,你可能会遇到使用Stream API对数据进行处理的场景,比如从集合中筛选、转换或统计元素。
4. **日期和时间API**:Java 8的`java.time`包取代了过时的`java.util.Date`和`Calendar`,提供了更强大、更易于使用的日期和时间处理功能。项目中可能涉及到创建、比较、格式化日期和时间等操作。
5. **Optional类**:这是一个容器对象,可能包含或者不包含非null值。Optional帮助避免空指针异常,使得代码更清晰。在项目里,你可能会看到Optional用于表示可能存在或不存在的值。
6. **方法引用来优化代码**:除了lambda,Java 8还引入了方法引用,可以将方法名用作函数接口的实例。这在需要传递方法作为参数时非常有用,可以使代码更紧凑。
在实践中,你需要了解如何组织代码结构,使用Maven或Gradle构建工具管理依赖,以及遵循良好的编程规范,例如SOLID原则。此外,理解和使用设计模式也是提升编程能力的关键,例如单例模式、工厂模式和观察者模式等。
"java8-main"项目是一个理想的起点,它涵盖了Java 8的重要特性,可以帮助初学者将理论知识转化为实际技能。通过完成这个项目,你不仅能够掌握Java编程的基础,还能理解如何应用现代Java语言特性来解决实际问题。

学习资源网
- 粉丝: 942
最新资源
- VMware虚拟机中安装win10操作系统.doc
- 车站设备电扶梯设备项目管理手册.pdf
- 办公楼施工项目管理实施规划及施工图预算的编制.doc
- 2022招标师《项目管理》考试大纲.docx
- 2023年02326操作系统自考.doc
- 2023年计算机应用基础开专选修期末改.doc
- 第四章自组织竞争神经网络第14周课.ppt
- 第2章通信线路工程设计概论.pptx
- 第六章网络经济效率.pptx
- 2023年重庆电大形成性考核EXCEL操作题操作步骤.docx
- 2023年市政公用工程主项试卷福建二建网络继续教育.doc
- WEB开发实训报告.doc
- 操作系统试题与答案.doc
- 常见http错误提示代码介绍Windows服务器操作系统-电脑资料.doc
- Java中国象棋对弈系统毕业论文.doc
- 2023年电大历年试题及答案近十套程序设计基础专.doc