简体   繁体   中英

BufferedReader.readLine() method blocking flow

I am hitting some commands through java code with the help of Process Builder. Following is my code

Process poc = null;
List<String> result = new LinkedList<String>();
BufferedReader response = null;

ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File(commands.get(GlobalConstants.ADB_PATH_WINDOW)));
poc = pb.start();

And I am reading its output using BufferedReader:

response = new BufferedReader(new InputStreamReader(poc.getInputStream()));
String line = "";
while ((line = response.readLine()) != null) 
{
    result.add(line);
    if (line.contains("daemon started successfully")) 
    {
        return result;
    }
}

But Sometimes line = response.readLine() method goes in infinite loop . I know what the cause is, but I am unable to resolve this by using readLine() function. Can somebody help me with different reading logic.

maybe the error is comming from another reason. Check if the EOF is really reached by:

try {
while (true) {
    result.add(response.readLine());
    if (line.contains("daemon started successfully")) 
    {
        return result;
    }
} } catch (EOFException e) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM