Reputation: 145
I am taking InputStream from a Process and reading it through BufferedReader. In while loop although, the br is having just one line, it is taking almost 1 minute. I checked this by printing before and after as shown in below code.
String[] sendCommand = { "/bin/bash", "/Users/apple/Downloads/yowsup-e56f2d28f38c7961a2ccf7df588462f1c9588edf/sendmessage.sh"};
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File("/Users/apple/Downloads/yowsup-e56f2d28f38c7961a2ccf7df588462f1c9588edf"));
processBuilder.environment().put("PYTHONPATH", "/Users/apple/Downloads/yowsup-e56f2d28f38c7961a2ccf7df588462f1c9588edf");
processBuilder.command(sendCommand);
Process process = processBuilder.start();
process.waitFor();
InputStream stream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String temp = null;
System.out.println("before");
long start_time = System.currentTimeMillis();
while ((temp = br.readLine()) != null)
{
str = temp;
}
long end_time = System.currentTimeMillis();
long time_taken = end_time-start_time;
System.out.println("after");
System.out.println("Time taken in while loop: "+time_taken/1000);
The result was:
before
after
Time taken in while loop: 67
I want to improve the time taken as 1 minute is very slow. What are the possible options? Is it dependent on what Process I am using? Please help me.
Upvotes: 1
Views: 1874
Reputation: 310860
It's not the reading, it's the writing. BufferedReader
can't go any faster than the output is produced. If there's nothing to read, it blocks. You're looking at the wrong end.
NB Your loop is basically pointless, or at best very odd. You're reading every line the process ever produces and throwing away all but the last line. Surely you should do something with every line?
Upvotes: 2