diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java index 98007dffc7f..9304b6c1c1f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java @@ -19,10 +19,10 @@ import org.apache.dubbo.common.utils.IOUtils; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Map; @@ -844,7 +844,7 @@ public static byte[] getMD5(byte[] source) { * @return MD5 byte array. */ public static byte[] getMD5(File file) throws IOException { - InputStream is = new FileInputStream(file); + InputStream is = Files.newInputStream(file.toPath()); try { return getMD5(is); } finally { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/AnnotationUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/AnnotationUtils.java index e3e53216b98..0c59d7232bf 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/AnnotationUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/AnnotationUtils.java @@ -154,7 +154,7 @@ static T getAttribute(Annotation annotation, String... attributeNames) throw } // exclude string attribute default is empty - if ((attribute instanceof String) && ((String) attribute).length() == 0) { + if ((attribute instanceof String) && ((String) attribute).isEmpty()) { continue; } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java index 3eb4e1860a5..e145da6c2d4 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java @@ -80,13 +80,13 @@ public static Object compatibleTypeConvert(Object value, Class type) { return new BigDecimal(string); } if (type == Short.class || type == short.class) { - return new Short(string); + return Short.valueOf(string); } if (type == Integer.class || type == int.class) { - return new Integer(string); + return Integer.valueOf(string); } if (type == Long.class || type == long.class) { - return new Long(string); + return Long.valueOf(string); } if (type == Double.class || type == double.class) { return new Double(string); @@ -95,7 +95,7 @@ public static Object compatibleTypeConvert(Object value, Class type) { return new Float(string); } if (type == Byte.class || type == byte.class) { - return new Byte(string); + return Byte.valueOf(string); } if (type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java index c2ee5587b3b..0a8feea7ecf 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java @@ -21,7 +21,6 @@ import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -36,6 +35,7 @@ import java.io.Writer; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -168,7 +168,7 @@ public static String[] readLines(File file) throws IOException { return new String[0]; } - return readLines(new FileInputStream(file)); + return readLines(Files.newInputStream(file.toPath())); } /** @@ -228,7 +228,7 @@ public static void writeLines(File file, String[] lines) throws IOException { if (file == null) { throw new IOException("File is null."); } - writeLines(new FileOutputStream(file), lines); + writeLines(Files.newOutputStream(file.toPath()), lines); } /** diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java b/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java index da1b9bc831e..e1346b627c6 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/ReferenceConfigBase.java @@ -321,7 +321,7 @@ protected void resolveFile() { resolveFile = userResolveFile.getAbsolutePath(); } } - if (resolveFile != null && resolveFile.length() > 0) { + if (resolveFile != null && !resolveFile.isEmpty()) { Properties properties = new RegexProperties(); try (FileInputStream fis = new FileInputStream(resolveFile)) { properties.load(fis); diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/container/page/ResourceFilter.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/container/page/ResourceFilter.java index 981508e4aeb..abf79685952 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/container/page/ResourceFilter.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/container/page/ResourceFilter.java @@ -27,11 +27,12 @@ import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -50,10 +51,10 @@ public class ResourceFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { String config = filterConfig.getInitParameter("resources"); - if (config != null && config.length() > 0) { + if (config != null && !config.isEmpty()) { String[] configs = Constants.COMMA_SPLIT_PATTERN.split(config); for (String c : configs) { - if (c != null && c.length() > 0) { + if (c != null && !c.isEmpty()) { c = c.replace('\\', '/'); if (c.endsWith("/")) { c = c.substring(0, c.length() - 1); @@ -118,7 +119,7 @@ private boolean isFile(String path) { private long getLastModified(String uri) { for (String resource : resources) { - if (resource != null && resource.length() > 0) { + if (resource != null && !resource.isEmpty()) { String path = resource + uri; if (isFile(path)) { File file = new File(path); @@ -136,7 +137,7 @@ private InputStream getInputStream(String uri) { String path = resource + uri; try { if (isFile(path)) { - return new FileInputStream(path); + return Files.newInputStream(Paths.get(path)); } else if (path.startsWith(CLASSPATH_PREFIX)) { return Thread.currentThread() .getContextClassLoader() diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java index f21e99b3dfd..76abbb1925a 100644 --- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java +++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java @@ -36,13 +36,13 @@ import org.apache.dubbo.rpc.model.ApplicationModel; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; @@ -233,7 +233,7 @@ private void doSaveProperties(long version) { void loadProperties() { if (file != null && file.exists()) { - try (InputStream in = new FileInputStream(file)) { + try (InputStream in = Files.newInputStream(file.toPath())) { properties.load(in); if (logger.isInfoEnabled()) { logger.info("Load service store file " + file + ", data: " + properties); diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java index 228392fe021..5b3c2d1ab15 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java @@ -110,7 +110,7 @@ public static Type[] getReturnTypes(Invocation invocation) { public static Long getInvocationId(Invocation inv) { String id = inv.getAttachment(ID_KEY); - return id == null ? null : new Long(id); + return id == null ? null : Long.valueOf(id); } /** diff --git a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/UnpackZookeeperInitializer.java b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/UnpackZookeeperInitializer.java index 209d0593bd3..2b58e6f9e8c 100644 --- a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/UnpackZookeeperInitializer.java +++ b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/UnpackZookeeperInitializer.java @@ -32,7 +32,7 @@ import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; -import org.apache.commons.compress.utils.IOUtils; +import org.apache.commons.io.IOUtils; /** * Unpack the downloaded zookeeper binary archive. @@ -52,7 +52,7 @@ private void unpack(ZookeeperContext context, int clientPort) throws DubboTestEx File sourceFile = context.getSourceFile().toFile(); Path targetPath = Paths.get(context.getSourceFile().getParent().toString(), String.valueOf(clientPort)); // check if it's unpacked. - if (targetPath.toFile() != null && targetPath.toFile().isDirectory()) { + if (targetPath.toFile().isDirectory()) { logger.info(String.format("The file has been unpacked, target path:%s", targetPath.toString())); return; } @@ -87,28 +87,31 @@ protected void doInitialize(ZookeeperContext context) throws DubboTestException // get the file name, just like apache-zookeeper-{version}-bin // the version we maybe unknown if the zookeeper archive binary file is copied by user self. Path parentPath = Paths.get(context.getSourceFile().getParent().toString(), String.valueOf(clientPort)); - if (!Files.exists(parentPath) - || !parentPath.toFile().isDirectory() - || parentPath.toFile().listFiles().length != 1) { + if (!Files.exists(parentPath) || !parentPath.toFile().isDirectory()) { + throw new IllegalStateException("There is something wrong in unpacked file!"); + } + + File[] files = parentPath.toFile().listFiles(); + if (files == null || files.length != 1) { throw new IllegalStateException("There is something wrong in unpacked file!"); } // rename directory - File sourceFile = parentPath.toFile().listFiles()[0]; - File targetFile = Paths.get(parentPath.toString(), context.getUnpackedDirectory()) - .toFile(); + File sourceFile = files[0]; + File targetFile = Paths.get(parentPath.toString(), context.getUnpackedDirectory()).toFile(); sourceFile.renameTo(targetFile); if (!Files.exists(targetFile.toPath()) || !targetFile.isDirectory()) { - throw new IllegalStateException(String.format( - "Failed to rename the directory. source directory: %s, target directory: %s", - sourceFile.toPath().toString(), targetFile.toPath().toString())); + throw new IllegalStateException(String.format("Failed to rename the directory. source directory: %s, target directory: %s", sourceFile.toPath(), targetFile.toPath())); } // get the bin path Path zookeeperBin = Paths.get(targetFile.toString(), "bin"); // update file permission - for (File file : zookeeperBin.toFile().listFiles()) { - file.setExecutable(true, false); - file.setReadable(true, false); - file.setWritable(false, false); + File[] zookeeperBinFiles = zookeeperBin.toFile().listFiles(); + if (zookeeperBinFiles != null) { + for (File file : zookeeperBinFiles) { + file.setExecutable(true, false); + file.setReadable(true, false); + file.setWritable(false, false); + } } } }