mirror of
https://github.com/revanced/jadb.git
synced 2025-02-11 01:26:47 +01:00
Adding support for pulling files from device
This commit is contained in:
parent
e09b429b86
commit
fef216f3ce
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user