1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00
This commit is contained in:
Looly 2022-04-01 11:34:30 +08:00
parent 1186a07da5
commit 2fce7eab06
3 changed files with 18 additions and 19 deletions
CHANGELOG.md
hutool-core/src
main/java/cn/hutool/core/io
test/java/cn/hutool/core/io

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.0.M2 (2022-03-31)
# 5.8.0.M2 (2022-04-01)
### ❌不兼容特性
* 【extra 】 【可能兼容问题】BeanCopierCache的key结构变更
@ -20,6 +20,7 @@
### 🐞Bug修复
* 【core 】 IdcardUtil#getCityCodeByIdCard位数问题issue#2224@Github
* 【core 】 修复urlWithParamIfGet函数逻辑问题issue#I50IUD@Gitee
* 【core 】 修复IoUtil.readBytes限制长度读取问题issue#2230@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -498,7 +498,7 @@ public class IoUtil extends NioUtil {
/**
* 读取指定长度的byte数组不关闭流
*
* @param in {@link InputStream}null返回null
* @param in {@link InputStream}{@code null}返回{@code null}
* @param length 长度小于等于0返回空byte数组
* @return bytes
* @throws IORuntimeException IO异常
@ -511,20 +511,9 @@ public class IoUtil extends NioUtil {
return new byte[0];
}
byte[] b = new byte[length];
int readLength;
try {
readLength = in.read(b);
} catch (IOException e) {
throw new IORuntimeException(e);
}
if (readLength > 0 && readLength < length) {
byte[] b2 = new byte[readLength];
System.arraycopy(b, 0, b2, 0, readLength);
return b2;
} else {
return b;
}
final FastByteArrayOutputStream out = new FastByteArrayOutputStream(length);
copy(in, out, DEFAULT_BUFFER_SIZE, length, null);
return out.toByteArray();
}
/**

View File

@ -1,6 +1,7 @@
package cn.hutool.core.io;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
@ -10,14 +11,22 @@ import java.io.IOException;
public class IoUtilTest {
@Test
public void readBytesTest(){
public void readBytesTest() {
final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg"));
Assert.assertEquals(22807, bytes.length);
}
@Test
public void readLinesTest(){
try(BufferedReader reader = ResourceUtil.getUtf8Reader("test_lines.csv");){
public void readBytesWithLengthTest() {
// 读取固定长度
final int limit = RandomUtil.randomInt(22807);
final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg"), limit);
Assert.assertEquals(limit, bytes.length);
}
@Test
public void readLinesTest() {
try (BufferedReader reader = ResourceUtil.getUtf8Reader("test_lines.csv");) {
IoUtil.readLines(reader, (LineHandler) Assert::assertNotNull);
} catch (IOException e) {
throw new IORuntimeException(e);