2016-08-24 21:58:15 +00:00
|
|
|
package com.topjohnwu.magisk.utils;
|
|
|
|
|
2017-07-15 17:20:39 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2017-07-19 10:01:22 +00:00
|
|
|
import java.util.Collection;
|
2016-08-24 21:58:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Modified by topjohnwu, based on Chainfire's libsuperuser
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class StreamGobbler extends Thread {
|
|
|
|
|
|
|
|
private BufferedReader reader = null;
|
2017-07-19 10:01:22 +00:00
|
|
|
private Collection<String> writer = null;
|
2016-08-24 21:58:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <p>StreamGobbler constructor</p>
|
|
|
|
*
|
2017-07-17 19:34:06 +00:00
|
|
|
* <p>We use this class because sh STDOUT and STDERR should be read as quickly as
|
2016-08-24 21:58:15 +00:00
|
|
|
* possible to prevent a deadlock from occurring, or Process.waitFor() never
|
|
|
|
* returning (as the buffer is full, pausing the native process)</p>
|
|
|
|
*
|
|
|
|
* @param inputStream InputStream to read from
|
|
|
|
* @param outputList {@literal List<String>} to write to, or null
|
|
|
|
*/
|
2017-07-19 10:01:22 +00:00
|
|
|
public StreamGobbler(InputStream inputStream, Collection<String> outputList) {
|
2017-07-15 19:32:29 +00:00
|
|
|
try {
|
|
|
|
while (inputStream.available() != 0) {
|
|
|
|
inputStream.skip(inputStream.available());
|
|
|
|
}
|
|
|
|
} catch (IOException ignored) {}
|
2016-08-24 21:58:15 +00:00
|
|
|
reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
writer = outputList;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// keep reading the InputStream until it ends (or an error occurs)
|
|
|
|
try {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
2017-07-17 19:34:06 +00:00
|
|
|
if (TextUtils.equals(line, "-shell-done-"))
|
2017-07-15 17:20:39 +00:00
|
|
|
return;
|
2016-08-27 11:02:41 +00:00
|
|
|
writer.add(line);
|
2017-07-17 19:34:06 +00:00
|
|
|
Logger.shell(line);
|
2016-08-24 21:58:15 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
// reader probably closed, expected exit condition
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure our stream is closed and resources will be freed
|
|
|
|
try {
|
|
|
|
reader.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// read already closed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|