summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorzhangshuai <[email protected]>2024-11-04 11:24:41 +0800
committerzhangshuai <[email protected]>2024-11-04 11:24:41 +0800
commit3965a7a44df1fc11b7c0884257b4a9192c4b26c6 (patch)
treea69a30a1fa4ef5f75125c4375d306f31896ba98b /src
parentf90a6deffb946b93f7336aed335b95d4101ce204 (diff)
fix: 调整 adb init 时日志
Diffstat (limited to 'src')
-rw-r--r--src/main/java/net/geedge/api/util/AdbUtil.java42
-rw-r--r--src/main/java/net/geedge/api/util/CommandExec.java41
2 files changed, 55 insertions, 28 deletions
diff --git a/src/main/java/net/geedge/api/util/AdbUtil.java b/src/main/java/net/geedge/api/util/AdbUtil.java
index c519a9a..0e09fab 100644
--- a/src/main/java/net/geedge/api/util/AdbUtil.java
+++ b/src/main/java/net/geedge/api/util/AdbUtil.java
@@ -9,7 +9,9 @@ import net.geedge.common.Constant;
import net.geedge.common.RCode;
import net.geedge.common.T;
+import java.io.BufferedReader;
import java.io.File;
+import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.*;
@@ -718,24 +720,11 @@ public class AdbUtil {
* exec shell command
*/
public String execShellCommand(String cmd, Integer timeout){
- Process process = commandExec.execForProcess(AdbCommandBuilder.builder()
+ String result = commandExec.exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell " + cmd)
.build());
-
- ExecutorService executor = Executors.newSingleThreadExecutor();
- Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
- try {
- String result = future.get(timeout, TimeUnit.SECONDS);
- return result;
- } catch (TimeoutException e) {
- process.destroyForcibly();
- throw new APIException(RCode.TIMEOUT);
- } catch (ExecutionException | InterruptedException e) {
- throw new APIException(RCode.ERROR);
- } finally {
- executor.shutdown();
- }
+ return result;
}
/**
@@ -750,7 +739,7 @@ public class AdbUtil {
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"iptables -L OUTPUT --line-numbers | grep ASW_OUTPUT\""))
.build());
- log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [result: {}]", outputChainResult);
+ log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [exist: {}]", T.StrUtil.isEmpty(outputChainResult));
if (T.StrUtil.isEmpty(outputChainResult)) {
// ASW_OUTPUT 添加到 OUTPUT 链中
this.execShellCommand("shell iptables -A OUTPUT -j ASW_OUTPUT");
@@ -864,20 +853,25 @@ public class AdbUtil {
.build();
Process process = commandExec.execForProcess(command);
- ExecutorService executor = Executors.newSingleThreadExecutor();
- Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
+ T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), logFile, "UTF-8");
+ InputStreamReader inputStreamReader = null;
+ BufferedReader bufferedReader = null;
try {
+ inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");
+ bufferedReader = new BufferedReader(inputStreamReader);
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ // 处理每一行输出
+ T.FileUtil.appendString(T.StrUtil.concat(true, line, "\n"), logFile, "UTF-8");
+ }
int exitCode = process.waitFor();
- String result = future.get(10, TimeUnit.SECONDS);
- T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), logFile, "UTF-8");
- T.FileUtil.appendString(T.StrUtil.concat(true, result.stripTrailing(), "\n"), logFile, "UTF-8");
-
- return new CommandResult(exitCode, result);
+ return new CommandResult(exitCode, T.StrUtil.EMPTY);
} catch (Exception e) {
process.destroyForcibly();
throw new APIException(RCode.ERROR);
}finally {
- executor.shutdown();
+ T.IoUtil.close(inputStreamReader);
+ T.IoUtil.close(bufferedReader);
}
}
} \ No newline at end of file
diff --git a/src/main/java/net/geedge/api/util/CommandExec.java b/src/main/java/net/geedge/api/util/CommandExec.java
index cab80f4..84dd0b8 100644
--- a/src/main/java/net/geedge/api/util/CommandExec.java
+++ b/src/main/java/net/geedge/api/util/CommandExec.java
@@ -1,22 +1,54 @@
package net.geedge.api.util;
+import net.geedge.common.APIException;
+import net.geedge.common.RCode;
import net.geedge.common.T;
-import java.io.File;
+import java.io.*;
import java.util.List;
import java.util.stream.Collectors;
public class CommandExec {
private File logFile;
+ private ProcessBuilder processBuilder;
public String exec(List<String> command) {
- String str = T.RuntimeUtil.execForStr(T.CharsetUtil.CHARSET_UTF_8, command.stream().toArray(String[]::new));
+
if (logFile != null) {
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), this.logFile, "UTF-8");
- T.FileUtil.appendString(T.StrUtil.concat(true, str.stripTrailing(), "\n"), this.logFile, "UTF-8");
}
- return str.stripTrailing();
+
+ InputStreamReader inputStreamReader = null;
+ BufferedReader bufferedReader = null;
+ StringBuilder stringBuilder = null;
+ Process process = null;
+ InputStream inputStream = null;
+ try {
+ process = processBuilder.command(command).start();
+ stringBuilder = new StringBuilder();
+ inputStream = process.getInputStream();
+ bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ if (logFile != null) {
+ // 处理每一行输出
+ T.FileUtil.appendString(T.StrUtil.concat(true, line, "\n"), this.logFile, "UTF-8");
+ }
+ stringBuilder.append(line).append(System.lineSeparator());
+ }
+ } catch (Exception e) {
+ throw new APIException(RCode.ERROR);
+ }finally {
+ if (process != null) {
+ process.destroy();
+ }
+ T.IoUtil.close(inputStreamReader);
+ T.IoUtil.close(bufferedReader);
+ T.IoUtil.close(inputStream);
+ }
+
+ return stringBuilder.toString().stripTrailing();
}
public Process execForProcess(List<String> command) {
@@ -26,5 +58,6 @@ public class CommandExec {
public CommandExec(File logFile ) {
this.logFile = logFile;
+ this.processBuilder = new ProcessBuilder();
}
} \ No newline at end of file