Skip to content

Commit

Permalink
nio enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Aug 1, 2020
1 parent f527c7a commit 04917da
Show file tree
Hide file tree
Showing 38 changed files with 422 additions and 420 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@

-------------------------------------------------------------------------------------------------------------

## 5.3.11 (2020-08-01)
# 5.4.0 (2020-08-01)

### 新特性
* 【socket】 对NioServer和NioClient改造(pr#992@Github)

### Bug修复#

-------------------------------------------------------------------------------------------------------------

# 5.3.11 (2020-08-01)

### 新特性
* 【captcha】 AbstractCaptcha增加getImageBase64Data方法(pr#985@Github)
Expand All @@ -13,7 +22,7 @@
* 【core 】 MapUtil增加getXXX的默认值重载(issue#I1PTGI@Gitee)
* 【core 】 CalendarUtil增加parseByPatterns方法(issue#993@Github)

### Bug修复
### Bug修复#

-------------------------------------------------------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,21 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.11</version>
<version>5.4.0</version>
</dependency>
```

### Gradle
```
compile 'cn.hutool:hutool-all:5.3.11'
compile 'cn.hutool:hutool-all:5.4.0'
```

### 非Maven项目

点击以下任一链接,下载`hutool-all-X.X.X.jar`即可:

- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.3.11/)
- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.3.11/)
- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/)
- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.4.0/)

> 注意
> Hutool 5.x支持JDK8+,对Android平台没有测试,不能保证所有工具类或工具方法可用。
Expand Down
2 changes: 1 addition & 1 deletion bin/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.11
5.4.0
2 changes: 1 addition & 1 deletion docs/js/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var version = '5.3.11'
var version = '5.4.0'
2 changes: 1 addition & 1 deletion hutool-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-all</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-aop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-aop</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-bloomFilter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-bloomFilter</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-bom</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-cache</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-captcha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-captcha</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-core</artifactId>
Expand Down
6 changes: 5 additions & 1 deletion hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,11 @@ public static byte[] bytes(CharSequence str, Charset charset) {

/**
* 将对象转为字符串<br>
* 1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
*
* <pre>
* 1、Byte数组和ByteBuffer会被转换为对应字符串的数组
* 2、对象数组会调用Arrays.toString方法
* </pre>
*
* @param obj 对象
* @return 字符串
Expand Down
2 changes: 1 addition & 1 deletion hutool-cron/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-cron</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-crypto</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-db/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-db</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-dfa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-dfa</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-extra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-extra</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-http</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-json</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-log</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-poi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-poi</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-script/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-script</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-setting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-setting</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion hutool-socket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.3.11-SNAPSHOT</version>
<version>5.4.0-SNAPSHOT</version>
</parent>

<artifactId>hutool-socket</artifactId>
Expand Down
25 changes: 10 additions & 15 deletions hutool-socket/src/main/java/cn/hutool/socket/aio/AioServer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package cn.hutool.socket.aio;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketOption;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadFactoryBuilder;
Expand All @@ -16,6 +8,14 @@
import cn.hutool.log.LogFactory;
import cn.hutool.socket.SocketConfig;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketOption;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;

/**
* 基于AIO的Socket服务端实现
*
Expand Down Expand Up @@ -76,11 +76,7 @@ public AioServer init(InetSocketAddress address) {
* @param sync 是否阻塞
*/
public void start(boolean sync) {
try {
doStart(sync);
} catch (IOException e) {
throw new IORuntimeException(e);
}
doStart(sync);
}

/**
Expand Down Expand Up @@ -173,9 +169,8 @@ public void close() {
* 开始监听
*
* @param sync 是否阻塞
* @throws IOException IO异常
*/
private void doStart(boolean sync) throws IOException {
private void doStart(boolean sync) {
log.debug("Aio Server started, waiting for accept.");

// 接收客户端连接
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.hutool.socket.nio;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.log.StaticLog;

import java.io.IOException;
import java.nio.channels.CompletionHandler;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
* 接入完成回调,单例使用
*
* @author looly
*/
public class AcceptHandler implements CompletionHandler<ServerSocketChannel, NioServer> {

@Override
public void completed(ServerSocketChannel serverSocketChannel, NioServer nioServer) {
SocketChannel socketChannel;
try {
// 获取连接到此服务器的客户端通道
socketChannel = serverSocketChannel.accept();
StaticLog.debug("Client [{}] accepted.", socketChannel.getRemoteAddress());
} catch (IOException e) {
throw new IORuntimeException(e);
}

// SocketChannel通道的可读事件注册到Selector中
NioUtil.registerChannel(nioServer.getSelector(), socketChannel, Operation.READ);
}

@Override
public void failed(Throwable exc, NioServer nioServer) {
StaticLog.error(exc);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.hutool.socket.nio;

import java.nio.channels.SocketChannel;

/**
* NIO数据处理接口,通过实现此接口,可以从{@link SocketChannel}中读写数据
*
*/
public interface ChannelHandler {

/**
* 处理NIO数据
*
* @param socketChannel {@link SocketChannel}
*/
void handle(SocketChannel socketChannel);
}
Loading

0 comments on commit 04917da

Please sign in to comment.