处理资源未释放的问题

main
Matthew 2 weeks ago
parent c23f30ce71
commit 8aec5f30ef

@ -1829,21 +1829,28 @@ public class MicroPhotoService extends Service {
public void setStaticNetwork(String iface, String ip, String gateway, String ipPrefix, int ipPrefixLength) { public void setStaticNetwork(String iface, String ip, String gateway, String ipPrefix, int ipPrefixLength) {
int exitValue = -1; int exitValue = -1;
boolean success = false; boolean success = false;
try { try {
File ethShellFile = new File(getFilesDir(), "eth.sh"); File ethShellFile = new File(getFilesDir(), "eth.sh");
if (ethShellFile.exists()) { if (ethShellFile.exists()) {
Process process = Runtime.getRuntime().exec("/system/xbin/su"); Process process = null;
DataOutputStream os = new DataOutputStream(process.getOutputStream()); DataOutputStream os = null;
BufferedReader inputReader = null;
try {
process = Runtime.getRuntime().exec("/system/xbin/su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("/system/bin/sh " + ethShellFile.getAbsolutePath() + "\n"); os.writeBytes("/system/bin/sh " + ethShellFile.getAbsolutePath() + "\n");
os.writeBytes("exit\n"); // 重要退出su shell os.writeBytes("exit\n"); // 重要退出su shell
os.flush(); os.flush();
exitValue = process.waitFor(); exitValue = process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); inputReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null; String line = null;
StringBuilder error = new StringBuilder(); StringBuilder error = new StringBuilder();
while ((line = reader.readLine()) != null) { while ((line = inputReader.readLine()) != null) {
error.append(line); error.append(line);
error.append("\n"); error.append("\n");
} }
@ -1853,6 +1860,15 @@ public class MicroPhotoService extends Service {
} else { } else {
infoLog(error.toString()); infoLog(error.toString());
} }
} catch (Exception ex) {
} finally {
FilesUtils.closeFriendly(os);
FilesUtils.closeFriendly(inputReader);
if (process != null) {
process.destroy();
}
}
} else { } else {
Process process = Runtime.getRuntime().exec("/system/xbin/su root"); Process process = Runtime.getRuntime().exec("/system/xbin/su root");
DataOutputStream os = new DataOutputStream(process.getOutputStream()); DataOutputStream os = new DataOutputStream(process.getOutputStream());

Loading…
Cancel
Save