Using streams rather than byte arrays.

This commit is contained in:
Samuel Carlsson
2016-03-02 21:06:19 +01:00
parent 6854642071
commit 92506f643d
3 changed files with 25 additions and 64 deletions

View File

@ -52,19 +52,12 @@ public class JadbDevice {
return transport.readString(); return transport.readString();
} }
public String executeShell(String command, String ... args) throws IOException, JadbException { public void executeShell(OutputStream stdout, String command, String ... args) throws IOException, JadbException {
execShell(command, args); executeShell(command, args);
String ret = this.transport.readResponse(); this.transport.readResponseTo(stdout);
return ret;
} }
public byte[] executeShellGetBytearr(String command, String ... args) throws IOException, JadbException { private void executeShell(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 {
getTransport(); getTransport();
StringBuilder shellLine = new StringBuilder(command); StringBuilder shellLine = new StringBuilder(command);
for (String arg : args) for (String arg : args)
@ -92,6 +85,7 @@ public class JadbDevice {
private int getMode(File file) private int getMode(File file)
{ {
//noinspection OctalInteger
return 0664; return 0664;
} }

View File

@ -1,8 +1,5 @@
package se.vidstige.jadb; package se.vidstige.jadb;
import org.apache.commons.io.IOUtils;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -32,12 +29,17 @@ class Transport {
return readString(length); 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 { public void readResponseTo(OutputStream output) throws IOException {
return repairTransportedArray(IOUtils.toByteArray(inputStream)); copy(inputStream, output);
} }
public void verifyResponse() throws IOException, JadbException { public void verifyResponse() throws IOException, JadbException {
@ -78,21 +80,4 @@ class Transport {
outputStream.close(); outputStream.close();
closed = true; closed = true;
} }
private byte[] repairTransportedArray(byte[] encoded) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i=0; i<encoded.length; i++) {
if (encoded.length > 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();
}
} }

View File

@ -1,18 +1,14 @@
package se.vidstige.jadb.test; 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 org.junit.Test;
import se.vidstige.jadb.JadbDevice;
import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.JadbDevice;
import se.vidstige.jadb.JadbException; import se.vidstige.jadb.JadbException;
import se.vidstige.jadb.RemoteFile; import se.vidstige.jadb.RemoteFile;
import java.io.File;
import java.util.List;
public class RealDeviceTestCases { public class RealDeviceTestCases {
@Test @Test
@ -31,18 +27,16 @@ public class RealDeviceTestCases {
} }
@Test @Test
public void testListFiles() throws Exception public void testListFilesTwice() throws Exception
{ {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice(); JadbDevice any = jadb.getAnyDevice();
List<RemoteFile> files = any.list("/"); for (RemoteFile f : any.list("/"))
for (RemoteFile f : files)
{ {
System.out.println(f.getPath()); System.out.println(f.getPath());
} }
//second read on the same device
List<RemoteFile> files2 = any.list("/"); for (RemoteFile f : any.list("/"))
for (RemoteFile f : files2)
{ {
System.out.println(f.getPath()); System.out.println(f.getPath());
} }
@ -85,23 +79,11 @@ public class RealDeviceTestCases {
} }
@Test @Test
public void testShell() throws Exception public void testShellExecuteTwice() throws Exception
{ {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice(); JadbDevice any = jadb.getAnyDevice();
String s=any.executeShell("ls -la"); any.executeShell(System.out, "ls /");
System.out.println(s); any.executeShell(System.out, "ls", "-la", "/");
//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);
} }
} }