diff --git a/common/src/main/java/com/taobao/arthas/common/JavaVersionUtils.java b/common/src/main/java/com/taobao/arthas/common/JavaVersionUtils.java index 603b1899..7887bd5c 100644 --- a/common/src/main/java/com/taobao/arthas/common/JavaVersionUtils.java +++ b/common/src/main/java/com/taobao/arthas/common/JavaVersionUtils.java @@ -28,19 +28,19 @@ public class JavaVersionUtils { } public static boolean isJava6() { - return JAVA_VERSION_STR.equals("1.6"); + return "1.6".equals(JAVA_VERSION_STR); } public static boolean isJava7() { - return JAVA_VERSION_STR.equals("1.7"); + return "1.7".equals(JAVA_VERSION_STR); } public static boolean isJava8() { - return JAVA_VERSION_STR.equals("1.8"); + return "1.8".equals(JAVA_VERSION_STR); } public static boolean isJava9() { - return JAVA_VERSION_STR.equals("9"); + return "9".equals(JAVA_VERSION_STR); } public static boolean isLessThanJava9() { diff --git a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java index bae5db72..05f8237f 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java @@ -42,20 +42,10 @@ public class FileUtils { * @since IO 2.1 */ public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException { - OutputStream out = null; - try { - out = openOutputStream(file, append); + try (OutputStream out = openOutputStream(file, append)) { out.write(data); - out.close(); // don't swallow close Exception if copy completes normally - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException ioe) { - // ignore - } } + // ignore } /** @@ -111,10 +101,8 @@ public class FileUtils { * @param file the file to save the history */ public static void saveCommandHistory(List history, File file) { - OutputStream out = null; - try { - out = new BufferedOutputStream(openOutputStream(file, false)); - for (int[] command: history) { + try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) { + for (int[] command : history) { String commandStr = Helper.fromCodePoints(command); if (isAuthCommand(commandStr)) { command = AUTH_CODEPOINTS; @@ -127,20 +115,13 @@ public class FileUtils { } } catch (IOException e) { // ignore - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException ioe) { - // ignore - } } + // ignore } public static List loadCommandHistory(File file) { BufferedReader br = null; - List history = new ArrayList(); + List history = new ArrayList<>(); try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line; @@ -167,10 +148,8 @@ public class FileUtils { * @param file the file to save the history */ public static void saveCommandHistoryString(List history, File file) { - OutputStream out = null; - try { - out = new BufferedOutputStream(openOutputStream(file, false)); - for (String command: history) { + try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) { + for (String command : history) { if (!StringUtils.isBlank(command)) { if (isAuthCommand(command)) { command = ArthasConstants.AUTH; @@ -181,20 +160,13 @@ public class FileUtils { } } catch (IOException e) { // ignore - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException ioe) { - // ignore - } } + // ignore } public static List loadCommandHistoryString(File file) { BufferedReader br = null; - List history = new ArrayList(); + List history = new ArrayList<>(); try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")); String line; @@ -218,8 +190,7 @@ public class FileUtils { } public static String readFileToString(File file, Charset encoding) throws IOException { - FileInputStream stream = new FileInputStream(file); - try { + try (FileInputStream stream = new FileInputStream(file)) { Reader reader = new BufferedReader(new InputStreamReader(stream, encoding)); StringBuilder builder = new StringBuilder(); char[] buffer = new char[8192]; @@ -228,8 +199,6 @@ public class FileUtils { builder.append(buffer, 0, read); } return builder.toString(); - } finally { - stream.close(); } }