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

View File

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

View File

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

View File

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