Skip to content

Commit be10ac2

Browse files
committed
初始代码
1 parent 63c0100 commit be10ac2

File tree

4 files changed

+154
-111
lines changed

4 files changed

+154
-111
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# async-file
22

33
#### 介绍
4-
框架提供Java异步读写文件功能,除依赖slf4j日志包,不依赖其他第三方包。Java应用程序引入框架可以简单的,异步和非阻塞的读写文件。框架包含三个工具类:
4+
框架提供Java异步读写文件工具,使用Java NIO 文件读写库。Java应用程序引入框架可以简单的,异步和非阻塞的读写文件。框架包含三个工具类:
55

66

7-
[`AIOFileReader`](src/main/java/io/github/kavahub/file/reader/AIOFileReader.java) 异步读取文件,使用Java NIO库 [`AsynchronousFileChannel`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html)[`CompletionHandler`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html) 实现。
7+
- [`AIOFileReader`](src/main/java/io/github/kavahub/file/reader/AIOFileReader.java) 异步读取文件,使用Java NIO库 [`AsynchronousFileChannel`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html)[`CompletionHandler`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html) 实现。
88

9-
[`AIOFileWriter`](src/main/java/io/github/kavahub/file/writer/AIOFileWriter.java) 异步写入文件,使用Java NIO库 [`AsynchronousFileChannel`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html)[`CompletionHandler`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html) 实现。
9+
- [`AIOFileWriter`](src/main/java/io/github/kavahub/file/writer/AIOFileWriter.java) 异步写入文件,使用Java NIO库 [`AsynchronousFileChannel`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/AsynchronousFileChannel.html)[`CompletionHandler`](https://docs.oracle.com/javase/10/docs/api/java/nio/channels/CompletionHandler.html) 实现。
1010

11-
[`NIOFileLineReader`](src/main/java/io/github/kavahub/file/reader/NIOFileLineReader.java) 非阻塞读取文件,使用 [`ForkJoinPool`](https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/ForkJoinPool.html)[`BufferedReader`](https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html) 实现
11+
- [`NIOFileLineReader`](src/main/java/io/github/kavahub/file/reader/NIOFileLineReader.java) 非阻塞读取文件,使用 [`ForkJoinPool`](https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/ForkJoinPool.html)[`BufferedReader`](https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html) 实现
1212

1313
Java提供的 [`Files`](https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html) 文件读取功能是阻塞的。
1414

@@ -302,7 +302,7 @@ Thread-7
302302

303303
关于使用的建议:
304304

305-
- 文件的异步读写,并不是未了提高文件的读取性能,而是提高文件读取的吞吐量(读取更多的文件,并保持性能,使JVM可以稳定运行)。
305+
- 文件的异步读写,并不是为了提高文件的读取性能,而是提高文件读取的吞吐量(读取更多的文件,并保持性能,使JVM可以稳定运行)。
306306
- 在大多数情况下,使用Jdk提供的[`Files`](https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html)或许更合适。
307307
- 不要为了异步而异步,找到问题所在,也许解决问题的关键不是异步。
308308

@@ -311,9 +311,13 @@ Thread-7
311311

312312
建议使用优先级: `Java NIO Files` > `NIOFileLineReader` > `AIOFileReader`
313313

314+
#### 如何参与开发
315+
316+
317+
314318
#### 其他开源项目
315319

316-
- [RxIo](https://github.com/javasync/RxIo)
320+
- [RxIo](https://github.com/javasync/RxIo) : Asynchronous non-blocking File Reader and Writer library for Java
317321

318322
#### 参考文档
319323

pom.xml

Lines changed: 126 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>io.github.kavahub</groupId>
5-
<artifactId>async-file</artifactId>
5+
<artifactId>kavahub-async-file</artifactId>
66
<version>1.0.0-SNA</version>
7-
<name>async-file</name>
7+
<name>kavahub-async-file</name>
88
<packaging>jar</packaging>
99

1010
<description>
@@ -14,10 +14,10 @@
1414
<url>https://gitee.com/yangyunjiao/async-file</url>
1515
<licenses>
1616
<license>
17-
<name>MIT license</name>
18-
<url>https://opensource.org/licenses/MIT</url>
17+
<name>MIT license</name>
18+
<url>https://opensource.org/licenses/MIT</url>
1919
</license>
20-
</licenses>
20+
</licenses>
2121
<developers>
2222
<developer>
2323
<name>PinWei Wan</name>
@@ -50,66 +50,72 @@
5050
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5151
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5252
<argLine>-Dfile.encoding=UTF-8</argLine>
53-
</properties>
5453

55-
<dependencyManagement>
56-
<dependencies>
57-
<dependency>
58-
<groupId>org.junit</groupId>
59-
<artifactId>junit-bom</artifactId>
60-
<version>5.8.1</version>
61-
<type>pom</type>
62-
<scope>import</scope>
63-
</dependency>
64-
</dependencies>
65-
</dependencyManagement>
54+
<!-- maven plugin -->
55+
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
56+
<maven-release-plugin.veriosn>3.0.0-M4</maven-release-plugin.veriosn>
57+
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
58+
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
59+
<maven-javadoc-plugin.version>3.3.1</maven-javadoc-plugin.version>
60+
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
61+
62+
<!-- thrid lib -->
63+
<junit-jupiter.version>5.8.1</junit-jupiter.version>
64+
<slf4j-api.version>1.7.32</slf4j-api.version>
65+
<jmh-generator-annprocess.version>1.33</jmh-generator-annprocess.version>
66+
<testng.version>7.4.0</testng.version>
67+
<assertj-core.version>3.21.0</assertj-core.version>
68+
<logback-classic.version>1.2.7</logback-classic.version>
69+
<lombok.version>1.18.20</lombok.version>
70+
</properties>
6671

6772
<dependencies>
6873
<dependency>
6974
<groupId>org.slf4j</groupId>
7075
<artifactId>slf4j-api</artifactId>
71-
<version>2.0.0-alpha4</version>
76+
<version>${slf4j-api.version}</version>
7277
</dependency>
7378

7479
<dependency>
7580
<groupId>org.openjdk.jmh</groupId>
7681
<artifactId>jmh-generator-annprocess</artifactId>
77-
<version>1.33</version>
82+
<version>${jmh-generator-annprocess.version}</version>
7883
<scope>provided</scope>
7984
</dependency>
8085

8186
<!-- test -->
8287
<dependency>
8388
<groupId>org.junit.jupiter</groupId>
8489
<artifactId>junit-jupiter</artifactId>
85-
<scope>test</scope>
86-
</dependency>
87-
88-
<dependency>
89-
<groupId>org.testng</groupId>
90-
<artifactId>testng</artifactId>
91-
<version>7.4.0</version>
90+
<version>${junit-jupiter.version}</version>
9291
<scope>test</scope>
9392
</dependency>
9493

9594
<dependency>
9695
<groupId>org.assertj</groupId>
9796
<artifactId>assertj-core</artifactId>
98-
<version>3.21.0</version>
97+
<version>${assertj-core.version}</version>
9998
<scope>test</scope>
10099
</dependency>
101100

102101
<dependency>
103102
<groupId>ch.qos.logback</groupId>
104103
<artifactId>logback-classic</artifactId>
105-
<version>1.3.0-alpha10</version>
104+
<version>${logback-classic.version}</version>
105+
<scope>test</scope>
106+
</dependency>
107+
108+
<dependency>
109+
<groupId>org.testng</groupId>
110+
<artifactId>testng</artifactId>
111+
<version>${testng.version}</version>
106112
<scope>test</scope>
107113
</dependency>
108114

109115
<dependency>
110116
<groupId>org.projectlombok</groupId>
111117
<artifactId>lombok</artifactId>
112-
<version>1.18.20</version>
118+
<version>${lombok.version}</version>
113119
<scope>provided</scope>
114120
</dependency>
115121
</dependencies>
@@ -130,7 +136,7 @@
130136
<plugin>
131137
<groupId>org.apache.maven.plugins</groupId>
132138
<artifactId>maven-surefire-plugin</artifactId>
133-
<version>3.0.0-M5</version>
139+
<version>${maven-surefire-plugin.version}</version>
134140
<configuration>
135141
<parallel>methods</parallel>
136142
<threadCount>10</threadCount>
@@ -139,67 +145,100 @@
139145
</suiteXmlFiles>
140146
</configuration>
141147
</plugin>
142-
143-
<plugin>
144-
<groupId>org.apache.maven.plugins</groupId>
145-
<artifactId>maven-release-plugin</artifactId>
146-
<version>3.0.0-M4</version>
147-
<configuration>
148-
<autoVersionSubmodules>true</autoVersionSubmodules>
149-
<useReleaseProfile>false</useReleaseProfile>
150-
<releaseProfiles>release</releaseProfiles>
151-
<goals>deploy</goals>
152-
<pushChanges>false</pushChanges>
153-
</configuration>
154-
</plugin>
155-
156-
<plugin>
157-
<groupId>org.apache.maven.plugins</groupId>
158-
<artifactId>maven-gpg-plugin</artifactId>
159-
<version>3.0.1</version>
160-
<executions>
161-
<execution>
162-
<id>sign-artifacts</id>
163-
<phase>verify</phase>
164-
<goals>
165-
<goal>sign</goal>
166-
</goals>
167-
</execution>
168-
</executions>
169-
</plugin>
170-
171-
<plugin>
172-
<groupId>org.apache.maven.plugins</groupId>
173-
<artifactId>maven-source-plugin</artifactId>
174-
<version>3.2.1</version>
175-
<executions>
176-
<execution>
177-
<id>attach-sources</id>
178-
<goals>
179-
<goal>jar-no-fork</goal>
180-
</goals>
181-
</execution>
182-
</executions>
183-
</plugin>
184-
185-
<plugin>
186-
<groupId>org.apache.maven.plugins</groupId>
187-
<artifactId>maven-javadoc-plugin</artifactId>
188-
<version>3.3.1</version>
189-
<executions>
190-
<execution>
191-
<id>attach-javadocs</id>
192-
<goals>
193-
<goal>jar</goal>
194-
</goals>
195-
</execution>
196-
</executions>
197-
</plugin>
148+
198149
<plugin>
199150
<groupId>org.apache.maven.plugins</groupId>
200151
<artifactId>maven-compiler-plugin</artifactId>
201-
<version>3.8.1</version>
152+
<version>${maven-compiler-plugin.version}</version>
202153
</plugin>
203154
</plugins>
204155
</build>
156+
157+
<profiles>
158+
<profile>
159+
<id>release</id>
160+
161+
<build>
162+
<plugins>
163+
<plugin>
164+
<groupId>org.apache.maven.plugins</groupId>
165+
<artifactId>maven-surefire-plugin</artifactId>
166+
<version>${maven-surefire-plugin.version}</version>
167+
<configuration>
168+
<parallel>methods</parallel>
169+
<threadCount>10</threadCount>
170+
<suiteXmlFiles>
171+
<file>testng.xml</file>
172+
</suiteXmlFiles>
173+
</configuration>
174+
</plugin>
175+
176+
<plugin>
177+
<groupId>org.apache.maven.plugins</groupId>
178+
<artifactId>maven-release-plugin</artifactId>
179+
<version>${maven-release-plugin.version}</version>
180+
<configuration>
181+
<autoVersionSubmodules>true</autoVersionSubmodules>
182+
<useReleaseProfile>false</useReleaseProfile>
183+
<releaseProfiles>release</releaseProfiles>
184+
<goals>deploy</goals>
185+
<pushChanges>false</pushChanges>
186+
</configuration>
187+
</plugin>
188+
189+
<plugin>
190+
<groupId>org.apache.maven.plugins</groupId>
191+
<artifactId>maven-gpg-plugin</artifactId>
192+
<version>${maven-gpg-plugin.version}</version>
193+
<executions>
194+
<execution>
195+
<id>sign-artifacts</id>
196+
<phase>verify</phase>
197+
<goals>
198+
<goal>sign</goal>
199+
</goals>
200+
</execution>
201+
</executions>
202+
</plugin>
203+
204+
<plugin>
205+
<groupId>org.apache.maven.plugins</groupId>
206+
<artifactId>maven-source-plugin</artifactId>
207+
<version>${maven-source-plugin.version}</version>
208+
<executions>
209+
<execution>
210+
<id>attach-sources</id>
211+
<goals>
212+
<goal>jar-no-fork</goal>
213+
</goals>
214+
</execution>
215+
</executions>
216+
</plugin>
217+
218+
<plugin>
219+
<groupId>org.apache.maven.plugins</groupId>
220+
<artifactId>maven-javadoc-plugin</artifactId>
221+
<version>${maven-javadoc-plugin.version}</version>
222+
<executions>
223+
<execution>
224+
<id>attach-javadocs</id>
225+
<goals>
226+
<goal>jar</goal>
227+
</goals>
228+
</execution>
229+
</executions>
230+
</plugin>
231+
<plugin>
232+
<groupId>org.apache.maven.plugins</groupId>
233+
<artifactId>maven-compiler-plugin</artifactId>
234+
<version>${maven-compiler-plugin.version}</version>
235+
<configuration>
236+
<source>1.8</source>
237+
<target>1.8</target>
238+
</configuration>
239+
</plugin>
240+
</plugins>
241+
</build>
242+
</profile>
243+
</profiles>
205244
</project>

src/test/java/io/github/kavahub/file/AIOFileWriterExample.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313
import io.github.kavahub.file.writer.AIOFileWriter;
1414

1515
public class AIOFileWriterExample {
16-
private static final String FILE_TO_WRITE = "fileToWrite.txt";
16+
private static final Path FILE_TO_WRITE = Paths.get("target", "fileToWrite.txt");
1717

1818
@BeforeEach
1919
public void clearUp() throws IOException {
20-
Files.deleteIfExists(Paths.get(FILE_TO_WRITE));
20+
Files.deleteIfExists(FILE_TO_WRITE);
2121
}
2222

2323
@Test
2424
public void whenWriteWithString() throws IOException {
25-
AIOFileWriter.write(Paths.get(FILE_TO_WRITE), "This is file content:你好").join();
25+
AIOFileWriter.write(FILE_TO_WRITE, "This is file content:你好").join();
2626
}
2727

2828
@Test
2929
public void whenWriteWithStringSplit() throws IOException {
3030
final String content = "This is file content:你好";
3131

32-
AIOFileWriter.write(Paths.get(FILE_TO_WRITE), String.join(System.lineSeparator(), content.split(" "))).join();
32+
AIOFileWriter.write(FILE_TO_WRITE, String.join(System.lineSeparator(), content.split(" "))).join();
3333
}
3434

3535
@Test
3636
public void whenWriteWithQueryFlatMapMerge() throws IOException {
3737
Query<String> data = Query.of("This is file content:你好")
3838
.flatMapMerge(line -> Query.of(line.split(" ")))
3939
.map((line) -> line + System.lineSeparator());
40-
AIOFileWriter.write(Paths.get(FILE_TO_WRITE), data).join();
40+
AIOFileWriter.write(FILE_TO_WRITE, data).join();
4141
}
4242

4343
@Test
@@ -53,6 +53,6 @@ public void whenWriteWithQueryFilter() throws IOException {
5353
.map(String::toUpperCase)
5454
// 加入换行符
5555
.map((line) -> line + System.lineSeparator());
56-
AIOFileWriter.write(Paths.get(FILE_TO_WRITE), reader).join();
56+
AIOFileWriter.write(FILE_TO_WRITE, reader).join();
5757
}
5858
}

0 commit comments

Comments
 (0)