From b4f5083d0012f43518ed1e0f90b677cb97fe39ba Mon Sep 17 00:00:00 2001 From: Daniel Friederich Date: Sun, 6 Nov 2016 20:43:11 -0600 Subject: [PATCH] Change AdbFilterInputStream.read not to wait for more data The previous AdbFilterInputStream.read function was always waiting for buffer to fill, even if this meant to block for a long time. Now it checks if the next read will block. The one case it still could block, when reading a 0xd (carriage return) is not expected to occur for interactive output (at least as the last character of some input). This removes the need for a separate executeShellRaw method. --- .../vidstige/jadb/AdbFilterInputStream.java | 5 ++++ src/se/vidstige/jadb/JadbDevice.java | 28 ++++--------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/se/vidstige/jadb/AdbFilterInputStream.java b/src/se/vidstige/jadb/AdbFilterInputStream.java index 49ced53..eb0277c 100644 --- a/src/se/vidstige/jadb/AdbFilterInputStream.java +++ b/src/se/vidstige/jadb/AdbFilterInputStream.java @@ -31,6 +31,11 @@ public class AdbFilterInputStream extends FilterInputStream { if (b == -1) return n == 0 ? -1 : n; buffer[offset + n] = (byte) b; n++; + + // Return as soon as no more data is available (and at least one byte was read) + if (in.available() <= 0) { + return n; + } } return n; } diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 5d976f7..a50c929 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -71,20 +71,15 @@ public class JadbDevice { return state; } - /** Execute a shell command with raw output. + /** Execute a shell command. * - * 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 + * @param command main command to run. E.g. "ls" + * @param args arguments to the command. * @return combined stdout/stderr stream. * @throws IOException * @throws JadbException */ - public InputStream executeShellRaw(String command, String... args) throws IOException, JadbException { + public InputStream executeShell(String command, String... args) throws IOException, JadbException { Transport transport = getTransport(); StringBuilder shellLine = new StringBuilder(command); for (String arg : args) { @@ -92,20 +87,7 @@ public class JadbDevice { shellLine.append(Bash.quote(arg)); } send(transport, "shell:" + shellLine.toString()); - 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)); + return new AdbFilterInputStream(new BufferedInputStream(transport.getInputStream())); } /**