mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user