mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
bd5160e8d0
commit
aba953cfd2
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user