Refactor: Avoids having stale transport lingering around.

This commit is contained in:
Samuel Carlsson 2016-03-02 21:14:32 +01:00
parent 34f4c7813c
commit 84950ceadb
2 changed files with 19 additions and 27 deletions

View File

@ -6,12 +6,11 @@ import java.util.List;
public class JadbDevice { public class JadbDevice {
private final String serial; private final String serial;
private Transport transport; private final ITransportFactory transportFactory;
private final ITransportFactory tFactory;
JadbDevice(String serial, String type, ITransportFactory tFactory) { JadbDevice(String serial, String type, ITransportFactory tFactory) {
this.serial = serial; this.serial = serial;
this.tFactory = tFactory; this.transportFactory = tFactory;
} }
static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); } static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
@ -19,14 +18,11 @@ public class JadbDevice {
private JadbDevice(ITransportFactory tFactory) private JadbDevice(ITransportFactory tFactory)
{ {
serial = null; serial = null;
this.tFactory = tFactory; this.transportFactory = tFactory;
} }
private void getTransport() throws IOException, JadbException { private Transport getTransport() throws IOException, JadbException {
if(transport!=null && !transport.isClosed()){ Transport transport = transportFactory.createTransport();
transport.close();
}
transport = tFactory.createTransport();
if (serial == null) if (serial == null)
{ {
transport.send("host:transport-any"); transport.send("host:transport-any");
@ -36,8 +32,8 @@ public class JadbDevice {
{ {
transport.send("host:transport:" + serial); transport.send("host:transport:" + serial);
transport.verifyResponse(); transport.verifyResponse();
} }
return transport;
} }
public String getSerial() public String getSerial()
@ -46,19 +42,18 @@ public class JadbDevice {
} }
public String getState() throws IOException, JadbException { public String getState() throws IOException, JadbException {
getTransport(); Transport transport = getTransport();
transport.send("get-state"); transport.send("get-state");
transport.verifyResponse(); transport.verifyResponse();
return transport.readString(); return transport.readString();
} }
public void executeShell(OutputStream stdout, String command, String ... args) throws IOException, JadbException { public void executeShell(String command, String ... args) throws IOException, JadbException {
executeShell(command, args); executeShell(null, command, args);
this.transport.readResponseTo(stdout);
} }
private void executeShell(String command, String[] args) throws IOException, JadbException { public void executeShell(OutputStream stdout, String command, String ... args) throws IOException, JadbException {
getTransport(); Transport transport = getTransport();
StringBuilder shellLine = new StringBuilder(command); StringBuilder shellLine = new StringBuilder(command);
for (String arg : args) for (String arg : args)
{ {
@ -67,11 +62,14 @@ public class JadbDevice {
// TODO: quote arg if it contains space // TODO: quote arg if it contains space
shellLine.append(arg); shellLine.append(arg);
} }
send("shell:" + shellLine.toString()); send(transport, "shell:" + shellLine.toString());
if (stdout != null) {
transport.readResponseTo(stdout);
}
} }
public List<RemoteFile> list(String remotePath) throws IOException, JadbException { public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
getTransport(); Transport transport = getTransport();
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("LIST", remotePath); sync.send("LIST", remotePath);
@ -90,7 +88,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 {
getTransport(); Transport transport = 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));
@ -107,7 +105,7 @@ public class JadbDevice {
} }
public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException { public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException {
getTransport(); Transport transport = getTransport();
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("RECV", remote.getPath()); sync.send("RECV", remote.getPath());
@ -120,7 +118,7 @@ public class JadbDevice {
fileStream.close(); fileStream.close();
} }
private void send(String command) throws IOException, JadbException { private void send(Transport transport, String command) throws IOException, JadbException {
transport.send(command); transport.send(command);
transport.verifyResponse(); transport.verifyResponse();
} }

View File

@ -8,11 +8,6 @@ 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;
@ -78,6 +73,5 @@ class Transport {
public void close() throws IOException { public void close() throws IOException {
inputStream.close(); inputStream.close();
outputStream.close(); outputStream.close();
closed = true;
} }
} }