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

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">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="WebServicesPlugin" addRequiredLibraries="true" />
</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.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<JadbDevice> 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();
}
}

View File

@ -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<RemoteFile> 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()

View File

@ -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) {