浏览代码

add methods

Looly 6 年之前
父节点
当前提交
14c343fefd

+ 3 - 0
CHANGELOG.md

@@ -12,6 +12,9 @@
 * 【core 】      CaseInsensitiveMap/CamelCaseMap增加toString(issue#636@Github)
 * 【core 】      CaseInsensitiveMap/CamelCaseMap增加toString(issue#636@Github)
 * 【core 】      XmlUtil多节点改进(issue#I15I0R@Gitee)
 * 【core 】      XmlUtil多节点改进(issue#I15I0R@Gitee)
 * 【core 】      Thread.excAsync修正为execAsync(issue#642@Github)
 * 【core 】      Thread.excAsync修正为execAsync(issue#642@Github)
+* 【core 】      FileUtil.getAbsolutePath修正正则(issue#648@Github)
+* 【core 】      NetUtil增加getNetworkInterface方法(issue#I15WEL@Gitee)
+* 【core 】      增加ReflectUtil.getFieldMap方法(issue#I15WJ7@Gitee)
 
 
 ### Bug修复
 ### Bug修复
 * 【extra】      修复SFTP.upload上传失败的问题(issue#I15O40@Gitee)
 * 【extra】      修复SFTP.upload上传失败的问题(issue#I15O40@Gitee)

+ 59 - 10
hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java

@@ -1,23 +1,65 @@
 package cn.hutool.core.io;
 package cn.hutool.core.io;
 
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.file.FileCopier;
+import cn.hutool.core.io.file.FileMode;
 import cn.hutool.core.io.file.FileReader;
 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.file.FileReader.ReaderHandler;
+import cn.hutool.core.io.file.FileWriter;
+import cn.hutool.core.io.file.LineSeparator;
+import cn.hutool.core.io.file.Tailer;
 import cn.hutool.core.io.resource.ResourceUtil;
 import cn.hutool.core.io.resource.ResourceUtil;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.lang.Assert;
-import cn.hutool.core.util.*;
-
-import java.io.*;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.CharUtil;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.core.util.ClassUtil;
+import cn.hutool.core.util.ReUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.core.util.URLUtil;
+import cn.hutool.core.util.ZipUtil;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.RandomAccessFile;
+import java.io.Reader;
 import java.net.URI;
 import java.net.URI;
 import java.net.URL;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLConnection;
 import java.nio.charset.Charset;
 import java.nio.charset.Charset;
-import java.nio.file.*;
+import java.nio.file.CopyOption;
+import java.nio.file.DirectoryStream;
+import java.nio.file.FileVisitOption;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.text.DecimalFormat;
 import java.text.DecimalFormat;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.EnumSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
 import java.util.jar.JarFile;
 import java.util.jar.JarFile;
 import java.util.regex.Pattern;
 import java.util.regex.Pattern;
 import java.util.zip.CRC32;
 import java.util.zip.CRC32;
@@ -562,8 +604,8 @@ public class FileUtil {
 			if (ArrayUtil.isEmpty(subFiles)) {
 			if (ArrayUtil.isEmpty(subFiles)) {
 				return 0L;// empty directory
 				return 0L;// empty directory
 			}
 			}
-			for (int i = 0; i < subFiles.length; i++) {
-				size += size(subFiles[i]);
+			for (File subFile : subFiles) {
+				size += size(subFile);
 			}
 			}
 			return size;
 			return size;
 		} else {
 		} else {
@@ -629,6 +671,7 @@ public class FileUtil {
 		if (false == file.exists()) {
 		if (false == file.exists()) {
 			mkParentDirs(file);
 			mkParentDirs(file);
 			try {
 			try {
+				//noinspection ResultOfMethodCallIgnored
 				file.createNewFile();
 				file.createNewFile();
 			} catch (Exception e) {
 			} catch (Exception e) {
 				throw new IORuntimeException(e);
 				throw new IORuntimeException(e);
@@ -672,6 +715,7 @@ public class FileUtil {
 	public static File mkParentDirs(File file) {
 	public static File mkParentDirs(File file) {
 		final File parentFile = file.getParentFile();
 		final File parentFile = file.getParentFile();
 		if (null != parentFile && false == parentFile.exists()) {
 		if (null != parentFile && false == parentFile.exists()) {
+			//noinspection ResultOfMethodCallIgnored
 			parentFile.mkdirs();
 			parentFile.mkdirs();
 		}
 		}
 		return parentFile;
 		return parentFile;
@@ -835,6 +879,7 @@ public class FileUtil {
 		final File[] files = directory.listFiles();
 		final File[] files = directory.listFiles();
 		if (ArrayUtil.isEmpty(files)) {
 		if (ArrayUtil.isEmpty(files)) {
 			// 空文件夹则删除之
 			// 空文件夹则删除之
+			//noinspection ResultOfMethodCallIgnored
 			directory.delete();
 			directory.delete();
 		} else {
 		} else {
 			for (File childFile : files) {
 			for (File childFile : files) {
@@ -871,6 +916,7 @@ public class FileUtil {
 			return null;
 			return null;
 		}
 		}
 		if (false == dir.exists()) {
 		if (false == dir.exists()) {
+			//noinspection ResultOfMethodCallIgnored
 			dir.mkdirs();
 			dir.mkdirs();
 		}
 		}
 		return dir;
 		return dir;
@@ -918,7 +964,9 @@ public class FileUtil {
 			try {
 			try {
 				File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile();
 				File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile();
 				if (isReCreat) {
 				if (isReCreat) {
+					//noinspection ResultOfMethodCallIgnored
 					file.delete();
 					file.delete();
+					//noinspection ResultOfMethodCallIgnored
 					file.createNewFile();
 					file.createNewFile();
 				}
 				}
 				return file;
 				return file;
@@ -1083,6 +1131,7 @@ public class FileUtil {
 		}
 		}
 
 
 		if (isOverride && dest.isFile()) {// 只有目标为文件的情况下覆盖之
 		if (isOverride && dest.isFile()) {// 只有目标为文件的情况下覆盖之
+			//noinspection ResultOfMethodCallIgnored
 			dest.delete();
 			dest.delete();
 		}
 		}
 
 
@@ -1238,7 +1287,7 @@ public class FileUtil {
 		}
 		}
 
 
 		// 给定的路径已经是绝对路径了
 		// 给定的路径已经是绝对路径了
-		return StrUtil.C_SLASH == path.charAt(0) || path.matches("^[a-zA-Z]:[/\\\\].*");
+		return StrUtil.C_SLASH == path.charAt(0) || path.matches("^[a-zA-Z]:([/\\\\].*)?");
 	}
 	}
 
 
 	/**
 	/**

+ 26 - 0
hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java

@@ -299,6 +299,32 @@ public class NetUtil {
 	}
 	}
 
 
 	/**
 	/**
+	 * 获取指定名称的网卡信息
+	 *
+	 * @param name 网络接口名,例如Linux下默认是eth0
+	 * @return 网卡,未找到返回<code>null</code>
+	 * @since 5.0.7
+	 */
+	public static NetworkInterface getNetworkInterface(String name) {
+		Enumeration<NetworkInterface> networkInterfaces;
+		try {
+			networkInterfaces = NetworkInterface.getNetworkInterfaces();
+		} catch (SocketException e) {
+			return null;
+		}
+
+		NetworkInterface netInterface;
+		while(networkInterfaces.hasMoreElements()){
+			netInterface = networkInterfaces.nextElement();
+			if(null != netInterface && name.equals(netInterface.getName())){
+				return netInterface;
+			}
+		}
+
+		return null;
+	}
+
+	/**
 	 * 获取本机所有网卡
 	 * 获取本机所有网卡
 	 * 
 	 * 
 	 * @return 所有网卡,异常返回<code>null</code>
 	 * @return 所有网卡,异常返回<code>null</code>

+ 22 - 13
hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java

@@ -6,14 +6,17 @@ import cn.hutool.core.exceptions.UtilException;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.lang.Filter;
 import cn.hutool.core.lang.Filter;
 import cn.hutool.core.lang.SimpleCache;
 import cn.hutool.core.lang.SimpleCache;
+import cn.hutool.core.map.MapUtil;
 
 
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.Set;
 
 
 /**
 /**
@@ -134,6 +137,22 @@ public class ReflectUtil {
 	}
 	}
 
 
 	/**
 	/**
+	 * 获取指定类中字段名和字段对应的Map,包括其父类中的字段
+	 *
+	 * @param beanClass 类
+	 * @return 字段名和字段对应的Map
+	 * @since 5.0.7
+	 */
+	public static Map<String, Field> getFieldMap(Class<?> beanClass){
+		final Field[] fields = getFields(beanClass);
+		final HashMap<String, Field> map = MapUtil.newHashMap(fields.length);
+		for (Field field : fields) {
+			map.put(field.getName(), field);
+		}
+		return map;
+	}
+
+	/**
 	 * 获得一个类中所有字段列表,包括其父类中的字段
 	 * 获得一个类中所有字段列表,包括其父类中的字段
 	 *
 	 *
 	 * @param beanClass 类
 	 * @param beanClass 类
@@ -350,12 +369,7 @@ public class ReflectUtil {
 	 */
 	 */
 	public static List<Method> getPublicMethods(Class<?> clazz, Method... excludeMethods) {
 	public static List<Method> getPublicMethods(Class<?> clazz, Method... excludeMethods) {
 		final HashSet<Method> excludeMethodSet = CollectionUtil.newHashSet(excludeMethods);
 		final HashSet<Method> excludeMethodSet = CollectionUtil.newHashSet(excludeMethods);
-		return getPublicMethods(clazz, new Filter<Method>() {
-			@Override
-			public boolean accept(Method method) {
-				return false == excludeMethodSet.contains(method);
-			}
-		});
+		return getPublicMethods(clazz, method -> false == excludeMethodSet.contains(method));
 	}
 	}
 
 
 	/**
 	/**
@@ -367,12 +381,7 @@ public class ReflectUtil {
 	 */
 	 */
 	public static List<Method> getPublicMethods(Class<?> clazz, String... excludeMethodNames) {
 	public static List<Method> getPublicMethods(Class<?> clazz, String... excludeMethodNames) {
 		final HashSet<String> excludeMethodNameSet = CollectionUtil.newHashSet(excludeMethodNames);
 		final HashSet<String> excludeMethodNameSet = CollectionUtil.newHashSet(excludeMethodNames);
-		return getPublicMethods(clazz, new Filter<Method>() {
-			@Override
-			public boolean accept(Method method) {
-				return false == excludeMethodNameSet.contains(method.getName());
-			}
-		});
+		return getPublicMethods(clazz, method -> false == excludeMethodNameSet.contains(method.getName()));
 	}
 	}
 
 
 	/**
 	/**
@@ -770,7 +779,7 @@ public class ReflectUtil {
 	 */
 	 */
 	public static <T> T invokeWithCheck(Object obj, Method method, Object... args) throws UtilException {
 	public static <T> T invokeWithCheck(Object obj, Method method, Object... args) throws UtilException {
 		final Class<?>[] types = method.getParameterTypes();
 		final Class<?>[] types = method.getParameterTypes();
-		if (null != types && null != args) {
+		if (null != args) {
 			Assert.isTrue(args.length == types.length, "Params length [{}] is not fit for param length [{}] of method !", args.length, types.length);
 			Assert.isTrue(args.length == types.length, "Params length [{}] is not fit for param length [{}] of method !", args.length, types.length);
 			Class<?> type;
 			Class<?> type;
 			for (int i = 0; i < args.length; i++) {
 			for (int i = 0; i < args.length; i++) {

+ 3 - 3
hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java

@@ -2401,7 +2401,7 @@ public class StrUtil {
 		Byte dataByte;
 		Byte dataByte;
 		for (int i = 0; i < data.length; i++) {
 		for (int i = 0; i < data.length; i++) {
 			dataByte = data[i];
 			dataByte = data[i];
-			bytes[i] = (null == dataByte) ? -1 : dataByte.byteValue();
+			bytes[i] = (null == dataByte) ? -1 : dataByte;
 		}
 		}
 
 
 		return str(bytes, charset);
 		return str(bytes, charset);
@@ -4018,8 +4018,8 @@ public class StrUtil {
 	 */
 	 */
 	public static int totalLength(CharSequence... strs) {
 	public static int totalLength(CharSequence... strs) {
 		int totalLength = 0;
 		int totalLength = 0;
-		for (int i = 0; i < strs.length; i++) {
-			totalLength += (null == strs[i] ? 0 : strs[i].length());
+		for (CharSequence str : strs) {
+			totalLength += (null == str ? 0 : str.length());
 		}
 		}
 		return totalLength;
 		return totalLength;
 	}
 	}

+ 4 - 3
hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java

@@ -38,12 +38,12 @@ public class FileUtilTest {
 		String absolutePath2 = FileUtil.getAbsolutePath(absolutePath);
 		String absolutePath2 = FileUtil.getAbsolutePath(absolutePath);
 		Assert.assertNotNull(absolutePath2);
 		Assert.assertNotNull(absolutePath2);
 		Assert.assertEquals(absolutePath, absolutePath2);
 		Assert.assertEquals(absolutePath, absolutePath2);
-	}
 
 
-	@Test
-	public void getAbsolutePathTest2() {
 		String path = FileUtil.getAbsolutePath("中文.xml");
 		String path = FileUtil.getAbsolutePath("中文.xml");
 		Assert.assertTrue(path.contains("中文.xml"));
 		Assert.assertTrue(path.contains("中文.xml"));
+
+		path = FileUtil.getAbsolutePath("d:");
+		Assert.assertEquals("d:", path);
 	}
 	}
 
 
 	@Test
 	@Test
@@ -134,6 +134,7 @@ public class FileUtilTest {
 		Assert.assertEquals("C:/bar", FileUtil.normalize("C:\\..\\bar"));
 		Assert.assertEquals("C:/bar", FileUtil.normalize("C:\\..\\bar"));
 		Assert.assertEquals("bar", FileUtil.normalize("../../bar"));
 		Assert.assertEquals("bar", FileUtil.normalize("../../bar"));
 		Assert.assertEquals("C:/bar", FileUtil.normalize("/C:/bar"));
 		Assert.assertEquals("C:/bar", FileUtil.normalize("/C:/bar"));
+		Assert.assertEquals("C:", FileUtil.normalize("C:"));
 
 
 		Assert.assertEquals("\\/192.168.1.1/Share/", FileUtil.normalize("\\\\192.168.1.1\\Share\\"));
 		Assert.assertEquals("\\/192.168.1.1/Share/", FileUtil.normalize("\\\\192.168.1.1\\Share\\"));
 	}
 	}