Merge pull request #95 from janosvitok/make-Transport-closeable-take-2

Make transport closeable take 2
This commit is contained in:
Samuel Carlsson 2018-11-12 13:38:32 +01:00 committed by GitHub
commit 075f29dd82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 65 deletions

View File

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

View File

@ -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());
}
}
/** <p>Execute a shell command.</p>
@ -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<RemoteFile> 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<RemoteFile> result = new ArrayList<>();
for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry()) {
result.add(dent);
List<RemoteFile> 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 {

View File

@ -11,11 +11,6 @@ public class SyncTransport {
private final DataOutput output;
private final DataInput input;
public SyncTransport(OutputStream outputStream, InputStream inputStream) {
output = new DataOutputStream(outputStream);
input = new DataInputStream(inputStream);
}
public SyncTransport(DataOutput outputStream, DataInput inputStream) {
output = outputStream;
input = inputStream;

View File

@ -4,14 +4,18 @@ import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
class Transport {
class Transport implements Closeable {
private final OutputStream outputStream;
private final InputStream inputStream;
private final DataInputStream dataInput;
private final DataOutputStream dataOutput;
private Transport(OutputStream outputStream, InputStream inputStream) {
this.outputStream = outputStream;
this.inputStream = inputStream;
this.dataInput = new DataInputStream(inputStream);
this.dataOutput = new DataOutputStream(outputStream);
}
public Transport(Socket socket) throws IOException {
@ -41,9 +45,8 @@ class Transport {
}
public String readString(int length) throws IOException {
DataInput reader = new DataInputStream(inputStream);
byte[] responseBuffer = new byte[length];
reader.readFully(responseBuffer);
dataInput.readFully(responseBuffer);
return new String(responseBuffer, StandardCharsets.UTF_8);
}
@ -61,11 +64,12 @@ class Transport {
public SyncTransport startSync() throws IOException, JadbException {
send("sync:");
verifyResponse();
return new SyncTransport(outputStream, inputStream);
return new SyncTransport(dataOutput, dataInput);
}
@Override
public void close() throws IOException {
inputStream.close();
outputStream.close();
dataInput.close();
dataOutput.close();
}
}