diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index a8966ea..9bc20ec 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -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 list(String remotePath) throws IOException, JadbException { ensureTransportIsSelected(); SyncTransport sync = transport.startSync(); diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index 49ab7e0..e087299 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -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 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(); + } } diff --git a/test/se/vidstige/jadb/test/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/RealDeviceTestCases.java index 5332a36..dfa8b84 100644 --- a/test/se/vidstige/jadb/test/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/RealDeviceTestCases.java @@ -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); + } }