Looly 5 年之前
父节点
当前提交
f47de0590d

+ 9 - 0
CHANGELOG.md

@@ -3,6 +3,15 @@
 
 -------------------------------------------------------------------------------------------------------------
 
+# 5.5.4 (2020-12-16)
+
+### 新特性
+### Bug修复
+* 【core   】     修复IoUtil.readBytes的问题
+
+
+-------------------------------------------------------------------------------------------------------------
+
 # 5.5.3 (2020-12-11)
 
 ### 新特性

+ 4 - 4
README-EN.md

@@ -125,19 +125,19 @@ Each module can be introduced individually, or all modules can be introduced by
 <dependency>
     <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
-    <version>5.5.3</version>
+    <version>5.5.4</version>
 </dependency>
 ```
 
 ### 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.

+ 4 - 4
README.md

@@ -123,21 +123,21 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不
 <dependency>
     <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
-    <version>5.5.3</version>
+    <version>5.5.4</version>
 </dependency>
 ```
 
 ### 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平台没有测试,不能保证所有工具类或工具方法可用。

+ 1 - 1
bin/version.txt

@@ -1 +1 @@
-5.5.3
+5.5.4

+ 1 - 1
docs/js/version.js

@@ -1 +1 @@
-var version = '5.5.3'
+var version = '5.5.4'

+ 1 - 1
hutool-all/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-all</artifactId>

+ 1 - 1
hutool-aop/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-aop</artifactId>

+ 1 - 1
hutool-bloomFilter/pom.xml

@@ -7,7 +7,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-bloomFilter</artifactId>

+ 1 - 1
hutool-bom/pom.xml

@@ -7,7 +7,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-bom</artifactId>

+ 1 - 1
hutool-cache/pom.xml

@@ -7,7 +7,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-cache</artifactId>

+ 1 - 1
hutool-captcha/pom.xml

@@ -7,7 +7,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-captcha</artifactId>

+ 1 - 1
hutool-core/pom.xml

@@ -17,7 +17,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-core</artifactId>

+ 44 - 19
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()}方法可用的流。<br>
 	 * 在Socket通信流中,服务端未返回数据情况下{@link InputStream#available()}方法始终为{@code 0}<br>
 	 * 因此,在读取前需要调用{@link InputStream#read()}读取一个字节(未返回会阻塞),一旦读取到了,{@link InputStream#available()}方法就正常了。<br>
+	 * 需要注意的是,在网络流中,是按照块来传输的,所以 {@link InputStream#available()} 读取到的并非最终长度,而是此次块的长度。<br>
 	 * 此方法返回对象的规则为:
 	 *
 	 * <ul>
@@ -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;
 		}

+ 15 - 0
hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java

@@ -384,6 +384,21 @@ public class PathUtil {
 	}
 
 	/**
+	 * 读取文件的所有内容为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);
+		}
+	}
+
+	/**
 	 * 获得输出流
 	 *
 	 * @param path Path

+ 14 - 0
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);
+	}
+}

+ 1 - 1
hutool-cron/pom.xml

@@ -7,7 +7,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-cron</artifactId>

+ 1 - 1
hutool-crypto/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-crypto</artifactId>

+ 1 - 1
hutool-db/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-db</artifactId>

+ 1 - 1
hutool-dfa/pom.xml

@@ -7,7 +7,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-dfa</artifactId>

+ 1 - 1
hutool-extra/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-extra</artifactId>

+ 1 - 1
hutool-http/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-http</artifactId>

+ 1 - 1
hutool-json/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-json</artifactId>

+ 1 - 1
hutool-log/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-log</artifactId>

+ 1 - 1
hutool-poi/pom.xml

@@ -8,7 +8,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-poi</artifactId>

+ 1 - 1
hutool-script/pom.xml

@@ -8,7 +8,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-script</artifactId>

+ 1 - 1
hutool-setting/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-setting</artifactId>

+ 1 - 1
hutool-socket/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-socket</artifactId>

+ 1 - 1
hutool-system/pom.xml

@@ -9,7 +9,7 @@
 	<parent>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-parent</artifactId>
-		<version>5.5.3</version>
+		<version>5.5.4</version>
 	</parent>
 
 	<artifactId>hutool-system</artifactId>

+ 1 - 1
pom.xml

@@ -8,7 +8,7 @@
 
 	<groupId>cn.hutool</groupId>
 	<artifactId>hutool-parent</artifactId>
-	<version>5.5.3</version>
+	<version>5.5.4</version>
 	<name>hutool</name>
 	<description>Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。</description>
 	<url>https://github.com/looly/hutool</url>