mirror of
https://github.com/revanced/jadb.git
synced 2025-05-22 19:08:52 +02:00
Add try-with-resources for Transport
This commit is contained in:
parent
6a4f5c38a1
commit
9d7f4f7846
@ -28,41 +28,34 @@ public class JadbConnection implements ITransportFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getHostVersion() throws IOException, JadbException {
|
public String getHostVersion() throws IOException, JadbException {
|
||||||
Transport main = createTransport();
|
try (Transport transport = createTransport()) {
|
||||||
main.send("host:version");
|
transport.send("host:version");
|
||||||
main.verifyResponse();
|
transport.verifyResponse();
|
||||||
String version = main.readString();
|
return transport.readString();
|
||||||
main.close();
|
}
|
||||||
return version;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress connectToTcpDevice(InetSocketAddress inetSocketAddress)
|
public InetSocketAddress connectToTcpDevice(InetSocketAddress inetSocketAddress)
|
||||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
Transport transport = createTransport();
|
try (Transport transport = createTransport()) {
|
||||||
try {
|
|
||||||
return new HostConnectToRemoteTcpDevice(transport).connect(inetSocketAddress);
|
return new HostConnectToRemoteTcpDevice(transport).connect(inetSocketAddress);
|
||||||
} finally {
|
|
||||||
transport.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public InetSocketAddress disconnectFromTcpDevice(InetSocketAddress tcpAddressEntity)
|
public InetSocketAddress disconnectFromTcpDevice(InetSocketAddress tcpAddressEntity)
|
||||||
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
throws IOException, JadbException, ConnectionToRemoteDeviceException {
|
||||||
Transport transport = createTransport();
|
try (Transport transport = createTransport()) {
|
||||||
try {
|
|
||||||
return new HostDisconnectFromRemoteTcpDevice(transport).disconnect(tcpAddressEntity);
|
return new HostDisconnectFromRemoteTcpDevice(transport).disconnect(tcpAddressEntity);
|
||||||
} finally {
|
|
||||||
transport.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
public List<JadbDevice> getDevices() throws IOException, JadbException {
|
||||||
Transport devices = createTransport();
|
try (Transport transport = createTransport()) {
|
||||||
devices.send("host:devices");
|
transport.send("host:devices");
|
||||||
devices.verifyResponse();
|
transport.verifyResponse();
|
||||||
String body = devices.readString();
|
String body = transport.readString();
|
||||||
devices.close();
|
return parseDevices(body);
|
||||||
return parseDevices(body);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException {
|
public DeviceWatcher createDeviceWatcher(DeviceDetectionListener listener) throws IOException, JadbException {
|
||||||
|
@ -47,7 +47,14 @@ public class JadbDevice {
|
|||||||
|
|
||||||
private Transport getTransport() throws IOException, JadbException {
|
private Transport getTransport() throws IOException, JadbException {
|
||||||
Transport transport = transportFactory.createTransport();
|
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;
|
return transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,12 +63,10 @@ public class JadbDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public State getState() throws IOException, JadbException {
|
public State getState() throws IOException, JadbException {
|
||||||
Transport transport = transportFactory.createTransport();
|
try (Transport transport = transportFactory.createTransport()) {
|
||||||
send(transport, serial == null ? "host:get-state" : "host-serial:" + serial + ":get-state");
|
send(transport, serial == null ? "host:get-state" : "host-serial:" + serial + ":get-state");
|
||||||
|
return convertState(transport.readString());
|
||||||
State state = convertState(transport.readString());
|
}
|
||||||
transport.close();
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** <p>Execute a shell command.</p>
|
/** <p>Execute a shell command.</p>
|
||||||
@ -88,16 +93,14 @@ public class JadbDevice {
|
|||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void executeShell(OutputStream output, String command, String... args) throws IOException, JadbException {
|
public void executeShell(OutputStream output, String command, String... args) throws IOException, JadbException {
|
||||||
Transport transport = getTransport();
|
try (Transport transport = getTransport()) {
|
||||||
StringBuilder shellLine = buildCmdLine(command, args);
|
StringBuilder shellLine = buildCmdLine(command, args);
|
||||||
send(transport, "shell:" + shellLine.toString());
|
send(transport, "shell:" + shellLine.toString());
|
||||||
if (output != null) {
|
if (output == null)
|
||||||
AdbFilterOutputStream out = new AdbFilterOutputStream(output);
|
return;
|
||||||
try {
|
|
||||||
transport.readResponseTo(out);
|
AdbFilterOutputStream out = new AdbFilterOutputStream(output);
|
||||||
} finally {
|
transport.readResponseTo(out);
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,26 +140,28 @@ public class JadbDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
||||||
Transport transport = getTransport();
|
try (Transport transport = getTransport()) {
|
||||||
SyncTransport sync = transport.startSync();
|
SyncTransport sync = transport.startSync();
|
||||||
sync.send("LIST", remotePath);
|
sync.send("LIST", remotePath);
|
||||||
|
|
||||||
List<RemoteFile> result = new ArrayList<>();
|
List<RemoteFile> result = new ArrayList<>();
|
||||||
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);
|
result.add(dent);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
Transport transport = getTransport();
|
try (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));
|
||||||
|
|
||||||
sync.sendStream(source);
|
sync.sendStream(source);
|
||||||
|
|
||||||
sync.sendStatus("DONE", (int) lastModified);
|
sync.sendStatus("DONE", (int) lastModified);
|
||||||
sync.verifyStatus();
|
sync.verifyStatus();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void push(File local, RemoteFile remote) throws IOException, JadbException {
|
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 {
|
public void pull(RemoteFile remote, OutputStream destination) throws IOException, JadbException {
|
||||||
Transport transport = getTransport();
|
try (Transport transport = getTransport()) {
|
||||||
SyncTransport sync = transport.startSync();
|
SyncTransport sync = transport.startSync();
|
||||||
sync.send("RECV", remote.getPath());
|
sync.send("RECV", remote.getPath());
|
||||||
|
|
||||||
sync.readChunksTo(destination);
|
sync.readChunksTo(destination);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pull(RemoteFile remote, File local) throws IOException, JadbException {
|
public void pull(RemoteFile remote, File local) throws IOException, JadbException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user