fix (codeorg): small code cleanup

This commit is contained in:
Gergő Törcsvári 2016-03-02 00:06:23 +01:00
parent 722ecc4f8b
commit 5d68ff0e59
5 changed files with 36 additions and 46 deletions

1
.idea/misc.xml generated
View File

@ -6,4 +6,5 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
<component name="WebServicesPlugin" addRequiredLibraries="true" />
</project> </project>

View File

@ -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;
}

View File

@ -5,15 +5,13 @@ import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class JadbConnection { public class JadbConnection implements ITransportFactory{
private final String host; private final String host;
private final int port; private final int port;
private static final int DEFAULTPORT = 5037; private static final int DEFAULTPORT = 5037;
private final Transport main;
public JadbConnection() throws IOException public JadbConnection() throws IOException
{ {
this("localhost", DEFAULTPORT); this("localhost", DEFAULTPORT);
@ -23,25 +21,17 @@ public class JadbConnection {
{ {
this.host = host; this.host = host;
this.port = port; this.port = port;
main = createTransport();
} }
protected Transport getMain(){ public Transport createTransport() throws IOException {
return main;
}
private Transport createTransport() throws IOException {
return new Transport(new Socket(host, port)); return new Transport(new Socket(host, port));
} }
public Transport getFreshTransport() throws IOException {
return createTransport();
}
public void getHostVersion() throws IOException, JadbException { public void getHostVersion() throws IOException, JadbException {
Transport main = createTransport();
main.send("host:version"); main.send("host:version");
main.verifyResponse(); main.verifyResponse();
main.close();
} }
public List<JadbDevice> getDevices() throws IOException, JadbException public List<JadbDevice> getDevices() throws IOException, JadbException
@ -70,8 +60,4 @@ public class JadbConnection {
public JadbDevice getAnyDevice() { public JadbDevice getAnyDevice() {
return JadbDevice.createAny(this); return JadbDevice.createAny(this);
} }
public void close() throws IOException {
main.close();
}
} }

View File

@ -7,28 +7,26 @@ import java.util.List;
public class JadbDevice { public class JadbDevice {
private final String serial; private final String serial;
private Transport transport; 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.serial = serial;
this.connection = connection; this.tFactory = tFactory;
this.transport = connection.getMain();
} }
static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); } static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
private JadbDevice(JadbConnection connection) private JadbDevice(ITransportFactory tFactory)
{ {
serial = null; serial = null;
this.connection = connection; this.tFactory = tFactory;
this.transport = connection.getMain();
} }
private void ensureTransportIsSelected() throws IOException, JadbException { private void getTransport() throws IOException, JadbException {
selectTransport(); if(transport!=null && !transport.isClosed()){
} transport.close();
}
private void selectTransport() throws IOException, JadbException { transport = tFactory.createTransport();
if (serial == null) if (serial == null)
{ {
transport.send("host:transport-any"); transport.send("host:transport-any");
@ -48,7 +46,7 @@ public class JadbDevice {
} }
public String getState() throws IOException, JadbException { public String getState() throws IOException, JadbException {
ensureTransportIsSelected(); getTransport();
transport.send("get-state"); transport.send("get-state");
transport.verifyResponse(); transport.verifyResponse();
return transport.readString(); return transport.readString();
@ -57,20 +55,17 @@ public class JadbDevice {
public String executeShell(String command, String ... args) throws IOException, JadbException { public String executeShell(String command, String ... args) throws IOException, JadbException {
execShell(command, args); execShell(command, args);
String ret = this.transport.readResponse(); String ret = this.transport.readResponse();
reOpenTransport();
return ret; return ret;
} }
public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException { public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException {
execShell(command, args); execShell(command, args);
byte[] ret = this.transport.readResponseAsArray(); byte[] ret = this.transport.readResponseAsArray();
reOpenTransport();
return ret; return ret;
} }
private void execShell(String command, String[] args) throws IOException, JadbException { private void execShell(String command, String[] args) throws IOException, JadbException {
ensureTransportIsSelected(); getTransport();
StringBuilder shellLine = new StringBuilder(command); StringBuilder shellLine = new StringBuilder(command);
for (String arg : args) for (String arg : args)
{ {
@ -83,7 +78,7 @@ public class JadbDevice {
} }
public List<RemoteFile> list(String remotePath) throws IOException, JadbException { public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
ensureTransportIsSelected(); getTransport();
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("LIST", remotePath); sync.send("LIST", remotePath);
@ -92,7 +87,6 @@ public class JadbDevice {
{ {
result.add(dent); result.add(dent);
} }
reOpenTransport();
return result; return result;
} }
@ -102,7 +96,7 @@ public class JadbDevice {
} }
public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException { public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException {
ensureTransportIsSelected(); getTransport();
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("SEND", remote.getPath() + "," + Integer.toString(mode)); sync.send("SEND", remote.getPath() + "," + Integer.toString(mode));
@ -110,7 +104,6 @@ public class JadbDevice {
sync.sendStatus("DONE", (int)lastModified); sync.sendStatus("DONE", (int)lastModified);
sync.verifyStatus(); sync.verifyStatus();
reOpenTransport();
} }
public void push(File local, RemoteFile remote) throws IOException, JadbException { 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 { public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException {
ensureTransportIsSelected(); getTransport();
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("RECV", remote.getPath()); sync.send("RECV", remote.getPath());
sync.readChunksTo(destination); sync.readChunksTo(destination);
reOpenTransport();
} }
public void pull(RemoteFile remote, File local) throws IOException, JadbException { public void pull(RemoteFile remote, File local) throws IOException, JadbException {
@ -138,11 +130,6 @@ public class JadbDevice {
transport.send(command); transport.send(command);
transport.verifyResponse(); transport.verifyResponse();
} }
private void reOpenTransport() throws IOException {
transport.close();
transport = connection.getFreshTransport();
}
@Override @Override
public String toString() public String toString()

View File

@ -11,6 +11,11 @@ class Transport {
private final OutputStream outputStream; private final OutputStream outputStream;
private final InputStream inputStream; private final InputStream inputStream;
private boolean closed=false;
public boolean isClosed(){
return closed;
}
private Transport(OutputStream outputStream, InputStream inputStream) { private Transport(OutputStream outputStream, InputStream inputStream) {
this.outputStream = outputStream; this.outputStream = outputStream;
@ -71,6 +76,7 @@ class Transport {
public void close() throws IOException { public void close() throws IOException {
inputStream.close(); inputStream.close();
outputStream.close(); outputStream.close();
closed = true;
} }
private byte[] repairTransportedArray(byte[] encoded) { private byte[] repairTransportedArray(byte[] encoded) {