mirror of
https://github.com/revanced/jadb.git
synced 2025-05-15 13:47:04 +02:00
bytearray shell execution added
This commit is contained in:
parent
5ab126ddbe
commit
722ecc4f8b
@ -55,22 +55,33 @@ public class JadbDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String executeShell(String command, String ... args) throws IOException, JadbException {
|
public String executeShell(String command, String ... args) throws IOException, JadbException {
|
||||||
ensureTransportIsSelected();
|
execShell(command, args);
|
||||||
|
|
||||||
StringBuilder shellLine = new StringBuilder(command);
|
|
||||||
for (String arg : args)
|
|
||||||
{
|
|
||||||
shellLine.append(" ");
|
|
||||||
// TODO: throw if arg contains double quote
|
|
||||||
// TODO: quote arg if it contains space
|
|
||||||
shellLine.append(arg);
|
|
||||||
}
|
|
||||||
send("shell:" + shellLine.toString());
|
|
||||||
String ret = this.transport.readResponse();
|
String ret = this.transport.readResponse();
|
||||||
reOpenTransport();
|
reOpenTransport();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException {
|
||||||
|
execShell(command, args);
|
||||||
|
byte[] ret = this.transport.readResponseAsArray();
|
||||||
|
reOpenTransport();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void execShell(String command, String[] args) throws IOException, JadbException {
|
||||||
|
ensureTransportIsSelected();
|
||||||
|
|
||||||
|
StringBuilder shellLine = new StringBuilder(command);
|
||||||
|
for (String arg : args)
|
||||||
|
{
|
||||||
|
shellLine.append(" ");
|
||||||
|
// TODO: throw if arg contains double quote
|
||||||
|
// TODO: quote arg if it contains space
|
||||||
|
shellLine.append(arg);
|
||||||
|
}
|
||||||
|
send("shell:" + shellLine.toString());
|
||||||
|
}
|
||||||
|
|
||||||
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
||||||
ensureTransportIsSelected();
|
ensureTransportIsSelected();
|
||||||
SyncTransport sync = transport.startSync();
|
SyncTransport sync = transport.startSync();
|
||||||
|
@ -31,6 +31,10 @@ class Transport {
|
|||||||
return new String(IOUtils.toByteArray(inputStream), Charset.forName("utf-8"));
|
return new String(IOUtils.toByteArray(inputStream), Charset.forName("utf-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] readResponseAsArray() throws IOException {
|
||||||
|
return repairTransportedArray(IOUtils.toByteArray(inputStream));
|
||||||
|
}
|
||||||
|
|
||||||
public void verifyResponse() throws IOException, JadbException {
|
public void verifyResponse() throws IOException, JadbException {
|
||||||
String response = readString(4);
|
String response = readString(4);
|
||||||
if (!"OKAY".equals(response))
|
if (!"OKAY".equals(response))
|
||||||
@ -68,4 +72,21 @@ class Transport {
|
|||||||
inputStream.close();
|
inputStream.close();
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private byte[] repairTransportedArray(byte[] encoded) {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
for (int i=0; i<encoded.length; i++) {
|
||||||
|
if (encoded.length > i+1 && encoded[i] == 0x0d && encoded[i+1] == 0x0a) {
|
||||||
|
//skip 0x0d
|
||||||
|
} else {
|
||||||
|
baos.write(encoded[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
baos.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return baos.toByteArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package se.vidstige.jadb.test;
|
package se.vidstige.jadb.test;
|
||||||
|
|
||||||
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import se.vidstige.jadb.JadbDevice;
|
import se.vidstige.jadb.JadbDevice;
|
||||||
@ -92,4 +95,13 @@ public class RealDeviceTestCases {
|
|||||||
String s2=any.executeShell("ls");
|
String s2=any.executeShell("ls");
|
||||||
System.out.println(s2);
|
System.out.println(s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShellArray() throws Exception
|
||||||
|
{
|
||||||
|
JadbConnection jadb = new JadbConnection();
|
||||||
|
JadbDevice any = jadb.getAnyDevice();
|
||||||
|
byte[] s=any.executeShellGetBytearr("screencap -p");
|
||||||
|
FileUtils.writeByteArrayToFile(new File("screen.png"), s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user