Adding support for pushing files to device.

This commit is contained in:
Samuel Carlsson 2014-03-19 21:15:40 +01:00
parent 6b8c330195
commit de0c65d922
3 changed files with 59 additions and 7 deletions

View File

@ -1,6 +1,6 @@
package se.vidstige.jadb;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@ -47,7 +47,7 @@ public class AndroidDevice {
public String getState() throws IOException, JadbException {
selectTransport();
transport.send(getPrefix() + "get-state");
transport.send("get-state");
transport.verifyResponse();
return transport.readString();
}
@ -79,8 +79,23 @@ public class AndroidDevice {
return result;
}
private int getMode(File file)
{
return 0664;
}
public void push(String localPath, String remotePath) throws IOException, JadbException {
selectTransport();
SyncTransport sync = transport.startSync();
File local = new File(localPath);
sync.send("SEND", remotePath + "," + Integer.toString(getMode(local)));
FileInputStream fileStream = new FileInputStream(local);
sync.sendStream(fileStream);
fileStream.close();
sync.sendStatus("DONE", (int) local.lastModified());
sync.verifyStatus();
}
private void send(String command) throws IOException, JadbException {
@ -88,11 +103,6 @@ public class AndroidDevice {
transport.verifyResponse();
}
private String getPrefix() {
//return "host-serial:" + serial + ":";
return "host-local:";
}
@Override
public String toString()
{

View File

@ -23,6 +23,25 @@ class SyncTransport {
output.writeBytes(name);
}
public void sendStatus(String statusCode, int length) throws IOException {
output.writeBytes(statusCode);
output.writeInt(Integer.reverseBytes(length));
}
public void verifyStatus() throws IOException, JadbException {
String status = readString(4);
int length = readInt();
if ("FAIL".equals(status))
{
String error = readString(length);
throw new JadbException(error);
}
if (!"OKAY".equals(status))
{
throw new JadbException("Unknown error: " + status);
}
}
private int readInt() throws IOException {
return Integer.reverseBytes(input.readInt());
}
@ -44,4 +63,19 @@ class SyncTransport {
if (!"DENT".equals(id)) return RemoteFile.DONE;
return new RemoteFile(id, name, mode, size, time);
}
private void sendBuffer(byte[] buffer, int offset, int length) throws IOException {
output.writeBytes("DATA");
output.writeInt(Integer.reverseBytes(length));
output.write(buffer, offset, length);
}
public void sendStream(InputStream in) throws IOException {
byte[] buffer = new byte[1024 * 64];
int n = in.read(buffer);
while (n != -1) {
sendBuffer(buffer, 0, n);
n = in.read(buffer);
}
}
}

View File

@ -38,4 +38,12 @@ public class JadbTestCases {
System.out.println(f.getName());
}
}
@Test
public void testPushFile() throws Exception
{
JadbConnection jadb = new JadbConnection();
AndroidDevice any = jadb.getAnyDevice();
any.push("README.md", "/sdcard/README.md");
}
}