Add executeShellRaw method. Avoids buffering issues

The buffering used in executeShell causes issues when using shell:logcat,
the character per character implementation of the InputStream handling in AdbFilterInputStream together with the BufferedInputStream class
cause that the caller only gets result once either the process ends or 8k of output is avilable. The executeShellRaw method bypasses
the custom buffer handling, allowing the caller to receive the output without delay.
Note that the issue is only apparent when using a long running commamd with little output.
This commit is contained in:
Daniel Friederich 2016-11-03 15:47:23 -05:00 committed by Samuel Carlsson
parent c5197eab01
commit 22f2442731

View File

@ -4,11 +4,7 @@ import se.vidstige.jadb.managers.Bash;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JadbDevice {
public enum State {
@ -75,7 +71,20 @@ public class JadbDevice {
return state;
}
public InputStream executeShell(String command, String... args) throws IOException, JadbException {
/** Execute a shell command with raw output.
*
* This function differs from executeShell in that no buffering and newline filtering is performed.
*
* Especially the buffering may be an issue if the output of an ongoing command should be displayed,
* e.g. the output of running logcat.
*
* @param command main command.
* @param args arguments to the commands
* @return combined stdout/stderr stream.
* @throws IOException
* @throws JadbException
*/
public InputStream executeShellRaw(String command, String... args) throws IOException, JadbException {
Transport transport = getTransport();
StringBuilder shellLine = new StringBuilder(command);
for (String arg : args) {
@ -83,7 +92,20 @@ public class JadbDevice {
shellLine.append(Bash.quote(arg));
}
send(transport, "shell:" + shellLine.toString());
return new AdbFilterInputStream(new BufferedInputStream(transport.getInputStream()));
return transport.getInputStream();
}
/** Execute a shell command.
*
* @param command main command.
* @param args arguments to the commands
* @return combined stdout/stderr stream.
* @throws IOException
* @throws JadbException
*/
public InputStream executeShell(String command, String... args) throws IOException, JadbException {
InputStream inputStream = executeShellRaw(command, args);
return new AdbFilterInputStream(new BufferedInputStream(inputStream));
}
/**
@ -101,7 +123,12 @@ public class JadbDevice {
}
send(transport, "shell:" + shellLine.toString());
if (output != null) {
transport.readResponseTo(new AdbFilterOutputStream(output));
AdbFilterOutputStream out = new AdbFilterOutputStream(output);
try {
transport.readResponseTo(out);
} finally {
out.close();
}
}
}