From fef216f3ce9ea4a27ee7cdf43e29abd371791e05 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Thu, 20 Mar 2014 10:10:35 +0100 Subject: [PATCH] Adding support for pulling files from device --- src/se/vidstige/jadb/JadbDevice.java | 10 ++++++++++ src/se/vidstige/jadb/SyncTransport.java | 20 +++++++++++++++++-- test/se/vidstige/jadb/test/JadbTestCases.java | 8 ++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 4aa7c06..372b2b2 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -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(); diff --git a/src/se/vidstige/jadb/SyncTransport.java b/src/se/vidstige/jadb/SyncTransport.java index 10c74fe..cfb4d77 100644 --- a/src/se/vidstige/jadb/SyncTransport.java +++ b/src/se/vidstige/jadb/SyncTransport.java @@ -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); + } + } } diff --git a/test/se/vidstige/jadb/test/JadbTestCases.java b/test/se/vidstige/jadb/test/JadbTestCases.java index 8d70cbd..3fa699a 100644 --- a/test/se/vidstige/jadb/test/JadbTestCases.java +++ b/test/se/vidstige/jadb/test/JadbTestCases.java @@ -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"); + } }