This commit is contained in:
Looly 2023-04-18 17:45:10 +08:00
parent bd5160e8d0
commit aba953cfd2
4 changed files with 22 additions and 32 deletions

View File

@ -13,11 +13,10 @@
package org.dromara.hutool.core.io.file; package org.dromara.hutool.core.io.file;
import org.dromara.hutool.core.exceptions.UtilException; 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.SerConsumer;
import org.dromara.hutool.core.func.SerFunction; 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 org.dromara.hutool.core.util.CharsetUtil;
import java.io.*; import java.io.*;
@ -53,7 +52,7 @@ public class FileReader extends FileWrapper {
* @return FileReader * @return FileReader
*/ */
public static FileReader of(final File file){ public static FileReader of(final File file){
return new FileReader(FileUtil.file(file), DEFAULT_CHARSET); return new FileReader(file, DEFAULT_CHARSET);
} }
// ------------------------------------------------------- Constructor start // ------------------------------------------------------- Constructor start
@ -76,27 +75,11 @@ public class FileReader extends FileWrapper {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public byte[] readBytes() throws IORuntimeException { 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 { try {
in = FileUtil.getInputStream(this.file); return Files.readAllBytes(this.file.toPath());
readLength = in.read(bytes); } catch (final IOException e) {
if(readLength < len){
throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", len, readLength));
}
} catch (final Exception e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} finally {
IoUtil.closeQuietly(in);
} }
return bytes;
} }
/** /**
@ -106,6 +89,7 @@ public class FileReader extends FileWrapper {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public String readString() throws IORuntimeException{ public String readString() throws IORuntimeException{
// TODO JDK11+不再推荐使用这种方式推荐使用Files.readString
return new String(readBytes(), this.charset); return new String(readBytes(), this.charset);
} }

View File

@ -1612,7 +1612,7 @@ public class FileUtil extends PathUtil {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
public static byte[] readBytes(final File file) throws IORuntimeException { public static byte[] readBytes(final File file) throws IORuntimeException {
return org.dromara.hutool.core.io.file.FileReader.of(file).readBytes(); return readBytes(file.toPath());
} }
/** /**

View File

@ -483,4 +483,10 @@ public class FileUtilTest {
path = "test\\aaa.txt"; path = "test\\aaa.txt";
Assertions.assertFalse(FileUtil.isAbsolutePath(path)); Assertions.assertFalse(FileUtil.isAbsolutePath(path));
} }
@Test
void readBytesTest() {
final byte[] bytes = FileUtil.readBytes("test.properties");
Assertions.assertEquals(125, bytes.length);
}
} }

View File

@ -51,14 +51,14 @@ class UrlDecoderTest {
@Test @Test
void decodeCharSetIsNullToStrTest() { void decodeCharSetIsNullToStrTest() {
final String hello = "你好"; final String hello = "你好";
final String decode = URLDecoder.decode(hello, null, true); String decode = URLDecoder.decode(hello, null, true);
Assertions.assertEquals(hello, decode); Assertions.assertEquals(hello, decode);
} }
@Test @Test
void decodeStrIsEmptyToStrTest() { void decodeStrIsEmptyToStrTest() {
final String strEmpty = ""; 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); Assertions.assertEquals(strEmpty, decode);
} }
@ -66,19 +66,19 @@ class UrlDecoderTest {
void decodeStrWithUTF8ToStrTest() { void decodeStrWithUTF8ToStrTest() {
final String exceptedDecode = "你好"; final String exceptedDecode = "你好";
final String encode = "%E4%BD%A0%E5%A5%BD"; final String encode = "%E4%BD%A0%E5%A5%BD";
final String s1 = URLDecoder.decode(encode); String s1 = URLDecoder.decode(encode);
Assertions.assertEquals(exceptedDecode, s1); 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); Assertions.assertEquals(exceptedDecode, s2);
final String s3 = URLDecoder.decode(encode, true); String s3 = URLDecoder.decode(encode, true);
Assertions.assertEquals(exceptedDecode, s3); Assertions.assertEquals(exceptedDecode, s3);
final String s4 = URLDecoder.decode(encode + "+", false); String s4 = URLDecoder.decode(encode + "+", false);
Assertions.assertEquals(exceptedDecode + "+", s4); 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); Assertions.assertEquals(exceptedDecode, s5);
} }
@ -86,10 +86,10 @@ class UrlDecoderTest {
void decodeStrWithUTF8ToByteTest(){ void decodeStrWithUTF8ToByteTest(){
final String exceptedDecode = "你好"; final String exceptedDecode = "你好";
final String encode = "%E4%BD%A0%E5%A5%BD"; 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)); 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)); Assertions.assertEquals(exceptedDecode+" ",new String(decode1,StandardCharsets.UTF_8));
} }
} }