diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
index 3572571..e7bedf3 100644
--- a/.idea/copyright/profiles_settings.xml
+++ b/.idea/copyright/profiles_settings.xml
@@ -1,5 +1,3 @@
-
-
-
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
index e206d70..f758959 100644
--- a/.idea/encodings.xml
+++ b/.idea/encodings.xml
@@ -1,5 +1,6 @@
-
-
-
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/junit_4_10.xml b/.idea/libraries/junit_4_10.xml
index 9b2b25d..abc0fe2 100644
--- a/.idea/libraries/junit_4_10.xml
+++ b/.idea/libraries/junit_4_10.xml
@@ -2,6 +2,7 @@
+
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 9732041..7b68680 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,8 +3,8 @@
-
+
-
-
+
+
\ No newline at end of file
diff --git a/.idea/runConfigurations/MockedTestCases.xml b/.idea/runConfigurations/MockedTestCases.xml
index 8cc97df..7223fa0 100644
--- a/.idea/runConfigurations/MockedTestCases.xml
+++ b/.idea/runConfigurations/MockedTestCases.xml
@@ -1,5 +1,6 @@
+
diff --git a/lib/commons-io-2.4.jar b/lib/commons-io-2.4.jar
new file mode 100644
index 0000000..90035a4
Binary files /dev/null and b/lib/commons-io-2.4.jar differ
diff --git a/src/se/vidstige/jadb/ITransportFactory.java b/src/se/vidstige/jadb/ITransportFactory.java
new file mode 100644
index 0000000..0f73a75
--- /dev/null
+++ b/src/se/vidstige/jadb/ITransportFactory.java
@@ -0,0 +1,10 @@
+package se.vidstige.jadb;
+
+import java.io.IOException;
+
+/**
+ * Created by Törcsi on 2016. 03. 01..
+ */
+public interface ITransportFactory {
+ public Transport createTransport() throws IOException;
+}
diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java
index d03aa0f..303c19f 100644
--- a/src/se/vidstige/jadb/JadbConnection.java
+++ b/src/se/vidstige/jadb/JadbConnection.java
@@ -5,15 +5,13 @@ import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
-public class JadbConnection {
+public class JadbConnection implements ITransportFactory{
private final String host;
private final int port;
private static final int DEFAULTPORT = 5037;
- private final Transport main;
-
public JadbConnection() throws IOException
{
this("localhost", DEFAULTPORT);
@@ -23,17 +21,17 @@ public class JadbConnection {
{
this.host = host;
this.port = port;
-
- main = createTransport();
}
- private Transport createTransport() throws IOException {
+ public Transport createTransport() throws IOException {
return new Transport(new Socket(host, port));
}
public void getHostVersion() throws IOException, JadbException {
+ Transport main = createTransport();
main.send("host:version");
main.verifyResponse();
+ main.close();
}
public List getDevices() throws IOException, JadbException
@@ -53,17 +51,13 @@ public class JadbConnection {
{
String[] parts = line.split("\t");
if (parts.length > 1) {
- devices.add(new JadbDevice(parts[0], parts[1], main));
+ devices.add(new JadbDevice(parts[0], parts[1], this));
}
}
return devices;
}
public JadbDevice getAnyDevice() {
- return JadbDevice.createAny(main);
- }
-
- public void close() throws IOException {
- main.close();
+ return JadbDevice.createAny(this);
}
}
diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java
index 5562870..ebcce92 100644
--- a/src/se/vidstige/jadb/JadbDevice.java
+++ b/src/se/vidstige/jadb/JadbDevice.java
@@ -6,31 +6,27 @@ import java.util.List;
public class JadbDevice {
private final String serial;
- private final Transport transport;
- private boolean selected = false;
+ private Transport transport;
+ private final ITransportFactory tFactory;
- JadbDevice(String serial, String type, Transport transport) {
+ JadbDevice(String serial, String type, ITransportFactory tFactory) {
this.serial = serial;
- this.transport = transport;
+ this.tFactory = tFactory;
}
- static JadbDevice createAny(Transport transport) { return new JadbDevice(transport); }
+ static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
- private JadbDevice(Transport transport)
+ private JadbDevice(ITransportFactory tFactory)
{
serial = null;
- this.transport = transport;
+ this.tFactory = tFactory;
}
- private void ensureTransportIsSelected() throws IOException, JadbException {
- if (!selected)
- {
- selectTransport();
- selected = true;
+ private void getTransport() throws IOException, JadbException {
+ if(transport!=null && !transport.isClosed()){
+ transport.close();
}
- }
-
- private void selectTransport() throws IOException, JadbException {
+ transport = tFactory.createTransport();
if (serial == null)
{
transport.send("host:transport-any");
@@ -50,28 +46,39 @@ public class JadbDevice {
}
public String getState() throws IOException, JadbException {
- ensureTransportIsSelected();
+ getTransport();
transport.send("get-state");
transport.verifyResponse();
return transport.readString();
}
- public void 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());
+ public String executeShell(String command, String ... args) throws IOException, JadbException {
+ execShell(command, args);
+ String ret = this.transport.readResponse();
+ return ret;
}
+ public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException {
+ execShell(command, args);
+ byte[] ret = this.transport.readResponseAsArray();
+ return ret;
+ }
+
+ private void execShell(String command, String[] args) throws IOException, JadbException {
+ getTransport();
+ 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();
+ getTransport();
SyncTransport sync = transport.startSync();
sync.send("LIST", remotePath);
@@ -89,7 +96,7 @@ public class JadbDevice {
}
public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException {
- ensureTransportIsSelected();
+ getTransport();
SyncTransport sync = transport.startSync();
sync.send("SEND", remote.getPath() + "," + Integer.toString(mode));
@@ -106,7 +113,7 @@ public class JadbDevice {
}
public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException {
- ensureTransportIsSelected();
+ getTransport();
SyncTransport sync = transport.startSync();
sync.send("RECV", remote.getPath());
diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java
index a4c1228..6cccb2b 100644
--- a/src/se/vidstige/jadb/Transport.java
+++ b/src/se/vidstige/jadb/Transport.java
@@ -1,5 +1,8 @@
package se.vidstige.jadb;
+
+import org.apache.commons.io.IOUtils;
+
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
@@ -8,6 +11,11 @@ class Transport {
private final OutputStream outputStream;
private final InputStream inputStream;
+ private boolean closed=false;
+
+ public boolean isClosed(){
+ return closed;
+ }
private Transport(OutputStream outputStream, InputStream inputStream) {
this.outputStream = outputStream;
@@ -23,7 +31,15 @@ class Transport {
int length = Integer.parseInt(encodedLength, 16);
return readString(length);
}
-
+
+ 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))
@@ -60,5 +76,23 @@ class Transport {
public void close() throws IOException {
inputStream.close();
outputStream.close();
+ closed = true;
}
+
+ 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/MockedTestCases.java b/test/se/vidstige/jadb/test/MockedTestCases.java
index 1863754..67f7d7c 100644
--- a/test/se/vidstige/jadb/test/MockedTestCases.java
+++ b/test/se/vidstige/jadb/test/MockedTestCases.java
@@ -32,7 +32,6 @@ public class MockedTestCases {
@After
public void tearDown() throws Exception {
- connection.close();
server.stop();
server.verifyExpectations();
}
diff --git a/test/se/vidstige/jadb/test/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/RealDeviceTestCases.java
index fd0c96b..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;
@@ -37,6 +40,12 @@ public class RealDeviceTestCases {
{
System.out.println(f.getPath());
}
+ //second read on the same device
+ List files2 = any.list("/");
+ for (RemoteFile f : files2)
+ {
+ System.out.println(f.getPath());
+ }
}
@Test
@@ -45,6 +54,8 @@ public class RealDeviceTestCases {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
+ //second read on the same device
+ any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
}
@Test(expected = JadbException.class)
@@ -61,6 +72,8 @@ public class RealDeviceTestCases {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md"));
+ //second read on the same device
+ any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md"));
}
@Test(expected = JadbException.class)
@@ -70,4 +83,25 @@ public class RealDeviceTestCases {
JadbDevice any = jadb.getAnyDevice();
any.pull(new RemoteFile("/file/does/not/exist"), new File("xyz"));
}
+
+ @Test
+ public void testShell() throws Exception
+ {
+ JadbConnection jadb = new JadbConnection();
+ JadbDevice any = jadb.getAnyDevice();
+ String s=any.executeShell("ls -la");
+ System.out.println(s);
+ //second read on the same device
+ 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);
+ }
}