bytearray shell execution added

This commit is contained in:
Gergő Törcsvári 2016-02-28 10:54:33 +01:00
parent 5ab126ddbe
commit 722ecc4f8b
3 changed files with 56 additions and 12 deletions

View File

@ -55,22 +55,33 @@ public class JadbDevice {
}
public String executeShell(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());
execShell(command, args);
String ret = this.transport.readResponse();
reOpenTransport();
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 {
ensureTransportIsSelected();
SyncTransport sync = transport.startSync();

View File

@ -30,7 +30,11 @@ class Transport {
public String readResponse() throws IOException {
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 {
String response = readString(4);
if (!"OKAY".equals(response))
@ -68,4 +72,21 @@ class Transport {
inputStream.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();
}
}

View File

@ -1,8 +1,11 @@
package se.vidstige.jadb.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import se.vidstige.jadb.JadbDevice;
@ -92,4 +95,13 @@ public class RealDeviceTestCases {
String s2=any.executeShell("ls");
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);
}
}