Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use automatic file resource management #112

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions xcrash_lib/src/main/java/xcrash/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down
16 changes: 8 additions & 8 deletions xcrash_lib/src/main/java/xcrash/XCrash.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down