diff --git a/xcrash_lib/src/main/java/xcrash/Util.java b/xcrash_lib/src/main/java/xcrash/Util.java index 883ac0d..feee3b3 100644 --- a/xcrash_lib/src/main/java/xcrash/Util.java +++ b/xcrash_lib/src/main/java/xcrash/Util.java @@ -93,9 +93,7 @@ static String getProcessName(Context ctx, int pid) { // } //get from /proc/PID/cmdline - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline")); + try (BufferedReader br = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"))) { String processName = br.readLine(); if (!TextUtils.isEmpty(processName)) { processName = processName.trim(); @@ -104,13 +102,6 @@ static String getProcessName(Context ctx, int pid) { } } } catch (Exception ignored) { - } finally { - try { - if (br != null) { - br.close(); - } - } catch (Exception ignored) { - } } //failed @@ -244,11 +235,9 @@ private static String getFileContent(String pathname) { private static String getFileContent(String pathname, int limit) { StringBuilder sb = new StringBuilder(); - BufferedReader br = null; String line; int n = 0; - try { - br = new BufferedReader(new FileReader(pathname)); + try (BufferedReader br = new BufferedReader(new FileReader(pathname))) { while (null != (line = br.readLine())) { String p = line.trim(); if (p.length() > 0) { @@ -263,13 +252,6 @@ private static String getFileContent(String pathname, int limit) { } } catch (Exception e) { XCrash.getLogger().i(Util.TAG, "Util getInfo(" + pathname + ") failed", e); - } finally { - if (br != null) { - try { - br.close(); - } catch (Exception ignored) { - } - } } return sb.toString(); } diff --git a/xcrash_lib/src/main/java/xcrash/XCrash.java b/xcrash_lib/src/main/java/xcrash/XCrash.java index 73821b0..e0bbaed 100644 --- a/xcrash_lib/src/main/java/xcrash/XCrash.java +++ b/xcrash_lib/src/main/java/xcrash/XCrash.java @@ -269,7 +269,7 @@ public InitParameters setLogDir(String dir) { */ @SuppressWarnings("unused") public InitParameters setLogFileMaintainDelayMs(int logFileMaintainDelayMs) { - this.logFileMaintainDelayMs = (logFileMaintainDelayMs < 0 ? 0 : logFileMaintainDelayMs); + this.logFileMaintainDelayMs = (Math.max(logFileMaintainDelayMs, 0)); return this; } @@ -309,7 +309,7 @@ public InitParameters setLibLoader(ILibLoader libLoader) { */ @SuppressWarnings("unused") public InitParameters setPlaceholderCountMax(int countMax) { - this.placeholderCountMax = (countMax < 0 ? 0 : countMax); + this.placeholderCountMax = (Math.max(countMax, 0)); return this; } @@ -321,7 +321,7 @@ public InitParameters setPlaceholderCountMax(int countMax) { */ @SuppressWarnings("unused") public InitParameters setPlaceholderSizeKb(int sizeKb) { - this.placeholderSizeKb = (sizeKb < 0 ? 0 : sizeKb); + this.placeholderSizeKb = (Math.max(sizeKb, 0)); return this; } @@ -382,7 +382,7 @@ public InitParameters setJavaRethrow(boolean rethrow) { */ @SuppressWarnings("unused") public InitParameters setJavaLogCountMax(int countMax) { - this.javaLogCountMax = (countMax < 1 ? 1 : countMax); + this.javaLogCountMax = (Math.max(countMax, 1)); return this; } @@ -470,7 +470,7 @@ public InitParameters setJavaDumpAllThreads(boolean flag) { */ @SuppressWarnings("unused") public InitParameters setJavaDumpAllThreadsCountMax(int countMax) { - this.javaDumpAllThreadsCountMax = (countMax < 0 ? 0 : countMax); + this.javaDumpAllThreadsCountMax = (Math.max(countMax, 0)); return this; } @@ -560,7 +560,7 @@ public InitParameters setNativeRethrow(boolean rethrow) { */ @SuppressWarnings("unused") public InitParameters setNativeLogCountMax(int countMax) { - this.nativeLogCountMax = (countMax < 1 ? 1 : countMax); + this.nativeLogCountMax = (Math.max(countMax, 1)); return this; } @@ -672,7 +672,7 @@ public InitParameters setNativeDumpAllThreads(boolean flag) { */ @SuppressWarnings("unused") public InitParameters setNativeDumpAllThreadsCountMax(int countMax) { - this.nativeDumpAllThreadsCountMax = (countMax < 0 ? 0 : countMax); + this.nativeDumpAllThreadsCountMax = (Math.max(countMax, 0)); return this; } @@ -781,7 +781,7 @@ public InitParameters setAnrCheckProcessState(boolean checkProcessState) { */ @SuppressWarnings("unused") public InitParameters setAnrLogCountMax(int countMax) { - this.anrLogCountMax = (countMax < 1 ? 1 : countMax); + this.anrLogCountMax = (Math.max(countMax, 1)); return this; }