This commit is contained in:
Looly
2023-03-10 13:07:05 +08:00
parent 869b12000d
commit 35feac4eae
13 changed files with 123 additions and 336 deletions

View File

@@ -4,12 +4,12 @@ import cn.hutool.core.compress.ZipUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.*;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.io.file.FileReader.ReaderHandler;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.io.stream.BOMInputStream;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.lang.func.SerFunction;
import cn.hutool.core.net.url.URLUtil;
import cn.hutool.core.reflect.ClassUtil;
import cn.hutool.core.regex.ReUtil;
@@ -1976,10 +1976,9 @@ public class FileUtil extends PathUtil {
* @param path 文件的绝对路径
* @return 从文件中load出的数据
* @throws IORuntimeException IO异常
* @since 3.1.1
*/
public static <T> T loadUtf8(final String path, final ReaderHandler<T> readerHandler) throws IORuntimeException {
return load(path, CharsetUtil.UTF_8, readerHandler);
public static <T> T readUtf8(final String path, final SerFunction<BufferedReader, T> readerHandler) throws IORuntimeException {
return read(path, CharsetUtil.UTF_8, readerHandler);
}
/**
@@ -1993,23 +1992,8 @@ public class FileUtil extends PathUtil {
* @throws IORuntimeException IO异常
* @since 3.1.1
*/
public static <T> T load(final String path, final String charset, final ReaderHandler<T> readerHandler) throws IORuntimeException {
return FileReader.of(file(path), CharsetUtil.charset(charset)).read(readerHandler);
}
/**
* 按照给定的readerHandler读取文件中的数据
*
* @param <T> 集合类型
* @param readerHandler Reader处理类
* @param path 文件的绝对路径
* @param charset 字符集
* @return 从文件中load出的数据
* @throws IORuntimeException IO异常
* @since 3.1.1
*/
public static <T> T load(final String path, final Charset charset, final ReaderHandler<T> readerHandler) throws IORuntimeException {
return FileReader.of(file(path), charset).read(readerHandler);
public static <T> T read(final String path, final Charset charset, final SerFunction<BufferedReader, T> readerHandler) throws IORuntimeException {
return read(file(path), charset, readerHandler);
}
/**
@@ -2022,8 +2006,8 @@ public class FileUtil extends PathUtil {
* @throws IORuntimeException IO异常
* @since 3.1.1
*/
public static <T> T loadUtf8(final File file, final ReaderHandler<T> readerHandler) throws IORuntimeException {
return load(file, CharsetUtil.UTF_8, readerHandler);
public static <T> T readUtf8(final File file, final SerFunction<BufferedReader, T> readerHandler) throws IORuntimeException {
return read(file, CharsetUtil.UTF_8, readerHandler);
}
/**
@@ -2037,7 +2021,7 @@ public class FileUtil extends PathUtil {
* @throws IORuntimeException IO异常
* @since 3.1.1
*/
public static <T> T load(final File file, final Charset charset, final ReaderHandler<T> readerHandler) throws IORuntimeException {
public static <T> T read(final File file, final Charset charset, final SerFunction<BufferedReader, T> readerHandler) throws IORuntimeException {
return FileReader.of(file, charset).read(readerHandler);
}

View File

@@ -1,9 +1,11 @@
package cn.hutool.core.io.file;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.func.SerConsumer;
import cn.hutool.core.lang.func.SerFunction;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.CharsetUtil;
@@ -17,6 +19,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
/**
* 文件读取器
@@ -207,14 +210,20 @@ public class FileReader extends FileWrapper {
* @return 从文件中read出的数据
* @throws IORuntimeException IO异常
*/
public <T> T read(final ReaderHandler<T> readerHandler) throws IORuntimeException {
public <T> T read(final SerFunction<BufferedReader, T> readerHandler) throws IORuntimeException {
BufferedReader reader = null;
T result;
try {
reader = FileUtil.getReader(this.file, charset);
result = readerHandler.handle(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
result = readerHandler.applying(reader);
} catch (final Exception e) {
if(e instanceof IOException){
throw new IORuntimeException(e);
} else if(e instanceof RuntimeException){
throw (RuntimeException)e;
} else{
throw new UtilException(e);
}
} finally {
IoUtil.close(reader);
}
@@ -277,19 +286,6 @@ public class FileReader extends FileWrapper {
}
}
// -------------------------------------------------------------------------- Interface start
/**
* Reader处理接口
*
* @author Luxiaolei
*
* @param <T> Reader处理返回结果类型
*/
public interface ReaderHandler<T> {
T handle(BufferedReader reader) throws IOException;
}
// -------------------------------------------------------------------------- Interface end
/**
* 检查文件
*

View File

@@ -1,12 +1,14 @@
package cn.hutool.core.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.BiMap;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrUtil;
@@ -181,7 +183,8 @@ public class XmlUtil {
* @return XML文档对象
* @since 3.0.9
*/
public static Document readXML(final String pathOrContent) {
public static Document readXML(String pathOrContent) {
pathOrContent = StrUtil.trim(pathOrContent);
if (StrUtil.startWith(pathOrContent, '<')) {
return parseXml(pathOrContent);
}
@@ -985,11 +988,7 @@ public class XmlUtil {
public static <T> T xmlToBean(final Node node, final Class<T> bean) {
final Map<String, Object> map = xmlToMap(node);
if (null != map && map.size() == 1) {
final String simpleName = bean.getSimpleName();
if (map.containsKey(simpleName)) {
// 只有key和bean的名称匹配时才做单一对象转换
return BeanUtil.toBean(map.get(simpleName), bean);
}
return BeanUtil.toBean(CollUtil.get(map.values(), 0), bean);
}
return BeanUtil.toBean(map, bean);
}

View File

@@ -7,6 +7,7 @@ import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -71,6 +72,7 @@ public class LambdaFactoryTest {
* @author nasodaengineer
*/
@RunWith(Parameterized.class)
@Ignore
public static class PerformanceTest {
@Parameterized.Parameter
@@ -140,6 +142,7 @@ public class LambdaFactoryTest {
@SuppressWarnings({"rawtypes", "unchecked", "Convert2MethodRef"})
@Test
@SneakyThrows
@Ignore
public void lambdaGetPerformanceTest() {
final Something something = new Something();
something.setId(1L);
@@ -216,6 +219,7 @@ public class LambdaFactoryTest {
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
@SneakyThrows
@Ignore
public void lambdaSetPerformanceTest() {
final Something something = new Something();
something.setId(1L);

View File

@@ -348,4 +348,23 @@ public class XmlUtilTest {
Console.log(XmlUtil.format(doc));
}
@Test
public void xmlStrToBeanTest(){
final String xml = "<user><name>张三</name><age>20</age><email>zhangsan@example.com</email></user>";
final Document document = XmlUtil.readXML(xml);
final UserInfo userInfo = XmlUtil.xmlToBean(document, UserInfo.class);
Assert.assertEquals("张三", userInfo.getName());
Assert.assertEquals("20", userInfo.getAge());
Assert.assertEquals("zhangsan@example.com", userInfo.getEmail());
}
@Data
static class UserInfo {
private String id;
private String name;
private String age;
private String email;
}
}