Skip to content

Commit

Permalink
Change AdbFilterInputStream.read not to wait for more data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danielfriederich authored and vidstige committed Nov 7, 2016
1 parent f8c9098 commit b4f5083
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/se/vidstige/jadb/AdbFilterInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public int read(byte[] buffer, int offset, int length) throws IOException {
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;
}
Expand Down
28 changes: 5 additions & 23 deletions src/se/vidstige/jadb/JadbDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,23 @@ public State getState() throws IOException, JadbException {
return state;
}

/** 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.
/** Execute a shell command.
*
* @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) {
shellLine.append(" ");
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()));
}

/**
Expand Down

0 comments on commit b4f5083

Please sign in to comment.