Adding support for pulling files from device

This commit is contained in:
Samuel Carlsson 2014-03-20 10:10:35 +01:00
parent e09b429b86
commit fef216f3ce
3 changed files with 36 additions and 2 deletions

View File

@ -102,6 +102,16 @@ public class JadbDevice {
sync.verifyStatus();
}
public void pull(String remotePath, String localPath) throws IOException, JadbException {
ensureTransportIsSelected();
SyncTransport sync = transport.startSync();
sync.send("RECV", remotePath);
FileOutputStream fileStream = new FileOutputStream(new File(localPath));
sync.readChunksTo(fileStream);
fileStream.close();
}
private void send(String command) throws IOException, JadbException {
transport.send(command);
transport.verifyResponse();

View File

@ -64,18 +64,34 @@ class SyncTransport {
return new RemoteFile(id, name, mode, size, time);
}
private void sendBuffer(byte[] buffer, int offset, int length) throws IOException {
private void sendChunk(byte[] buffer, int offset, int length) throws IOException {
output.writeBytes("DATA");
output.writeInt(Integer.reverseBytes(length));
output.write(buffer, offset, length);
}
private int readChunk(byte[] buffer) throws IOException {
String id = readString(4);
if (!"DATA".equals(id)) return -1;
int n = readInt();
return input.read(buffer, 0, n);
}
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);
sendChunk(buffer, 0, n);
n = in.read(buffer);
}
}
public void readChunksTo(OutputStream stream) throws IOException {
byte[] buffer = new byte[1024 * 64];
int n = readChunk(buffer);
while (n != -1) {
stream.write(buffer, 0, n);
n = readChunk(buffer);
}
}
}

View File

@ -44,4 +44,12 @@ public class JadbTestCases {
JadbDevice any = jadb.getAnyDevice();
any.push("README.md", "/sdcard/README.md");
}
@Test
public void testPullFile() throws Exception
{
JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice();
any.pull("/sdcard/README.md", "foobar.md");
}
}