0

I'm currently running a command on Android, and am expecting an immediate result when the display is touched. The command, "su -c getevent -l", when run in a terminal works perfectly; every screen touch immediately prints that line to the terminal.

However, when running the following code, the read() command blocks, and doesn't unblock (and runs Log.d()) when new data comes in:

Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", "getevent -l" });
InputStreamReader reader = new InputStreamReader(process.getInputStream());
while (reader.read() != -1) {
    Log.d("", "Got data from reader.read()");
}

I can sometimes get this to unblock by saturating the su process with a ton of data. Hence, this seems like some kind of a buffering issue to me, but I'm not using a BufferedReader.

Furthermore, even if I manually check for more data in a loop (via reader.ready()), it still fails to detect that data is ready to be read until I touch the screen a number of times (again, seemingly filling some unknown buffer):

while(true){
    while(reader.ready()){
        Log.d("", "Got data from reader.read()");
    }
    Thread.sleep(100);
}

Is there any buffering that might be causing this? Or a better way to directly access the input stream?

Shookit
  • 1,162
  • 2
  • 13
  • 29
  • Are the data in the process properly flushed? – Olivier Grégoire Jul 27 '15 at 16:59
  • Is there any way to do this manually from the consumer side? I don't have access to the su or getevent source code. Also, the command seems to work properly in the terminal, which makes me not think that the issue is flushing-related. – Shookit Jul 27 '15 at 17:00
  • The fact that it's working properly in the terminal has nothing to do with it. From what I read over the internet, `getevent` buffers and releases only when the buffer of 4096 byte is full. I don't have a solution. – Olivier Grégoire Jul 27 '15 at 18:43
  • Olivier, wouldn't you expect the terminal and the java code to behave in a similar manner from a buffering standpoint, though? I don't see why it would be flushed frequently to the terminal but not in the Java app... – Shookit Jul 27 '15 at 20:25
  • Actually, what I'm trying to do might be impossible: http://stackoverflow.com/questions/4230837/java-cant-get-stdout-data-from-process-unless-its-manually-flushed Any other suggestions are welcome, however. – Shookit Jul 27 '15 at 20:36
  • No, stdin/stdout/stderr are an interface different from the terminal. Sometimes they can be the same. In the Java world they are. Not always. I'm not gonna elaborate because I barely know this, but see the c-library `curses` for info about that. If you want to get stuff flushed out to std, your best chance is to ask the android guys to make `getevent`'s data pipe-friendly. – Olivier Grégoire Jul 27 '15 at 22:43

0 Answers0