From 2fce7eab06e168c96b999d3069d9496f31723de5 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 1 Apr 2022 11:34:30 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 3 ++- .../main/java/cn/hutool/core/io/IoUtil.java | 19 ++++--------------- .../java/cn/hutool/core/io/IoUtilTest.java | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac3e4c630..418be3f2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java index b9b3ecdda..b259bbdde 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java @@ -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(); } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java index c649d7659..c0caa2500 100644 --- a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java @@ -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);