From 1bd60cd9461d9538766dd665fc16155da5143d35 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 10:24:54 +0200 Subject: [PATCH 01/17] Use JadbDevice.send() where possible --- src/se/vidstige/jadb/JadbDevice.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 6c6bf6d..c14244a 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -46,11 +46,9 @@ public class JadbDevice { private Transport getTransport() throws IOException, JadbException { Transport transport = transportFactory.createTransport(); if (serial == null) { - transport.send("host:transport-any"); - transport.verifyResponse(); + send(transport,"host:transport-any"); } else { - transport.send("host:transport:" + serial); - transport.verifyResponse(); + send(transport,"host:transport:" + serial); } return transport; } @@ -62,11 +60,9 @@ public class JadbDevice { public State getState() throws IOException, JadbException { Transport transport = transportFactory.createTransport(); if (serial == null) { - transport.send("host:get-state"); - transport.verifyResponse(); + send(transport, "host:get-state"); } else { - transport.send("host-serial:" + serial + ":get-state"); - transport.verifyResponse(); + send(transport, "host-serial:" + serial + ":get-state"); } State state = convertState(transport.readString()); From d5e464e52d790d09a34512537d64f158f3770027 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 10:37:09 +0200 Subject: [PATCH 02/17] Convert "if"s to ternary operators --- src/se/vidstige/jadb/JadbDevice.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index c14244a..cda1a57 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -45,11 +45,7 @@ public class JadbDevice { private Transport getTransport() throws IOException, JadbException { Transport transport = transportFactory.createTransport(); - if (serial == null) { - send(transport,"host:transport-any"); - } else { - send(transport,"host:transport:" + serial); - } + send(transport, serial == null ? "host:transport-any" : "host:transport:" + serial ); return transport; } @@ -59,11 +55,7 @@ public class JadbDevice { public State getState() throws IOException, JadbException { Transport transport = transportFactory.createTransport(); - if (serial == null) { - send(transport, "host:get-state"); - } else { - send(transport, "host-serial:" + serial + ":get-state"); - } + send(transport, serial == null ? "host:get-state" : "host-serial:" + serial + ":get-state"); State state = convertState(transport.readString()); transport.close(); From cb989acfa9eb2889739eb7cc1e5b193f406017d9 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:10:33 +0200 Subject: [PATCH 03/17] Simplify "if"s --- src/se/vidstige/jadb/JadbDevice.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index cda1a57..f3ac12e 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -210,10 +210,8 @@ public class JadbDevice { return false; JadbDevice other = (JadbDevice) obj; if (serial == null) { - if (other.serial != null) - return false; - } else if (!serial.equals(other.serial)) - return false; - return true; + return other.serial == null; + } + return serial.equals(other.serial); } } From 45c67bad0ab5c505aef7e5958054cf83c150eb3d Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 10:29:19 +0200 Subject: [PATCH 04/17] Use try-with-resources for reading/writing files --- src/se/vidstige/jadb/JadbDevice.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index f3ac12e..2000f63 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -163,9 +163,9 @@ public class JadbDevice { } public void push(File local, RemoteFile remote) throws IOException, JadbException { - FileInputStream fileStream = new FileInputStream(local); - push(fileStream, local.lastModified(), getMode(local), remote); - fileStream.close(); + try (FileInputStream fileStream = new FileInputStream(local)) { + push(fileStream, local.lastModified(), getMode(local), remote); + } } public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException { @@ -177,9 +177,9 @@ public class JadbDevice { } public void pull(RemoteFile remote, File local) throws IOException, JadbException { - FileOutputStream fileStream = new FileOutputStream(local); - pull(remote, fileStream); - fileStream.close(); + try (FileOutputStream fileStream = new FileOutputStream(local)) { + pull(remote, fileStream); + } } private void send(Transport transport, String command) throws IOException, JadbException { From eb853f2b0170380441972a2b937de76e0ff79465 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 10:30:18 +0200 Subject: [PATCH 05/17] Remove unused parameter --- src/se/vidstige/jadb/JadbConnection.java | 2 +- src/se/vidstige/jadb/JadbDevice.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 1af2dd2..556b761 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -78,7 +78,7 @@ public class JadbConnection implements ITransportFactory { for (String line : lines) { String[] parts = line.split("\t"); if (parts.length > 1) { - devices.add(new JadbDevice(parts[0], parts[1], this)); + devices.add(new JadbDevice(parts[0], this)); // parts[1] is type } } return devices; diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 2000f63..cb0d77a 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -19,7 +19,7 @@ public class JadbDevice { private final String serial; private final ITransportFactory transportFactory; - JadbDevice(String serial, String type, ITransportFactory tFactory) { + JadbDevice(String serial, ITransportFactory tFactory) { this.serial = serial; this.transportFactory = tFactory; } From c39c0d16eeef5719f44dcc47440c5b6b60281977 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 10:33:42 +0200 Subject: [PATCH 06/17] Replace hardcoded method with constant --- src/se/vidstige/jadb/JadbDevice.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index cb0d77a..0ca5295 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -16,6 +16,8 @@ public class JadbDevice { BootLoader } + //noinspection OctalInteger + private static final int DEFAULT_MODE = 0664; private final String serial; private final ITransportFactory transportFactory; @@ -146,11 +148,6 @@ public class JadbDevice { return result; } - private int getMode(File file) { - //noinspection OctalInteger - return 0664; - } - public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException { Transport transport = getTransport(); SyncTransport sync = transport.startSync(); @@ -164,7 +161,7 @@ public class JadbDevice { public void push(File local, RemoteFile remote) throws IOException, JadbException { try (FileInputStream fileStream = new FileInputStream(local)) { - push(fileStream, local.lastModified(), getMode(local), remote); + push(fileStream, local.lastModified(), DEFAULT_MODE, remote); } } From b255b3b618d2e7bb646b035672ca84965fb09c1a Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 11:12:43 +0200 Subject: [PATCH 07/17] Use more general DataOutput instead of DataOutputStream where possible --- src/se/vidstige/jadb/server/AdbProtocolHandler.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/se/vidstige/jadb/server/AdbProtocolHandler.java b/src/se/vidstige/jadb/server/AdbProtocolHandler.java index 884112b..02f2469 100644 --- a/src/se/vidstige/jadb/server/AdbProtocolHandler.java +++ b/src/se/vidstige/jadb/server/AdbProtocolHandler.java @@ -80,7 +80,7 @@ class AdbProtocolHandler implements Runnable { return true; } - private void hostSerial(DataOutputStream output, String command) throws IOException { + private void hostSerial(DataOutput output, String command) throws IOException { String[] strs = command.split(":",0); if (strs.length != 3) { throw new ProtocolException("Invalid command: " + command); @@ -102,7 +102,7 @@ class AdbProtocolHandler implements Runnable { } } - private void hostGetState(DataOutputStream output) throws IOException { + private void hostGetState(DataOutput output) throws IOException { // TODO: Check so that exactly one device is selected. AdbDeviceResponder device = responder.getDevices().get(0); output.writeBytes("OKAY"); @@ -115,13 +115,13 @@ class AdbProtocolHandler implements Runnable { shell(shellCommand, output, input); } - private void hostTransport(DataOutputStream output, String command) throws IOException { + private void hostTransport(DataOutput output, String command) throws IOException { String serial = command.substring("host:transport:".length()); selected = findDevice(serial); output.writeBytes("OKAY"); } - private void hostDevices(DataOutputStream output) throws IOException { + private void hostDevices(DataOutput output) throws IOException { ByteArrayOutputStream tmp = new ByteArrayOutputStream(); DataOutputStream writer = new DataOutputStream(tmp); for (AdbDeviceResponder d : responder.getDevices()) { @@ -131,13 +131,13 @@ class AdbProtocolHandler implements Runnable { send(output, new String(tmp.toByteArray(), StandardCharsets.UTF_8)); } - private void hostTransportAny(DataOutputStream output) throws IOException { + private void hostTransportAny(DataOutput output) throws IOException { // TODO: Check so that exactly one device is selected. selected = responder.getDevices().get(0); output.writeBytes("OKAY"); } - private void hostVersion(DataOutputStream output) throws IOException { + private void hostVersion(DataOutput output) throws IOException { output.writeBytes("OKAY"); send(output, String.format("%04x", responder.getVersion())); } From 71689e47576da11574f724edf289ed4a14805c6c Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 12:51:24 +0200 Subject: [PATCH 08/17] Use StandardCharsets.UTF_8 instead of Charset.forName("utf-8); add charset where needed Application should always work with utf-8 strings regardless of locale. --- src/se/vidstige/jadb/SyncTransport.java | 4 ++-- src/se/vidstige/jadb/Transport.java | 6 +++--- src/se/vidstige/jadb/managers/PropertyManager.java | 3 ++- test/se/vidstige/jadb/test/fakes/FakeAdbServer.java | 6 +++--- test/se/vidstige/jadb/test/unit/MockedTestCases.java | 8 ++++---- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/se/vidstige/jadb/SyncTransport.java b/src/se/vidstige/jadb/SyncTransport.java index 9a36542..22787fe 100644 --- a/src/se/vidstige/jadb/SyncTransport.java +++ b/src/se/vidstige/jadb/SyncTransport.java @@ -1,7 +1,7 @@ package se.vidstige.jadb; import java.io.*; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * Created by vidstige on 2014-03-19. @@ -52,7 +52,7 @@ public class SyncTransport { public String readString(int length) throws IOException { byte[] buffer = new byte[length]; input.readFully(buffer); - return new String(buffer, Charset.forName("utf-8")); + return new String(buffer, StandardCharsets.UTF_8); } public RemoteFileRecord readDirectoryEntry() throws IOException { diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index 701c9de..d3a8a93 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -2,7 +2,7 @@ package se.vidstige.jadb; import java.io.*; import java.net.Socket; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; class Transport { @@ -44,7 +44,7 @@ class Transport { DataInput reader = new DataInputStream(inputStream); byte[] responseBuffer = new byte[length]; reader.readFully(responseBuffer); - return new String(responseBuffer, Charset.forName("utf-8")); + return new String(responseBuffer, StandardCharsets.UTF_8); } public String getCommandLength(String command) { @@ -52,7 +52,7 @@ class Transport { } public void send(String command) throws IOException { - OutputStreamWriter writer = new OutputStreamWriter(outputStream); + OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); writer.write(getCommandLength(command)); writer.write(command); writer.flush(); diff --git a/src/se/vidstige/jadb/managers/PropertyManager.java b/src/se/vidstige/jadb/managers/PropertyManager.java index 7da7003..2dd7e89 100644 --- a/src/se/vidstige/jadb/managers/PropertyManager.java +++ b/src/se/vidstige/jadb/managers/PropertyManager.java @@ -6,6 +6,7 @@ import se.vidstige.jadb.JadbException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -24,7 +25,7 @@ public class PropertyManager { public Map getprop() throws IOException, JadbException { try (BufferedReader bufferedReader = - new BufferedReader(new InputStreamReader(device.executeShell("getprop")))) { + new BufferedReader(new InputStreamReader(device.executeShell("getprop"), StandardCharsets.UTF_8))) { return parseProp(bufferedReader); } } diff --git a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java index f5d2e2c..63ee5d1 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java @@ -11,7 +11,7 @@ import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; import java.net.ProtocolException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -179,7 +179,7 @@ public class FakeAdbServer implements AdbResponder { @Override public void withContent(String content) { - this.content = content.getBytes(Charset.forName("utf-8")); + this.content = content.getBytes(StandardCharsets.UTF_8); } public boolean matches(RemoteFile path) { @@ -212,7 +212,7 @@ public class FakeAdbServer implements AdbResponder { } public void returns(String stdout) { - this.stdout = stdout.getBytes(Charset.forName("utf-8")); + this.stdout = stdout.getBytes(StandardCharsets.UTF_8); } public void writeOutputTo(DataOutputStream stdout) throws IOException { diff --git a/test/se/vidstige/jadb/test/unit/MockedTestCases.java b/test/se/vidstige/jadb/test/unit/MockedTestCases.java index 9b8d58b..87d16a9 100644 --- a/test/se/vidstige/jadb/test/unit/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/unit/MockedTestCases.java @@ -12,7 +12,7 @@ import se.vidstige.jadb.test.fakes.FakeAdbServer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -72,7 +72,7 @@ public class MockedTestCases { server.add("serial-123"); server.expectPush("serial-123", new RemoteFile("/remote/path/abc.txt")).withContent("abc"); JadbDevice device = connection.getDevices().get(0); - ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes()); + ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); device.push(fileContents, parseDate("1981-08-25 13:37"), 0666, new RemoteFile("/remote/path/abc.txt")); } @@ -81,7 +81,7 @@ public class MockedTestCases { server.add("serial-123"); server.expectPush("serial-123", new RemoteFile("/remote/path/abc.txt")).failWith("No such directory"); JadbDevice device = connection.getDevices().get(0); - ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes()); + ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); device.push(fileContents, parseDate("1981-08-25 13:37"), 0666, new RemoteFile("/remote/path/abc.txt")); } @@ -92,7 +92,7 @@ public class MockedTestCases { JadbDevice device = connection.getDevices().get(0); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); device.pull(new RemoteFile("/remote/path/abc.txt"), buffer); - Assert.assertArrayEquals("foobar".getBytes(Charset.forName("utf-8")), buffer.toByteArray()); + Assert.assertArrayEquals("foobar".getBytes(StandardCharsets.UTF_8), buffer.toByteArray()); } @Test From fef9997edf8606f67a82976e4783ba3f75a98881 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 12:53:03 +0200 Subject: [PATCH 09/17] Performance: Make inner classes static --- test/se/vidstige/jadb/test/fakes/FakeAdbServer.java | 6 +++--- test/se/vidstige/jadb/test/fakes/FakeSubprocess.java | 2 +- test/se/vidstige/jadb/test/unit/PackageManagerTest.java | 2 +- test/se/vidstige/jadb/test/unit/PropertyManagerTest.java | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java index 63ee5d1..a0c6db4 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java @@ -91,7 +91,7 @@ public class FakeAdbServer implements AdbResponder { return new ArrayList(devices); } - private class DeviceResponder implements AdbDeviceResponder { + private static class DeviceResponder implements AdbDeviceResponder { private final String serial; private final String type; private List fileExpectations = new ArrayList(); @@ -155,7 +155,7 @@ public class FakeAdbServer implements AdbResponder { org.junit.Assert.assertEquals(0, shellExpectations.size()); } - private class FileExpectation implements ExpectationBuilder { + private static class FileExpectation implements ExpectationBuilder { private final RemoteFile path; private byte[] content; private String failMessage; @@ -199,7 +199,7 @@ public class FakeAdbServer implements AdbResponder { } } - public class ShellExpectation { + public static class ShellExpectation { private final String command; private byte[] stdout; diff --git a/test/se/vidstige/jadb/test/fakes/FakeSubprocess.java b/test/se/vidstige/jadb/test/fakes/FakeSubprocess.java index a09556b..f616962 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeSubprocess.java +++ b/test/se/vidstige/jadb/test/fakes/FakeSubprocess.java @@ -48,7 +48,7 @@ public class FakeSubprocess extends Subprocess { } } - private class Expectation { + private static class Expectation { private final String[] command; private final int exitValue; diff --git a/test/se/vidstige/jadb/test/unit/PackageManagerTest.java b/test/se/vidstige/jadb/test/unit/PackageManagerTest.java index 4f72f47..65b3d43 100644 --- a/test/se/vidstige/jadb/test/unit/PackageManagerTest.java +++ b/test/se/vidstige/jadb/test/unit/PackageManagerTest.java @@ -15,7 +15,7 @@ import java.util.List; import static org.junit.Assert.assertEquals; public class PackageManagerTest { - private final String DEVICE_SERIAL = "serial-123"; + private static final String DEVICE_SERIAL = "serial-123"; private FakeAdbServer server; private JadbConnection connection; diff --git a/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java b/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java index 1b59874..d97ae3c 100644 --- a/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java +++ b/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java @@ -14,7 +14,7 @@ import java.util.Map; import static org.junit.Assert.assertEquals; public class PropertyManagerTest { - private final String DEVICE_SERIAL = "serial-123"; + private static final String DEVICE_SERIAL = "serial-123"; private FakeAdbServer server; private JadbConnection connection; From 048291c3cabf47d3c9fad90453fbe7a978a5df33 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:02:40 +0200 Subject: [PATCH 10/17] Cleanup: remove unused member --- test/se/vidstige/jadb/test/unit/PackageManagerTest.java | 5 +---- test/se/vidstige/jadb/test/unit/PropertyManagerTest.java | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/test/se/vidstige/jadb/test/unit/PackageManagerTest.java b/test/se/vidstige/jadb/test/unit/PackageManagerTest.java index 65b3d43..e406099 100644 --- a/test/se/vidstige/jadb/test/unit/PackageManagerTest.java +++ b/test/se/vidstige/jadb/test/unit/PackageManagerTest.java @@ -18,7 +18,6 @@ public class PackageManagerTest { private static final String DEVICE_SERIAL = "serial-123"; private FakeAdbServer server; - private JadbConnection connection; private JadbDevice device; @Before @@ -26,9 +25,7 @@ public class PackageManagerTest { server = new FakeAdbServer(15037); server.start(); server.add(DEVICE_SERIAL); - connection = new JadbConnection("localhost", 15037); - - device = connection.getDevices().get(0); + device = new JadbConnection("localhost", 15037).getDevices().get(0); } @After diff --git a/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java b/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java index d97ae3c..ff0602b 100644 --- a/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java +++ b/test/se/vidstige/jadb/test/unit/PropertyManagerTest.java @@ -17,7 +17,6 @@ public class PropertyManagerTest { private static final String DEVICE_SERIAL = "serial-123"; private FakeAdbServer server; - private JadbConnection connection; private JadbDevice device; @Before @@ -25,9 +24,7 @@ public class PropertyManagerTest { server = new FakeAdbServer(15037); server.start(); server.add(DEVICE_SERIAL); - connection = new JadbConnection("localhost", 15037); - - device = connection.getDevices().get(0); + device = new JadbConnection("localhost", 15037).getDevices().get(0); } @After From 6dd5ad2566f4c22d7bb24c72741bf212bc1fad10 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:07:10 +0200 Subject: [PATCH 11/17] Remove public from interfaces --- src/se/vidstige/jadb/DeviceDetectionListener.java | 4 ++-- src/se/vidstige/jadb/ITransportFactory.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/se/vidstige/jadb/DeviceDetectionListener.java b/src/se/vidstige/jadb/DeviceDetectionListener.java index 2416998..2e23853 100644 --- a/src/se/vidstige/jadb/DeviceDetectionListener.java +++ b/src/se/vidstige/jadb/DeviceDetectionListener.java @@ -3,7 +3,7 @@ package se.vidstige.jadb; import java.util.List; public interface DeviceDetectionListener { - public void onDetect(List devices); - public void onException(Exception e); + void onDetect(List devices); + void onException(Exception e); } diff --git a/src/se/vidstige/jadb/ITransportFactory.java b/src/se/vidstige/jadb/ITransportFactory.java index 0f73a75..2d34aa5 100644 --- a/src/se/vidstige/jadb/ITransportFactory.java +++ b/src/se/vidstige/jadb/ITransportFactory.java @@ -6,5 +6,5 @@ import java.io.IOException; * Created by Törcsi on 2016. 03. 01.. */ public interface ITransportFactory { - public Transport createTransport() throws IOException; + Transport createTransport() throws IOException; } From a735c0f693998a4b2ab491ec0eb5de9e13c5dfdc Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:17:09 +0200 Subject: [PATCH 12/17] Java 7: use diamond operator --- test/se/vidstige/jadb/test/fakes/FakeAdbServer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java index a0c6db4..c817baa 100644 --- a/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java +++ b/test/se/vidstige/jadb/test/fakes/FakeAdbServer.java @@ -20,7 +20,7 @@ import java.util.List; */ public class FakeAdbServer implements AdbResponder { private final AdbServer server; - private final List devices = new ArrayList(); + private final List devices = new ArrayList<>(); public FakeAdbServer(int port) { server = new AdbServer(this, port); @@ -94,8 +94,8 @@ public class FakeAdbServer implements AdbResponder { private static class DeviceResponder implements AdbDeviceResponder { private final String serial; private final String type; - private List fileExpectations = new ArrayList(); - private List shellExpectations = new ArrayList(); + private List fileExpectations = new ArrayList<>(); + private List shellExpectations = new ArrayList<>(); private DeviceResponder(String serial, String type) { this.serial = serial; From bdbb454acb00280c6131187729bb0138938b29db Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:18:09 +0200 Subject: [PATCH 13/17] Java 7: collapse catch clauses --- .../vidstige/jadb/test/integration/RealDeviceTestCases.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java index 9bb1339..22df5d7 100644 --- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java @@ -31,9 +31,7 @@ public class RealDeviceTestCases { public static void tryToStartAdbServer() { try { new AdbServerLauncher(new Subprocess(), System.getenv()).launch(); - } catch (IOException e) { - System.out.println("Could not start adb-server"); - } catch (InterruptedException e) { + } catch (IOException | InterruptedException e) { System.out.println("Could not start adb-server"); } } From adb0cca09abccb56a409c7d488c45014f68d0eb6 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:22:55 +0200 Subject: [PATCH 14/17] Convert to try-with-resources --- .../jadb/test/integration/RealDeviceTestCases.java | 6 +----- .../vidstige/jadb/test/unit/AdbOutputStreamFixture.java | 9 +++------ 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java index 22df5d7..9913e9d 100644 --- a/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/integration/RealDeviceTestCases.java @@ -111,13 +111,9 @@ public class RealDeviceTestCases { @Test public void testScreenshot() throws Exception { JadbDevice any = jadb.getAnyDevice(); - FileOutputStream outputStream = null; - try { - outputStream = new FileOutputStream(temporaryFolder.newFile("screenshot.png")); + try (FileOutputStream outputStream = new FileOutputStream(temporaryFolder.newFile("screenshot.png"))) { InputStream stdout = any.executeShell("screencap", "-p"); Stream.copy(stdout, outputStream); - } finally { - if (outputStream != null) outputStream.close(); } } diff --git a/test/se/vidstige/jadb/test/unit/AdbOutputStreamFixture.java b/test/se/vidstige/jadb/test/unit/AdbOutputStreamFixture.java index 09a012d..0ecb7f7 100644 --- a/test/se/vidstige/jadb/test/unit/AdbOutputStreamFixture.java +++ b/test/se/vidstige/jadb/test/unit/AdbOutputStreamFixture.java @@ -12,12 +12,9 @@ public class AdbOutputStreamFixture { private byte[] passthrough(byte[] input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); - OutputStream sut = new AdbFilterOutputStream(output); - try { - sut.write(input); - sut.flush(); - } finally { - sut.close(); + try (OutputStream sut = new AdbFilterOutputStream(output)) { + sut.write(input); + sut.flush(); } return output.toByteArray(); } From 3fd68d337f9aab1b2ac9b52f9f663192e443220c Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:26:37 +0200 Subject: [PATCH 15/17] Various small fixes found by IDEA inspection --- src/se/vidstige/jadb/HostConnectionCommand.java | 2 +- src/se/vidstige/jadb/managers/PackageManager.java | 2 +- src/se/vidstige/jadb/server/AdbProtocolHandler.java | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/se/vidstige/jadb/HostConnectionCommand.java b/src/se/vidstige/jadb/HostConnectionCommand.java index 78eeae2..6428269 100644 --- a/src/se/vidstige/jadb/HostConnectionCommand.java +++ b/src/se/vidstige/jadb/HostConnectionCommand.java @@ -61,7 +61,7 @@ public class HostConnectionCommand { private String extractError(String response) { int lastColon = response.lastIndexOf(':'); if (lastColon != -1) { - return response.substring(lastColon, response.length()); + return response.substring(lastColon); } else { return response; } diff --git a/src/se/vidstige/jadb/managers/PackageManager.java b/src/se/vidstige/jadb/managers/PackageManager.java index 40d0fde..d9a3262 100644 --- a/src/se/vidstige/jadb/managers/PackageManager.java +++ b/src/se/vidstige/jadb/managers/PackageManager.java @@ -55,7 +55,7 @@ public class PackageManager { arguments.add("install"); arguments.addAll(extraArguments); arguments.add(remote.getPath()); - InputStream s = device.executeShell("pm", arguments.toArray(new String[arguments.size()])); + InputStream s = device.executeShell("pm", arguments.toArray(new String[0])); String result = Stream.readAll(s, StandardCharsets.UTF_8); remove(remote); verifyOperation("install", apkFile.getName(), result); diff --git a/src/se/vidstige/jadb/server/AdbProtocolHandler.java b/src/se/vidstige/jadb/server/AdbProtocolHandler.java index 02f2469..fc71a13 100644 --- a/src/se/vidstige/jadb/server/AdbProtocolHandler.java +++ b/src/se/vidstige/jadb/server/AdbProtocolHandler.java @@ -41,6 +41,7 @@ class AdbProtocolHandler implements Runnable { DataInputStream input = new DataInputStream(socket.getInputStream()); DataOutputStream output = new DataOutputStream(socket.getOutputStream()) ) { + //noinspection StatementWithEmptyBody while (processCommand(input, output)) { // nothing to do here } From e4f41371082ad9637431d859c66951b1a6b37472 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:30:21 +0200 Subject: [PATCH 16/17] Remove unnecessary escapes from RegExp --- src/se/vidstige/jadb/managers/PropertyManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/se/vidstige/jadb/managers/PropertyManager.java b/src/se/vidstige/jadb/managers/PropertyManager.java index 2dd7e89..081df45 100644 --- a/src/se/vidstige/jadb/managers/PropertyManager.java +++ b/src/se/vidstige/jadb/managers/PropertyManager.java @@ -16,7 +16,7 @@ import java.util.regex.Pattern; * A class which works with properties, uses getprop and setprop methods of android shell */ public class PropertyManager { - private final Pattern pattern = Pattern.compile("^\\[([a-zA-Z0-9_.-]*)\\]:.\\[([^\\[\\]]*)\\]"); + private final Pattern pattern = Pattern.compile("^\\[([a-zA-Z0-9_.-]*)]:.\\[([^\\[\\]]*)]"); private final JadbDevice device; public PropertyManager(JadbDevice device) { From 3db1b31a0c4f5e74503cf43bbdcf1d1dc51b8ab5 Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 13:30:31 +0200 Subject: [PATCH 17/17] Typos --- CONTRIBUTING.md | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8bc1118..3ac9266 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ # Contributing # ## Why contributing? ## -The original author and all users of this library are very greatful for your contribution +The original author and all users of this library are very grateful for your contribution to this Open Source Project. Also most employers value people active in the Open Source community. @@ -25,4 +25,4 @@ your proposed change meets, or exceeds, the quality of the jadb source code. full terse. * Newline at end of file - This makes `cat`-ing files, etc easier. -Happy coding! I, the original author, and all users are greatful for your contribution. :-) \ No newline at end of file +Happy coding! I, the original author, and all users are grateful for your contribution. :-) \ No newline at end of file diff --git a/README.md b/README.md index 9468b4e..0c0ed3d 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ An overview of the protocol can be found here: [Overview](https://android.google A list of the available commands that a ADB Server may accept can be found here: [Services](https://android.googlesource.com/platform/system/adb/+/master/SERVICES.TXT) -The description for the protocol for transfering files can be found here: [SYNC.TXT](https://android.googlesource.com/platform/system/adb/+/master/SYNC.TXT). +The description for the protocol for transferring files can be found here: [SYNC.TXT](https://android.googlesource.com/platform/system/adb/+/master/SYNC.TXT). ## Using JADB in your application ##