mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
1186a07da5
commit
2fce7eab06
@ -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)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user