diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9b7c1cd2c..dec1188f3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,15 @@
# Changelog
+-------------------------------------------------------------------------------------------------------------
+
+# 5.5.4 (2020-12-16)
+
+### 新特性
+### Bug修复
+* 【core 】 修复IoUtil.readBytes的问题
+
+
-------------------------------------------------------------------------------------------------------------
# 5.5.3 (2020-12-11)
diff --git a/README-EN.md b/README-EN.md
index 472d6b799..de675a27d 100644
--- a/README-EN.md
+++ b/README-EN.md
@@ -125,19 +125,19 @@ Each module can be introduced individually, or all modules can be introduced by
cn.hutool
hutool-all
- 5.5.3
+ 5.5.4
```
### Gradle
```
-compile 'cn.hutool:hutool-all:5.5.3'
+compile 'cn.hutool:hutool-all:5.5.4'
```
## Download
-- [Maven1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.3/)
-- [Maven2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.3/)
+- [Maven1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.4/)
+- [Maven2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.4/)
> note:
> Hutool 5.x supports JDK8+ and is not tested on Android platforms, and cannot guarantee that all tool classes or tool methods are available.
diff --git a/README.md b/README.md
index 76d6ef33f..5663dd161 100644
--- a/README.md
+++ b/README.md
@@ -123,21 +123,21 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不
cn.hutool
hutool-all
- 5.5.3
+ 5.5.4
```
### Gradle
```
-compile 'cn.hutool:hutool-all:5.5.3'
+compile 'cn.hutool:hutool-all:5.5.4'
```
### 非Maven项目
点击以下任一链接,下载`hutool-all-X.X.X.jar`即可:
-- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.3/)
-- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.3/)
+- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.4/)
+- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.4/)
> 注意
> Hutool 5.x支持JDK8+,对Android平台没有测试,不能保证所有工具类或工具方法可用。
diff --git a/bin/version.txt b/bin/version.txt
index d4e50692a..c8f1d098d 100755
--- a/bin/version.txt
+++ b/bin/version.txt
@@ -1 +1 @@
-5.5.3
+5.5.4
diff --git a/docs/js/version.js b/docs/js/version.js
index f133da001..44b8b9b87 100644
--- a/docs/js/version.js
+++ b/docs/js/version.js
@@ -1 +1 @@
-var version = '5.5.3'
\ No newline at end of file
+var version = '5.5.4'
\ No newline at end of file
diff --git a/hutool-all/pom.xml b/hutool-all/pom.xml
index 8c3103403..537d1e97f 100644
--- a/hutool-all/pom.xml
+++ b/hutool-all/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-all
diff --git a/hutool-aop/pom.xml b/hutool-aop/pom.xml
index 4b1c5e526..5f0a36be5 100644
--- a/hutool-aop/pom.xml
+++ b/hutool-aop/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-aop
diff --git a/hutool-bloomFilter/pom.xml b/hutool-bloomFilter/pom.xml
index 712cda75d..cac489419 100644
--- a/hutool-bloomFilter/pom.xml
+++ b/hutool-bloomFilter/pom.xml
@@ -7,7 +7,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-bloomFilter
diff --git a/hutool-bom/pom.xml b/hutool-bom/pom.xml
index 4b721b72f..969b22b13 100644
--- a/hutool-bom/pom.xml
+++ b/hutool-bom/pom.xml
@@ -7,7 +7,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-bom
diff --git a/hutool-cache/pom.xml b/hutool-cache/pom.xml
index 49ed9b7f9..e1ee87553 100644
--- a/hutool-cache/pom.xml
+++ b/hutool-cache/pom.xml
@@ -7,7 +7,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-cache
diff --git a/hutool-captcha/pom.xml b/hutool-captcha/pom.xml
index 8f52eeaf5..1e1d08f19 100644
--- a/hutool-captcha/pom.xml
+++ b/hutool-captcha/pom.xml
@@ -7,7 +7,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-captcha
diff --git a/hutool-core/pom.xml b/hutool-core/pom.xml
index 5417ec209..deccd861e 100644
--- a/hutool-core/pom.xml
+++ b/hutool-core/pom.xml
@@ -17,7 +17,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-core
diff --git a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java
index 6feba0a8f..ce5bbcc82 100644
--- a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java
@@ -43,7 +43,7 @@ import java.util.zip.Checksum;
*
* @author xiaoleilu
*/
-public class IoUtil extends NioUtil{
+public class IoUtil extends NioUtil {
// -------------------------------------------------------------------------------------- Copy start
@@ -330,7 +330,7 @@ public class IoUtil extends NioUtil{
}
/**
- * 从流中读取内容
+ * 从流中读取内容,读取完成后关闭流
*
* @param in 输入流
* @param charsetName 字符集
@@ -338,7 +338,7 @@ public class IoUtil extends NioUtil{
* @throws IORuntimeException IO异常
*/
public static String read(InputStream in, String charsetName) throws IORuntimeException {
- FastByteArrayOutputStream out = read(in);
+ final FastByteArrayOutputStream out = read(in);
return StrUtil.isBlank(charsetName) ? out.toString() : out.toString(charsetName);
}
@@ -362,8 +362,27 @@ public class IoUtil extends NioUtil{
* @throws IORuntimeException IO异常
*/
public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException {
+ return read(in, true);
+ }
+
+ /**
+ * 从流中读取内容,读到输出流中,读取完毕后并不关闭流
+ *
+ * @param in 输入流
+ * @param isClose 读取完毕后是否关闭流
+ * @return 输出流
+ * @throws IORuntimeException IO异常
+ * @since 5.5.3
+ */
+ public static FastByteArrayOutputStream read(InputStream in, boolean isClose) throws IORuntimeException {
final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
- copy(in, out);
+ try {
+ copy(in, out);
+ } finally {
+ if (isClose) {
+ close(in);
+ }
+ }
return out;
}
@@ -417,26 +436,31 @@ public class IoUtil extends NioUtil{
/**
* 从流中读取bytes
*
- * @param in {@link InputStream}
- * @param isCloseStream 是否关闭输入流
+ * @param in {@link InputStream}
+ * @param isCLose 是否关闭输入流
* @return bytes
* @throws IORuntimeException IO异常
* @since 5.0.4
*/
- public static byte[] readBytes(InputStream in, boolean isCloseStream) throws IORuntimeException {
- final InputStream availableStream = toAvailableStream(in);
- try{
- final int available = availableStream.available();
- if(available > 0){
- byte[] result = new byte[available];
- //noinspection ResultOfMethodCallIgnored
- availableStream.read(result);
- return result;
+ public static byte[] readBytes(InputStream in, boolean isCLose) throws IORuntimeException {
+ if (in instanceof FileInputStream) {
+ // 文件流的长度是可预见的,此时直接读取效率更高
+ final byte[] result;
+ try {
+ final int available = in.available();
+ result = new byte[available];
+ final int readLength = in.read(result);
+ if (readLength != available) {
+ throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", available, readLength));
+ }
+ } catch (IOException e) {
+ throw new IORuntimeException(e);
}
- } catch (IOException e){
- throw new IORuntimeException(e);
+ return result;
}
- return new byte[0];
+
+ // 未知bytes总量的流
+ return read(in, isCLose).toByteArray();
}
/**
@@ -804,6 +828,7 @@ public class IoUtil extends NioUtil{
* 将指定{@link InputStream} 转换为{@link InputStream#available()}方法可用的流。
* 在Socket通信流中,服务端未返回数据情况下{@link InputStream#available()}方法始终为{@code 0}
* 因此,在读取前需要调用{@link InputStream#read()}读取一个字节(未返回会阻塞),一旦读取到了,{@link InputStream#available()}方法就正常了。
+ * 需要注意的是,在网络流中,是按照块来传输的,所以 {@link InputStream#available()} 读取到的并非最终长度,而是此次块的长度。
* 此方法返回对象的规则为:
*
*
@@ -816,7 +841,7 @@ public class IoUtil extends NioUtil{
* @since 5.5.3
*/
public static InputStream toAvailableStream(InputStream in) {
- if(in instanceof FileInputStream){
+ if (in instanceof FileInputStream) {
// FileInputStream本身支持available方法。
return in;
}
diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java
index e9877d575..cd737c9be 100644
--- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java
@@ -383,6 +383,21 @@ public class PathUtil {
return IoUtil.getReader(getInputStream(path), charset);
}
+ /**
+ * 读取文件的所有内容为byte数组
+ *
+ * @param path 文件
+ * @return byte数组
+ * @since 5.5.4
+ */
+ public static byte[] readBytes(Path path){
+ try {
+ return Files.readAllBytes(path);
+ } catch (IOException e) {
+ throw new IORuntimeException(e);
+ }
+ }
+
/**
* 获得输出流
*
diff --git a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java
new file mode 100644
index 000000000..db9d35a31
--- /dev/null
+++ b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java
@@ -0,0 +1,14 @@
+package cn.hutool.core.io;
+
+import cn.hutool.core.io.resource.ResourceUtil;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class IoUtilTest {
+
+ @Test
+ public void readBytesTest(){
+ final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg"));
+ Assert.assertEquals(22807, bytes.length);
+ }
+}
diff --git a/hutool-cron/pom.xml b/hutool-cron/pom.xml
index ea131479a..199014a90 100644
--- a/hutool-cron/pom.xml
+++ b/hutool-cron/pom.xml
@@ -7,7 +7,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-cron
diff --git a/hutool-crypto/pom.xml b/hutool-crypto/pom.xml
index e2ed08f64..7f20e5415 100644
--- a/hutool-crypto/pom.xml
+++ b/hutool-crypto/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-crypto
diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml
index cf326dac6..0000e4a96 100644
--- a/hutool-db/pom.xml
+++ b/hutool-db/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-db
diff --git a/hutool-dfa/pom.xml b/hutool-dfa/pom.xml
index 34060c810..8aa56dbed 100644
--- a/hutool-dfa/pom.xml
+++ b/hutool-dfa/pom.xml
@@ -7,7 +7,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-dfa
diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml
index 91b17ef79..462d16f71 100644
--- a/hutool-extra/pom.xml
+++ b/hutool-extra/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-extra
diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml
index 136dce963..7518587a1 100644
--- a/hutool-http/pom.xml
+++ b/hutool-http/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-http
diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml
index 08a235a7a..7b0401562 100644
--- a/hutool-json/pom.xml
+++ b/hutool-json/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-json
diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml
index dee06f766..78e10dfab 100644
--- a/hutool-log/pom.xml
+++ b/hutool-log/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-log
diff --git a/hutool-poi/pom.xml b/hutool-poi/pom.xml
index 8a53c9df4..0f06fb0bb 100644
--- a/hutool-poi/pom.xml
+++ b/hutool-poi/pom.xml
@@ -8,7 +8,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-poi
diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml
index d9ba86df5..60c38c5b8 100644
--- a/hutool-script/pom.xml
+++ b/hutool-script/pom.xml
@@ -8,7 +8,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-script
diff --git a/hutool-setting/pom.xml b/hutool-setting/pom.xml
index 418fe7227..975b734c7 100644
--- a/hutool-setting/pom.xml
+++ b/hutool-setting/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-setting
diff --git a/hutool-socket/pom.xml b/hutool-socket/pom.xml
index c1b37b679..0c565c437 100644
--- a/hutool-socket/pom.xml
+++ b/hutool-socket/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-socket
diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml
index 39fa8e271..4d88fb9e5 100644
--- a/hutool-system/pom.xml
+++ b/hutool-system/pom.xml
@@ -9,7 +9,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool-system
diff --git a/pom.xml b/pom.xml
index 0f479c9ee..8561b4443 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,7 +8,7 @@
cn.hutool
hutool-parent
- 5.5.3
+ 5.5.4
hutool
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
https://github.com/looly/hutool