From 9d7f4f784681f2fbcaab5e2beb7829528a10933f Mon Sep 17 00:00:00 2001 From: Jano Svitok Date: Mon, 6 Aug 2018 11:09:27 +0200 Subject: [PATCH] Add try-with-resources for Transport --- src/se/vidstige/jadb/JadbConnection.java | 33 +++++------ src/se/vidstige/jadb/JadbDevice.java | 74 +++++++++++++----------- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 556b761..c8509e1 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -28,41 +28,34 @@ public class JadbConnection implements ITransportFactory { } public String getHostVersion() throws IOException, JadbException { - Transport main = createTransport(); - main.send("host:version"); - main.verifyResponse(); - String version = main.readString(); - main.close(); - return version; + try (Transport transport = createTransport()) { + transport.send("host:version"); + transport.verifyResponse(); + return transport.readString(); + } } public InetSocketAddress connectToTcpDevice(InetSocketAddress inetSocketAddress) throws IOException, JadbException, ConnectionToRemoteDeviceException { - Transport transport = createTransport(); - try { + try (Transport transport = createTransport()) { return new HostConnectToRemoteTcpDevice(transport).connect(inetSocketAddress); - } finally { - transport.close(); } } public InetSocketAddress disconnectFromTcpDevice(InetSocketAddress tcpAddressEntity) throws IOException, JadbException, ConnectionToRemoteDeviceException { - Transport transport = createTransport(); - try { + try (Transport transport = createTransport()) { return new HostDisconnectFromRemoteTcpDevice(transport).disconnect(tcpAddressEntity); - } finally { - transport.close(); } } public List getDevices() throws IOException, JadbException { - Transport devices = createTransport(); - devices.send("host:devices"); - devices.verifyResponse(); - String body = devices.readString(); - devices.close(); - return parseDevices(body); + try (Transport transport = createTransport()) { + transport.send("host:devices"); + transport.verifyResponse(); + String body = transport.readString(); + return parseDevices(body); + } } public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException { diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 0ca5295..602fbbd 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -47,7 +47,14 @@ public class JadbDevice { private Transport getTransport() throws IOException, JadbException { Transport transport = transportFactory.createTransport(); - send(transport, serial == null ? "host:transport-any" : "host:transport:" + serial ); + // Do not use try-with-resources here. We want to return unclosed Transport and it is up to caller + // to close it. Here we close it only in case of exception. + try { + send(transport, serial == null ? "host:transport-any" : "host:transport:" + serial ); + } catch (IOException|JadbException e) { + transport.close(); + throw e; + } return transport; } @@ -56,12 +63,10 @@ public class JadbDevice { } public State getState() throws IOException, JadbException { - Transport transport = transportFactory.createTransport(); - send(transport, serial == null ? "host:get-state" : "host-serial:" + serial + ":get-state"); - - State state = convertState(transport.readString()); - transport.close(); - return state; + try (Transport transport = transportFactory.createTransport()) { + send(transport, serial == null ? "host:get-state" : "host-serial:" + serial + ":get-state"); + return convertState(transport.readString()); + } } /**

Execute a shell command.

@@ -88,16 +93,14 @@ public class JadbDevice { */ @Deprecated public void executeShell(OutputStream output, String command, String... args) throws IOException, JadbException { - Transport transport = getTransport(); - StringBuilder shellLine = buildCmdLine(command, args); - send(transport, "shell:" + shellLine.toString()); - if (output != null) { - AdbFilterOutputStream out = new AdbFilterOutputStream(output); - try { - transport.readResponseTo(out); - } finally { - out.close(); - } + try (Transport transport = getTransport()) { + StringBuilder shellLine = buildCmdLine(command, args); + send(transport, "shell:" + shellLine.toString()); + if (output == null) + return; + + AdbFilterOutputStream out = new AdbFilterOutputStream(output); + transport.readResponseTo(out); } } @@ -137,26 +140,28 @@ public class JadbDevice { } public List list(String remotePath) throws IOException, JadbException { - Transport transport = getTransport(); - SyncTransport sync = transport.startSync(); - sync.send("LIST", remotePath); + try (Transport transport = getTransport()) { + SyncTransport sync = transport.startSync(); + sync.send("LIST", remotePath); - List result = new ArrayList<>(); - for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) { - result.add(dent); + List result = new ArrayList<>(); + for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) { + result.add(dent); + } + return result; } - return result; } public void push(InputStream source, long lastModified, int mode, RemoteFile remote) throws IOException, JadbException { - Transport transport = getTransport(); - SyncTransport sync = transport.startSync(); - sync.send("SEND", remote.getPath() + "," + Integer.toString(mode)); + try (Transport transport = getTransport()) { + SyncTransport sync = transport.startSync(); + sync.send("SEND", remote.getPath() + "," + Integer.toString(mode)); - sync.sendStream(source); + sync.sendStream(source); - sync.sendStatus("DONE", (int) lastModified); - sync.verifyStatus(); + sync.sendStatus("DONE", (int) lastModified); + sync.verifyStatus(); + } } public void push(File local, RemoteFile remote) throws IOException, JadbException { @@ -166,11 +171,12 @@ public class JadbDevice { } public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException { - Transport transport = getTransport(); - SyncTransport sync = transport.startSync(); - sync.send("RECV", remote.getPath()); + try (Transport transport = getTransport()) { + SyncTransport sync = transport.startSync(); + sync.send("RECV", remote.getPath()); - sync.readChunksTo(destination); + sync.readChunksTo(destination); + } } public void pull(RemoteFile remote, File local) throws IOException, JadbException {