Refactor: Consistent whitespace & brace style

This commit is contained in:
Samuel Carlsson 2016-03-20 09:13:30 +01:00
parent d716ba0325
commit 41872e3bb9
13 changed files with 249 additions and 300 deletions

View File

@ -12,13 +12,11 @@ public class JadbConnection implements ITransportFactory{
private static final int DEFAULTPORT = 5037;
public JadbConnection() throws IOException
{
public JadbConnection() throws IOException {
this("localhost", DEFAULTPORT);
}
public JadbConnection(String host, int port) throws IOException
{
public JadbConnection(String host, int port) throws IOException {
this.host = host;
this.port = port;
}
@ -34,8 +32,7 @@ public class JadbConnection implements ITransportFactory{
main.close();
}
public List<JadbDevice> getDevices() throws IOException, JadbException
{
public List<JadbDevice> getDevices() throws IOException, JadbException {
Transport devices = createTransport();
devices.send("host:devices");
@ -47,8 +44,7 @@ public class JadbConnection implements ITransportFactory{
private List<JadbDevice> parseDevices(String body) {
String[] lines = body.split("\n");
ArrayList<JadbDevice> devices = new ArrayList<JadbDevice>(lines.length);
for (String line : lines)
{
for (String line : lines) {
String[] parts = line.split("\t");
if (parts.length > 1) {
devices.add(new JadbDevice(parts[0], parts[1], this));

View File

@ -13,31 +13,28 @@ public class JadbDevice {
this.transportFactory = tFactory;
}
static JadbDevice createAny(JadbConnection connection) { return new JadbDevice(connection); }
static JadbDevice createAny(JadbConnection connection) {
return new JadbDevice(connection);
}
private JadbDevice(ITransportFactory tFactory)
{
private JadbDevice(ITransportFactory tFactory) {
serial = null;
this.transportFactory = tFactory;
}
private Transport getTransport() throws IOException, JadbException {
Transport transport = transportFactory.createTransport();
if (serial == null)
{
if (serial == null) {
transport.send("host:transport-any");
transport.verifyResponse();
}
else
{
} else {
transport.send("host:transport:" + serial);
transport.verifyResponse();
}
return transport;
}
public String getSerial()
{
public String getSerial() {
return serial;
}
@ -55,8 +52,7 @@ public class JadbDevice {
public void executeShell(OutputStream stdout, String command, String... args) throws IOException, JadbException {
Transport transport = getTransport();
StringBuilder shellLine = new StringBuilder(command);
for (String arg : args)
{
for (String arg : args) {
shellLine.append(" ");
// TODO: throw if arg contains double quote
// TODO: quote arg if it contains space
@ -74,15 +70,13 @@ public class JadbDevice {
sync.send("LIST", remotePath);
List<RemoteFile> result = new ArrayList<RemoteFile>();
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry())
{
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) {
result.add(dent);
}
return result;
}
private int getMode(File file)
{
private int getMode(File file) {
//noinspection OctalInteger
return 0664;
}
@ -124,8 +118,7 @@ public class JadbDevice {
}
@Override
public String toString()
{
public String toString() {
return "Android Device with serial " + serial;
}

View File

@ -7,5 +7,4 @@ public class JadbException extends Exception {
}
private static final long serialVersionUID = -3879283786835654165L;
}

View File

@ -20,6 +20,7 @@ public class SyncTransport {
output = outputStream;
input = inputStream;
}
public void send(String syncCommand, String name) throws IOException {
if (syncCommand.length() != 4) throw new IllegalArgumentException("sync commands must have length 4");
output.writeBytes(syncCommand);
@ -35,13 +36,11 @@ public class SyncTransport {
public void verifyStatus() throws IOException, JadbException {
String status = readString(4);
int length = readInt();
if ("FAIL".equals(status))
{
if ("FAIL".equals(status)) {
String error = readString(length);
throw new JadbException(error);
}
if (!"OKAY".equals(status))
{
if (!"OKAY".equals(status)) {
throw new JadbException("Unknown error: " + status);
}
}
@ -77,8 +76,7 @@ public class SyncTransport {
private int readChunk(byte[] buffer) throws IOException, JadbException {
String id = readString(4);
int n = readInt();
if ("FAIL".equals(id))
{
if ("FAIL".equals(id)) {
throw new JadbException(readString(n));
}
if (!"DATA".equals(id)) return -1;

View File

@ -39,8 +39,7 @@ class Transport {
public void verifyResponse() throws IOException, JadbException {
String response = readString(4);
if (!"OKAY".equals(response))
{
if (!"OKAY".equals(response)) {
String error = readString();
throw new JadbException("command failed: " + error);
}

View File

@ -20,16 +20,14 @@ class AdbProtocolHandler implements Runnable {
}
private AdbDeviceResponder findDevice(String serial) throws ProtocolException {
for (AdbDeviceResponder d : responder.getDevices())
{
for (AdbDeviceResponder d : responder.getDevices()) {
if (d.getSerial().equals(serial)) return d;
}
throw new ProtocolException("'" + serial + "' not connected");
}
@Override
public void run()
{
public void run() {
try {
runServer();
} catch (IOException e) {
@ -42,8 +40,7 @@ class AdbProtocolHandler implements Runnable {
DataInput input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
while (true)
{
while (true) {
byte[] buffer = new byte[4];
input.readFully(buffer);
String encodedLength = new String(buffer, Charset.forName("utf-8"));
@ -55,47 +52,35 @@ class AdbProtocolHandler implements Runnable {
responder.onCommand(command);
try
{
try {
if ("host:version".equals(command)) {
output.writeBytes("OKAY");
send(output, String.format("%04x", responder.getVersion()));
}
else if ("host:transport-any".equals(command))
{
} else if ("host:transport-any".equals(command)) {
// TODO: Check so that exactly one device is selected.
selected = responder.getDevices().get(0);
output.writeBytes("OKAY");
}
else if ("host:devices".equals(command)) {
} else if ("host:devices".equals(command)) {
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(tmp);
for (AdbDeviceResponder d : responder.getDevices())
{
for (AdbDeviceResponder d : responder.getDevices()) {
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
}
output.writeBytes("OKAY");
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8")));
}
else if (command.startsWith("host:transport:"))
{
} else if (command.startsWith("host:transport:")) {
String serial = command.substring("host:transport:".length());
selected = findDevice(serial);
output.writeBytes("OKAY");
}
else if ("sync:".equals(command)) {
} else if ("sync:".equals(command)) {
output.writeBytes("OKAY");
try
{
try {
sync(output, input);
}
catch (JadbException e) { // sync response with a different type of fail message
} catch (JadbException e) { // sync response with a different type of fail message
SyncTransport sync = new SyncTransport(output, input);
sync.send("FAIL", e.getMessage());
}
}
else
{
} else {
throw new ProtocolException("Unknown command: " + command);
}
} catch (ProtocolException e) {
@ -119,14 +104,12 @@ class AdbProtocolHandler implements Runnable {
private void sync(DataOutput output, DataInput input) throws IOException, JadbException {
String id = readString(input, 4);
int length = readInt(input);
if ("SEND".equals(id))
{
if ("SEND".equals(id)) {
String remotePath = readString(input, length);
int idx = remotePath.lastIndexOf(',');
String path = remotePath;
int mode = 0666;
if (idx > 0)
{
if (idx > 0) {
path = remotePath.substring(0, idx);
mode = Integer.parseInt(remotePath.substring(idx + 1));
}
@ -135,16 +118,14 @@ class AdbProtocolHandler implements Runnable {
transport.readChunksTo(buffer);
selected.filePushed(new RemoteFile(path), mode, buffer);
transport.sendStatus("OKAY", 0); // 0 = ignored
}
else if ("RECV".equals(id)) {
} else if ("RECV".equals(id)) {
String remotePath = readString(input, length);
SyncTransport transport = new SyncTransport(output, input);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
selected.filePulled(new RemoteFile(remotePath), buffer);
transport.sendStream(new ByteArrayInputStream(buffer.toByteArray()));
transport.sendStatus("DONE", 0); // ignored
}
else throw new JadbException("Unknown sync id " + id);
} else throw new JadbException("Unknown sync id " + id);
}
private String getCommandLength(String command) {

View File

@ -10,8 +10,7 @@ public class AdbServer extends SocketServer {
public static final int DEFAULT_PORT = 15037;
private final AdbResponder responder;
public AdbServer(AdbResponder responder)
{
public AdbServer(AdbResponder responder) {
this(responder, DEFAULT_PORT);
}

View File

@ -12,13 +12,11 @@ public abstract class SocketServer implements Runnable {
private Thread thread;
private final Object lockObject = new Object();
protected SocketServer(int port)
{
protected SocketServer(int port) {
this.port = port;
}
public void start() throws InterruptedException
{
public void start() throws InterruptedException {
thread = new Thread(this, "Fake Adb Server");
thread.setDaemon(true);
thread.start();
@ -34,7 +32,6 @@ public abstract class SocketServer implements Runnable {
@Override
public void run() {
try {
System.out.println("Starting on port " + port);
socket = new ServerSocket(port);
socket.setReuseAddress(true);
@ -42,8 +39,7 @@ public abstract class SocketServer implements Runnable {
lockObject.notify();
}
while (true)
{
while (true) {
Socket c = socket.accept();
Thread clientThread = new Thread(createResponder(c), "AdbClientWorker");
clientThread.setDaemon(true);

View File

@ -81,5 +81,4 @@ public class MockedTestCases {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return dateFormat.parse(date).getTime();
}
}

View File

@ -18,8 +18,7 @@ public class RealDeviceTestCases {
}
@Test
public void testGetDevices() throws Exception
{
public void testGetDevices() throws Exception {
//JadbConnection jadb = new JadbConnection("localhost", 15037);
JadbConnection jadb = new JadbConnection();
List<JadbDevice> actual = jadb.getDevices();
@ -27,24 +26,20 @@ public class RealDeviceTestCases {
}
@Test
public void testListFilesTwice() throws Exception
{
public void testListFilesTwice() throws Exception {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
for (RemoteFile f : any.list("/"))
{
for (RemoteFile f : any.list("/")) {
System.out.println(f.getPath());
}
for (RemoteFile f : any.list("/"))
{
for (RemoteFile f : any.list("/")) {
System.out.println(f.getPath());
}
}
@Test
public void testPushFile() throws Exception
{
public void testPushFile() throws Exception {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.push(new File("README.md"), new RemoteFile("/sdcard/README.md"));
@ -53,16 +48,14 @@ public class RealDeviceTestCases {
}
@Test(expected = JadbException.class)
public void testPushFileToInvalidPath() throws Exception
{
public void testPushFileToInvalidPath() throws Exception {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.push(new File("README.md"), new RemoteFile("/no/such/directory/README.md"));
}
@Test
public void testPullFile() throws Exception
{
public void testPullFile() throws Exception {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.pull(new RemoteFile("/sdcard/README.md"), new File("foobar.md"));
@ -71,16 +64,14 @@ public class RealDeviceTestCases {
}
@Test(expected = JadbException.class)
public void testPullInvalidFile() throws Exception
{
public void testPullInvalidFile() throws Exception {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.pull(new RemoteFile("/file/does/not/exist"), new File("xyz"));
}
@Test
public void testShellExecuteTwice() throws Exception
{
public void testShellExecuteTwice() throws Exception {
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.executeShell(System.out, "ls /");

View File

@ -23,12 +23,13 @@ public class FakeAdbServer implements AdbResponder {
server = new AdbServer(this, port);
}
public void start() throws InterruptedException {
System.out.println("Starting fake on port " + server.getPort());
server.start();
}
public void stop() throws IOException, InterruptedException {
System.out.println("Stopping fake on port " + server.getPort());
server.stop();
}
@ -42,8 +43,7 @@ public class FakeAdbServer implements AdbResponder {
return 31;
}
public void add(String serial)
{
public void add(String serial) {
devices.add(new DeviceResponder(serial));
}
@ -54,7 +54,9 @@ public class FakeAdbServer implements AdbResponder {
public interface ExpectationBuilder {
void failWith(String message);
void withContent(byte[] content);
void withContent(String content);
}
@ -65,8 +67,7 @@ public class FakeAdbServer implements AdbResponder {
return null;
}
public ExpectationBuilder expectPush(String serial, RemoteFile path)
{
public ExpectationBuilder expectPush(String serial, RemoteFile path) {
return findBySerial(serial).expectPush(path);
}
@ -100,8 +101,7 @@ public class FakeAdbServer implements AdbResponder {
@Override
public void filePushed(RemoteFile path, int mode, ByteArrayOutputStream buffer) throws JadbException {
for (FileExpectation fe : expectations) {
if (fe.matches(path))
{
if (fe.matches(path)) {
expectations.remove(fe);
fe.throwIfFail();
fe.verifyContent(buffer.toByteArray());
@ -114,8 +114,7 @@ public class FakeAdbServer implements AdbResponder {
@Override
public void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException {
for (FileExpectation fe : expectations) {
if (fe.matches(path))
{
if (fe.matches(path)) {
expectations.remove(fe);
fe.throwIfFail();
fe.returnFile(buffer);
@ -184,6 +183,5 @@ public class FakeAdbServer implements AdbResponder {
expectations.add(expectation);
return expectation;
}
}
}