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();
}
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;
}

View File

@ -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<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;
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<RemoteFile> files = any.list("/");
for (RemoteFile f : files)
for (RemoteFile f : any.list("/"))
{
System.out.println(f.getPath());
}
//second read on the same device
List<RemoteFile> 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", "/");
}
}