Java output from a process handler overwritten when using BufferedReader

I am trying to run an external Java program and read the output. The program is a C ++ Linux application that runs a data mining algorithm and prints samples found on standard output. I want to be able to read this output from my Java application and show patterns using a table. The problem is that the output size is quite large (as a test, it produces 6.5 MB in 30 seconds). I use ProcessBuilder and read the output using an InputStreamReader buffered using a BufferedReader, as you can see in the following code:

      String[] cmd = {"./clogen_periodic", selected, support, "-t 4"};
      Process p = new ProcessBuilder(cmd).start();
      input = new BufferedReader (new InputStreamReader(p.getInputStream()));

      while ((line = input.readLine()) != null) {
         ...
         process line;
         ...
      }

The problem is that the result is damaged. When I run the same program on the console, the output is correct, but when I use the Java application, some lines are concatenated. More precisely, the output should be like this:

      TMEmulation log_pseduo_allocation (34985) (2 45 76 89 90)
      __divw clock timer (8273) (4 6 67 4 2)

but it looks like

      TMEmulation log_pseduo_allocation (34985) (2__divw 45clock 76timer (89 8273) 904) (6 67 4 2)

Any idea on a possible problem?

Thank you very much in advance, Patricia

+3
source share
4 answers

Several features are associated with the called program.

1) since @Artefacto says that C ++ program output cannot be fully buffered, so call setvbuf to make it consistent. those. the first output is partially buffered, and the second is not and therefore is first reset after the second. In general, buffering may differ when invoked from the command line and process.

2) , - java, .

, , / .

+1

stdout stderr , . , , , , (, stdout).

, , . http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

(. 2), stderr stdout, , .

, , .

0

++ setvbuf _IOLBF. , ++, , , |, .

0

If you are doing System.out.print () or someday for debugging in each iteration at the moment, try putting all the lines from all iterations on one line and give it a try.

Perhaps your output method prints asynchronously. Therefore, your printed result may be damaged, but not the one you received from the input stream.

Just an idea ...

0
source

All Articles