diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java index cce78d1ed..8a21d24c8 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileReader.java @@ -13,11 +13,10 @@ package org.dromara.hutool.core.io.file; import org.dromara.hutool.core.exceptions.UtilException; -import org.dromara.hutool.core.io.IORuntimeException; -import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.func.SerConsumer; import org.dromara.hutool.core.func.SerFunction; -import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.core.io.IORuntimeException; +import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.util.CharsetUtil; import java.io.*; @@ -53,7 +52,7 @@ public class FileReader extends FileWrapper { * @return FileReader */ public static FileReader of(final File file){ - return new FileReader(FileUtil.file(file), DEFAULT_CHARSET); + return new FileReader(file, DEFAULT_CHARSET); } // ------------------------------------------------------- Constructor start @@ -76,27 +75,11 @@ public class FileReader extends FileWrapper { * @throws IORuntimeException IO异常 */ public byte[] readBytes() throws IORuntimeException { - final long len = this.file.length(); - if (len >= Integer.MAX_VALUE) { - throw new IORuntimeException("File is larger then max array size"); - } - - final byte[] bytes = new byte[(int) len]; - InputStream in = null; - final int readLength; try { - in = FileUtil.getInputStream(this.file); - readLength = in.read(bytes); - if(readLength < len){ - throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", len, readLength)); - } - } catch (final Exception e) { + return Files.readAllBytes(this.file.toPath()); + } catch (final IOException e) { throw new IORuntimeException(e); - } finally { - IoUtil.closeQuietly(in); } - - return bytes; } /** @@ -106,6 +89,7 @@ public class FileReader extends FileWrapper { * @throws IORuntimeException IO异常 */ public String readString() throws IORuntimeException{ + // TODO JDK11+不再推荐使用这种方式,推荐使用Files.readString return new String(readBytes(), this.charset); } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java index c24356eb7..624632062 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileUtil.java @@ -1612,7 +1612,7 @@ public class FileUtil extends PathUtil { * @throws IORuntimeException IO异常 */ public static byte[] readBytes(final File file) throws IORuntimeException { - return org.dromara.hutool.core.io.file.FileReader.of(file).readBytes(); + return readBytes(file.toPath()); } /** diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java index ba4b2ecfd..9dd631324 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/io/FileUtilTest.java @@ -483,4 +483,10 @@ public class FileUtilTest { path = "test\\aaa.txt"; Assertions.assertFalse(FileUtil.isAbsolutePath(path)); } + + @Test + void readBytesTest() { + final byte[] bytes = FileUtil.readBytes("test.properties"); + Assertions.assertEquals(125, bytes.length); + } } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java index 2c162d329..d7b72fdee 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java @@ -51,14 +51,14 @@ class UrlDecoderTest { @Test void decodeCharSetIsNullToStrTest() { final String hello = "你好"; - final String decode = URLDecoder.decode(hello, null, true); + String decode = URLDecoder.decode(hello, null, true); Assertions.assertEquals(hello, decode); } @Test void decodeStrIsEmptyToStrTest() { final String strEmpty = ""; - final String decode = URLDecoder.decode(strEmpty, StandardCharsets.UTF_8, true); + String decode = URLDecoder.decode(strEmpty, StandardCharsets.UTF_8, true); Assertions.assertEquals(strEmpty, decode); } @@ -66,19 +66,19 @@ class UrlDecoderTest { void decodeStrWithUTF8ToStrTest() { final String exceptedDecode = "你好"; final String encode = "%E4%BD%A0%E5%A5%BD"; - final String s1 = URLDecoder.decode(encode); + String s1 = URLDecoder.decode(encode); Assertions.assertEquals(exceptedDecode, s1); - final String s2 = URLDecoder.decode(encode, StandardCharsets.UTF_8); + String s2 = URLDecoder.decode(encode, StandardCharsets.UTF_8); Assertions.assertEquals(exceptedDecode, s2); - final String s3 = URLDecoder.decode(encode, true); + String s3 = URLDecoder.decode(encode, true); Assertions.assertEquals(exceptedDecode, s3); - final String s4 = URLDecoder.decode(encode + "+", false); + String s4 = URLDecoder.decode(encode + "+", false); Assertions.assertEquals(exceptedDecode + "+", s4); - final String s5 = URLDecoder.decode(encode, StandardCharsets.UTF_8, false); + String s5 = URLDecoder.decode(encode, StandardCharsets.UTF_8, false); Assertions.assertEquals(exceptedDecode, s5); } @@ -86,10 +86,10 @@ class UrlDecoderTest { void decodeStrWithUTF8ToByteTest(){ final String exceptedDecode = "你好"; final String encode = "%E4%BD%A0%E5%A5%BD"; - final byte[] decode = URLDecoder.decode(encode.getBytes(StandardCharsets.UTF_8)); + byte[] decode = URLDecoder.decode(encode.getBytes(StandardCharsets.UTF_8)); Assertions.assertEquals(exceptedDecode, new String(decode,StandardCharsets.UTF_8)); - final byte[] decode1 = URLDecoder.decode((encode + "+").getBytes(StandardCharsets.UTF_8)); + byte[] decode1 = URLDecoder.decode((encode + "+").getBytes(StandardCharsets.UTF_8)); Assertions.assertEquals(exceptedDecode+" ",new String(decode1,StandardCharsets.UTF_8)); } }