2013-07-25 21:38:44 +02:00
|
|
|
package se.vidstige.jadb;
|
|
|
|
|
2014-03-19 10:06:40 +01:00
|
|
|
import java.io.*;
|
2013-07-25 23:36:18 +02:00
|
|
|
import java.net.Socket;
|
2018-08-06 12:51:24 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2013-07-25 21:38:44 +02:00
|
|
|
|
2018-08-06 09:51:50 +02:00
|
|
|
class Transport implements Closeable {
|
2013-07-25 21:38:44 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
private final OutputStream outputStream;
|
|
|
|
private final InputStream inputStream;
|
2018-08-06 14:04:31 +02:00
|
|
|
private final DataInputStream dataInput;
|
|
|
|
private final DataOutputStream dataOutput;
|
2013-07-25 21:38:44 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
private Transport(OutputStream outputStream, InputStream inputStream) {
|
|
|
|
this.outputStream = outputStream;
|
|
|
|
this.inputStream = inputStream;
|
2018-08-06 14:04:31 +02:00
|
|
|
this.dataInput = new DataInputStream(inputStream);
|
|
|
|
this.dataOutput = new DataOutputStream(outputStream);
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
2013-07-25 21:38:44 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public Transport(Socket socket) throws IOException {
|
|
|
|
this(socket.getOutputStream(), socket.getInputStream());
|
|
|
|
}
|
2013-07-25 23:36:18 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public String readString() throws IOException {
|
|
|
|
String encodedLength = readString(4);
|
|
|
|
int length = Integer.parseInt(encodedLength, 16);
|
|
|
|
return readString(length);
|
|
|
|
}
|
2016-02-28 03:04:59 +01:00
|
|
|
|
2016-04-27 20:28:21 +02:00
|
|
|
public void readResponseTo(OutputStream output) throws IOException {
|
|
|
|
Stream.copy(inputStream, output);
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
2016-02-28 10:54:33 +01:00
|
|
|
|
2016-04-27 20:28:21 +02:00
|
|
|
public InputStream getInputStream() {
|
|
|
|
return inputStream;
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
2016-02-28 10:54:33 +01:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public void verifyResponse() throws IOException, JadbException {
|
|
|
|
String response = readString(4);
|
|
|
|
if (!"OKAY".equals(response)) {
|
2014-03-18 21:33:18 +01:00
|
|
|
String error = readString();
|
|
|
|
throw new JadbException("command failed: " + error);
|
|
|
|
}
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
2013-07-25 21:38:44 +02:00
|
|
|
|
2016-03-20 09:13:30 +01:00
|
|
|
public String readString(int length) throws IOException {
|
|
|
|
byte[] responseBuffer = new byte[length];
|
2018-08-06 14:04:31 +02:00
|
|
|
dataInput.readFully(responseBuffer);
|
2018-08-06 12:51:24 +02:00
|
|
|
return new String(responseBuffer, StandardCharsets.UTF_8);
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
2013-07-25 21:38:44 +02:00
|
|
|
|
2018-09-11 20:15:06 +02:00
|
|
|
private String getCommandLength(String command) {
|
2021-03-02 10:13:09 +01:00
|
|
|
return String.format("%04x", command.getBytes().length);
|
2016-03-20 09:13:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void send(String command) throws IOException {
|
2018-08-06 12:51:24 +02:00
|
|
|
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
|
2016-03-20 09:13:30 +01:00
|
|
|
writer.write(getCommandLength(command));
|
|
|
|
writer.write(command);
|
|
|
|
writer.flush();
|
|
|
|
}
|
2014-03-19 10:06:40 +01:00
|
|
|
|
|
|
|
public SyncTransport startSync() throws IOException, JadbException {
|
|
|
|
send("sync:");
|
|
|
|
verifyResponse();
|
2018-08-06 14:04:31 +02:00
|
|
|
return new SyncTransport(dataOutput, dataInput);
|
2014-03-19 10:06:40 +01:00
|
|
|
}
|
2014-03-20 10:31:13 +01:00
|
|
|
|
2022-06-11 18:14:23 +02:00
|
|
|
public ShellProtocolTransport startShellProtocol(String command) throws IOException, JadbException {
|
|
|
|
send("shell,v2,raw:" + command);
|
|
|
|
verifyResponse();
|
|
|
|
return new ShellProtocolTransport(dataOutput, dataInput);
|
|
|
|
}
|
|
|
|
|
2018-08-06 09:51:50 +02:00
|
|
|
@Override
|
2014-03-20 10:31:13 +01:00
|
|
|
public void close() throws IOException {
|
2018-08-06 14:04:31 +02:00
|
|
|
dataInput.close();
|
|
|
|
dataOutput.close();
|
2014-03-20 10:31:13 +01:00
|
|
|
}
|
2013-07-25 21:38:44 +02:00
|
|
|
}
|