mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-04-21 20:15:05 +00:00
Add shell script execution to the server
This commit is contained in:
parent
fa5b2a29e9
commit
2056a21538
1 changed files with 41 additions and 1 deletions
|
@ -1,7 +1,9 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Scanner;
|
||||
|
||||
public final class Command {
|
||||
|
@ -17,6 +19,44 @@ public final class Command {
|
|||
}
|
||||
}
|
||||
|
||||
public static void execShellScript(String script, String... args) throws IOException, InterruptedException {
|
||||
|
||||
ArrayList<String> cmd = new ArrayList<>();
|
||||
cmd.add("sh");
|
||||
cmd.add("-s");
|
||||
cmd.addAll(Arrays.asList(args));
|
||||
|
||||
Process process = Runtime.getRuntime().exec(cmd.toArray(new String[]{}));
|
||||
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
||||
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
BufferedWriter input = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
||||
input.write(script);
|
||||
input.close();
|
||||
|
||||
StringBuilder fullLines = new StringBuilder();
|
||||
String line;
|
||||
if (Ln.isEnabled(Ln.Level.DEBUG)) {
|
||||
while ((line = output.readLine()) != null) {
|
||||
fullLines.append(line);
|
||||
fullLines.append("\n");
|
||||
}
|
||||
Ln.d("Custom script output:\n---\n" + fullLines + "\n----\n");
|
||||
}
|
||||
fullLines = new StringBuilder();
|
||||
if (Ln.isEnabled(Ln.Level.WARN)) {
|
||||
while ((line = err.readLine()) != null) {
|
||||
fullLines.append(line);
|
||||
fullLines.append("\n");
|
||||
}
|
||||
Ln.w("Custom script err:\n---\n" + fullLines + "\n----\n");
|
||||
}
|
||||
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode != 0) {
|
||||
throw new IOException("Custom script with args: " + Arrays.toString(args) + " returned with value " + exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
public static String execReadLine(String... cmd) throws IOException, InterruptedException {
|
||||
String result = null;
|
||||
Process process = Runtime.getRuntime().exec(cmd);
|
||||
|
|
Loading…
Add table
Reference in a new issue