From 92506f643db12b3f7b27624a29913067ce1c9c5c Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Wed, 2 Mar 2016 21:06:19 +0100 Subject: [PATCH] Using streams rather than byte arrays. --- src/se/vidstige/jadb/JadbDevice.java | 16 +++----- src/se/vidstige/jadb/Transport.java | 33 +++++---------- .../jadb/test/RealDeviceTestCases.java | 40 +++++-------------- 3 files changed, 25 insertions(+), 64 deletions(-) diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index ebcce92..c66d858 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -52,19 +52,12 @@ public class JadbDevice { return transport.readString(); } - public String executeShell(String command, String ... args) throws IOException, JadbException { - execShell(command, args); - String ret = this.transport.readResponse(); - return ret; + public void executeShell(OutputStream stdout, String command, String ... args) throws IOException, JadbException { + executeShell(command, args); + this.transport.readResponseTo(stdout); } - public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException { - execShell(command, args); - byte[] ret = this.transport.readResponseAsArray(); - return ret; - } - - private void execShell(String command, String[] args) throws IOException, JadbException { + private void executeShell(String command, String[] args) throws IOException, JadbException { getTransport(); StringBuilder shellLine = new StringBuilder(command); for (String arg : args) @@ -92,6 +85,7 @@ public class JadbDevice { private int getMode(File file) { + //noinspection OctalInteger return 0664; } diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index 6cccb2b..d3954aa 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -1,8 +1,5 @@ package se.vidstige.jadb; - -import org.apache.commons.io.IOUtils; - import java.io.*; import java.net.Socket; import java.nio.charset.Charset; @@ -32,12 +29,17 @@ class Transport { return readString(length); } - public String readResponse() throws IOException { - return new String(IOUtils.toByteArray(inputStream), Charset.forName("utf-8")); + + private static void copy(InputStream in, OutputStream out) throws IOException { + byte[] buffer = new byte[1024 * 10]; + int len; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } } - public byte[] readResponseAsArray() throws IOException { - return repairTransportedArray(IOUtils.toByteArray(inputStream)); + public void readResponseTo(OutputStream output) throws IOException { + copy(inputStream, output); } public void verifyResponse() throws IOException, JadbException { @@ -78,21 +80,4 @@ class Transport { outputStream.close(); closed = true; } - - private byte[] repairTransportedArray(byte[] encoded) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for (int i=0; i i+1 && encoded[i] == 0x0d && encoded[i+1] == 0x0a) { - //skip 0x0d - } else { - baos.write(encoded[i]); - } - } - try { - baos.close(); - } catch (IOException ioe) { - - } - return baos.toByteArray(); - } } diff --git a/test/se/vidstige/jadb/test/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/RealDeviceTestCases.java index dfa8b84..59651da 100644 --- a/test/se/vidstige/jadb/test/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/RealDeviceTestCases.java @@ -1,18 +1,14 @@ package se.vidstige.jadb.test; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.util.List; - -import org.apache.commons.io.FileUtils; import org.junit.Test; - -import se.vidstige.jadb.JadbDevice; import se.vidstige.jadb.JadbConnection; +import se.vidstige.jadb.JadbDevice; import se.vidstige.jadb.JadbException; import se.vidstige.jadb.RemoteFile; +import java.io.File; +import java.util.List; + public class RealDeviceTestCases { @Test @@ -31,18 +27,16 @@ public class RealDeviceTestCases { } @Test - public void testListFiles() throws Exception + public void testListFilesTwice() throws Exception { JadbConnection jadb = new JadbConnection(); JadbDevice any = jadb.getAnyDevice(); - List files = any.list("/"); - for (RemoteFile f : files) + for (RemoteFile f : any.list("/")) { System.out.println(f.getPath()); } - //second read on the same device - List files2 = any.list("/"); - for (RemoteFile f : files2) + + for (RemoteFile f : any.list("/")) { System.out.println(f.getPath()); } @@ -85,23 +79,11 @@ public class RealDeviceTestCases { } @Test - public void testShell() throws Exception + public void testShellExecuteTwice() throws Exception { JadbConnection jadb = new JadbConnection(); JadbDevice any = jadb.getAnyDevice(); - String s=any.executeShell("ls -la"); - System.out.println(s); - //second read on the same device - String s2=any.executeShell("ls"); - System.out.println(s2); - } - - @Test - public void testShellArray() throws Exception - { - JadbConnection jadb = new JadbConnection(); - JadbDevice any = jadb.getAnyDevice(); - byte[] s=any.executeShellGetBytearr("screencap -p"); - FileUtils.writeByteArrayToFile(new File("screen.png"), s); + any.executeShell(System.out, "ls /"); + any.executeShell(System.out, "ls", "-la", "/"); } }