From 27bed5a88ac3df686ccbea452349c0de14cecc50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20T=C3=B6rcsv=C3=A1ri?= Date: Wed, 2 Mar 2016 00:06:23 +0100 Subject: [PATCH] fix (codeorg): small code cleanup fix (test): bugfix --- .idea/misc.xml | 1 + src/se/vidstige/jadb/ITransportFactory.java | 10 +++++ src/se/vidstige/jadb/JadbConnection.java | 22 ++-------- src/se/vidstige/jadb/JadbDevice.java | 43 +++++++------------ src/se/vidstige/jadb/Transport.java | 6 +++ .../vidstige/jadb/test/MockedTestCases.java | 1 - 6 files changed, 36 insertions(+), 47 deletions(-) create mode 100644 src/se/vidstige/jadb/ITransportFactory.java diff --git a/.idea/misc.xml b/.idea/misc.xml index e5a00db..7b68680 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,4 +6,5 @@ + \ No newline at end of file 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 f5a8893..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,25 +21,17 @@ public class JadbConnection { { this.host = host; this.port = port; - - main = createTransport(); } - protected Transport getMain(){ - return main; - } - - private Transport createTransport() throws IOException { + public Transport createTransport() throws IOException { return new Transport(new Socket(host, port)); } - public Transport getFreshTransport() throws IOException { - return createTransport(); - } - public void getHostVersion() throws IOException, JadbException { + Transport main = createTransport(); main.send("host:version"); main.verifyResponse(); + main.close(); } public List getDevices() throws IOException, JadbException @@ -70,8 +60,4 @@ public class JadbConnection { public JadbDevice getAnyDevice() { return JadbDevice.createAny(this); } - - public void close() throws IOException { - main.close(); - } } diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 9bc20ec..ebcce92 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -7,28 +7,26 @@ import java.util.List; public class JadbDevice { private final String serial; private Transport transport; - private final JadbConnection connection; + private final ITransportFactory tFactory; - JadbDevice(String serial, String type, JadbConnection connection) { + JadbDevice(String serial, String type, ITransportFactory tFactory) { this.serial = serial; - this.connection = connection; - this.transport = connection.getMain(); + this.tFactory = tFactory; } static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); } - private JadbDevice(JadbConnection connection) + private JadbDevice(ITransportFactory tFactory) { serial = null; - this.connection = connection; - this.transport = connection.getMain(); + this.tFactory = tFactory; } - private void ensureTransportIsSelected() throws IOException, JadbException { - selectTransport(); - } - - private void selectTransport() throws IOException, JadbException { + private void getTransport() throws IOException, JadbException { + if(transport!=null && !transport.isClosed()){ + transport.close(); + } + transport = tFactory.createTransport(); if (serial == null) { transport.send("host:transport-any"); @@ -48,7 +46,7 @@ public class JadbDevice { } public String getState() throws IOException, JadbException { - ensureTransportIsSelected(); + getTransport(); transport.send("get-state"); transport.verifyResponse(); return transport.readString(); @@ -57,20 +55,17 @@ public class JadbDevice { public String executeShell(String command, String ... args) throws IOException, JadbException { 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(); - + getTransport(); StringBuilder shellLine = new StringBuilder(command); for (String arg : args) { @@ -83,7 +78,7 @@ public class JadbDevice { } public List list(String remotePath) throws IOException, JadbException { - ensureTransportIsSelected(); + getTransport(); SyncTransport sync = transport.startSync(); sync.send("LIST", remotePath); @@ -92,7 +87,6 @@ public class JadbDevice { { result.add(dent); } - reOpenTransport(); return result; } @@ -102,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)); @@ -110,7 +104,6 @@ public class JadbDevice { sync.sendStatus("DONE", (int)lastModified); sync.verifyStatus(); - reOpenTransport(); } public void push(File local, RemoteFile remote) throws IOException, JadbException { @@ -120,12 +113,11 @@ public class JadbDevice { } public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException { - ensureTransportIsSelected(); + getTransport(); SyncTransport sync = transport.startSync(); sync.send("RECV", remote.getPath()); sync.readChunksTo(destination); - reOpenTransport(); } public void pull(RemoteFile remote, File local) throws IOException, JadbException { @@ -138,11 +130,6 @@ public class JadbDevice { transport.send(command); transport.verifyResponse(); } - - private void reOpenTransport() throws IOException { - transport.close(); - transport = connection.getFreshTransport(); - } @Override public String toString() diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index e087299..6cccb2b 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -11,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; @@ -71,6 +76,7 @@ class Transport { public void close() throws IOException { inputStream.close(); outputStream.close(); + closed = true; } private byte[] repairTransportedArray(byte[] encoded) { 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(); }